如何创建numpy数组

2026-02-16 06:39:57

1、第一步,import numpy

import numpy as np 

如何创建numpy数组

2、创建一个numpy数组

 可以手动指定所有的元素

a = np.array([1, 2, 3, 4, 5])

print(a)Out[]: [1 2 3 4 5]

3、#创建一个一行5列全为0的数组

arr5 = np.zeros(5)

print(arr5)

#[ 0.  0.  0.  0.  0.]

4、查看数组的行列

c = [[1],[2],[3]]

arr3 = np.array(c)

#第一个参数表示行,第二个参数表示列

#注意一行三列和三行一列的区别

print(arr1.shape)

#(3,),一行三列

print(arr3.shape)

#(3, 1),三行一列

print(arr2.shape)

#(2, 3)

猜你喜欢