python中如何基于numpy创建矩阵
python中如何基于numpy创建矩阵
python的功能强大依赖于各种库发挥的作用,例如numpy库就提供了矩阵运算的功能,如果我们想要进行矩阵运算,首先要导入numpy的包,创建矩阵。本文介绍python中基于numpy创建矩阵的三种方法:1、手动创建;2、利用numpy数组创建;3、使用numpy.matix()函数创建矩阵。
一、导入numpy
>>>fromnumpyimport*;#导入numpy的库函数
>>>importnumpyasnp;#这个方式使用numpy的函数时,需要以np.开头。
二、python基于numpy创建矩阵方法
1、手动创建
a=np.mat('123;456;789')#中间打逗号也可以b=np.mat('1,2,3;4,5,6;7,8,9')
2、利用numpy数组创建
c=np.mat(np.arange(9))#一维的矩阵
c=np.mat(np.arange(9).reshape(3,3))
3、使用numpy.matix()函数创建矩阵
importnumpyasnp
#create2x2matrix
a=np.matrix([[1,2],[3,4]])#usingarrayofarray
print('2x2matrixis:\n',a)
#usingshapeattributetogetthetupledescribingmatrixshape
print('Thedimensionofthematrixis:',a.shape)
以上就是python中基于numpy创建矩阵的三种方法,希望能帮助到你进行创建矩阵哦~更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
怎样用python计算矩阵乘法?
python中计算矩阵乘法的方法:1、使用np.multiply()函数计算矩阵乘法函数作用:数组和矩阵对应位置相乘,输出与相乘数组/矩阵的大小一致示例:n...详情>>
2023-11-09 20:01:23
python如何调用另一个文件夹中的内容?
python中调用另外一个文件夹中的内容:1、同一文件目录下在b.py文件中用下面两条语句即可完成对a.py文件中func()函数的调用importa#引用模块a.f...详情>>
2023-11-09 19:42:45
Python变量简介
1.Python变量的赋值在编程语言中,将数据放入变量的过程叫做赋值(Assignment)。每个变量在使用前都必须赋值,变量赋值以后,该变量才会被创建。...详情>>
2023-11-09 15:05:10
Python文件读取相关方法
我们知道,对文件的读取操作需要将文件中的数据加载到内存中,而上面所用到的read()方法会一次性把文件中所有的内容全部加载到内存中。这明显是...详情>>
2023-11-09 14:45:51热门推荐
技术干货






