您的位置:首页 > 编程语言 > Python开发

numpy库常用操作汇总

2020-06-19 22:52 183 查看

初学python,整理numpy库中的常用操作,包括矩阵基本属性,矩阵创建,矩阵(二维数组)运算

[code]import numpy as np

#矩阵基本属性
#arrange函数生成排列
test_numpy = np.arange(15).reshape(5,3)
print(type(test_numpy))
#返回矩阵维数
test_ndim = test_numpy.ndim
print(test_ndim)
#返回矩阵维度
print(test_numpy.shape)
#返回矩阵元素个数
print(test_numpy.size)
#返回矩阵元素类型
print(test_numpy.dtype)
#返回矩阵每个元素字节数
print(test_numpy.itemsize)

#矩阵的创建
#注意array()里是列表或元组
test_mat = np.array([1,2,3,4])
print(test_mat)
#2行3列矩阵创建
test_mat = np.array([(1,2,3),(4,5,6)])
#创建全0矩阵
test_zero = np.zeros((3,4),dtype = np.int32)
print(test_zero)
#创建全1矩阵
test_one = np.ones((3,4),dtype = np.int32)
print(test_one)
#用法类似np.arange
test_space = np.linspace(0,15,17)
print(test_space)
#创建0,1均匀分布随机矩阵
test_random = np.random.rand(3,3)
print(test_random)
#创建标准正太分布随机矩阵
test_randn = np.random.randn(3,3)
print(test_randn)

#数组乘法
a = np.array([(1,2),(3,4)])
b = np.arange(0,4).reshape(2,2)
c = np.dot(a,b)
print(c)

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: