一、从头创建数组
1、创建一个长度为10的数组,数组的值都为0:
np.zeros(10,dtype=int)
array([0,0,0,0,0,0,0,0,0,0]
2、创建一个3*5的浮点型数组,数组的值全部为1
np.ones((3,5),dtype=float) array([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])
3、创建一个3*5的浮点型数组,数组的值全部为3.14
np.full((3,5),3.14) array([[3.14, 3.14, 3.14, 3.14, 3.14], [3.14, 3.14, 3.14, 3.14, 3.14], [3.14, 3.14, 3.14, 3.14, 3.14]])
4、创建一个线性序列数组;从0开始,到20结束,步长为2;它和内置函数range()类似
np.arange(0,20,2)
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
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)) array([[0.2131937 , 0.99476852, 0.12470957], [0.57641785, 0.00268608, 0.13124108], [0.0626813 , 0.73013671, 0.72995278]])
7、创建一个3*3均值为0,标准差为1的正态分布的随机数数组
Int[7]:np.random.normal(0,1,(3,3)) Out[7]: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.]])
赠送一张图
————————————————
版权声明:本文为CSDN博主「步步星愿」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_41940950/article/details/83895318