numpy的数组创建

1、创建一个长度为10的数组,数组的值都为0:

print(np.zeros(10,dtype=int))

2、创建一个3*5的浮点型数组,数组的值全部为1

np.ones((3,5),dtype=float)

3、创建一个3*5的浮点型数组,数组的值全部为8

np.full((3,5),8)

4、创建一个线性序列数组;从0开始,到20结束,步长为2;它和内置函数range()类似

print(np.arange(0,20,2))

5、创建5个元素数组,这5个元素均匀分布到0到1之间

np.linspace(0,1,5)

array([0.  , 0.25, 0.5 , 0.75, 1.  ])

6、创建一个3*3的在0~1范围内的随机数组成的数组

np.random.random((3,3))

7、创建一个3*3均值为0,标准差为1的正态分布的随机数数组

np.random.normal(0,1,(3,3)) 

array([[ 1.15213505, 3.70161801, 0.9126224 ], [-0.19543063, 1.54035371, 0.32575042], [ 0.16181688, -2.81311219, -0.86688313]])

8、创建一个3*3的,[0,10)区间的随机整型数

np.random.randint(0,10,(3,3))

array([[2, 6, 3],
       [4, 3, 8],
       [4, 3, 8]])

9、创建一个3*3的单位矩阵

np.eye(3)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

 

posted @ 2019-05-28 13:24  xfxlesson  阅读(690)  评论(0编辑  收藏  举报