关键字
array
:创建数组dtype
:指定数据类型zeros
:创建数据全为0ones
:创建数据全为1empty
:创建数据接近0arrange
:按指定范围创建数据linspace
:创建线段
创建数组
a = np.array([2,23,4]) # list 1d
print(a)
# [2 23 4]
指定数据 dtype
a = np.array([[2,23,4],[2,32,4]]) # 2d 矩阵 2行3列
print(a)
"""
[[ 2 23 4]
[ 2 32 4]]
"""
创建全零数组
a = np.zeros((3,4)) # 数据全为0,3行4列
"""
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
"""
创建全一数组, 同时也能指定这些特定数据的 dtype:
a = np.ones((3,4),dtype = np.int) # 数据为1,3行4列
"""
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
"""
创建全空数组, 其实每个值都是接近于零的数:
a = np.empty((3,4)) # 数据为empty,3行4列
"""
array([[ 0.00000000e+000, 4.94065646e-324, 9.88131292e-324,
1.48219694e-323],
[ 1.97626258e-323, 2.47032823e-323, 2.96439388e-323,
3.45845952e-323],
[ 3.95252517e-323, 4.44659081e-323, 4.94065646e-323,
5.43472210e-323]])
"""
用 arange 创建连续数组:
a = np.arange(10,20,2) # 10-19 的数据,2步长
"""
array([10, 12, 14, 16, 18])
"""
使用 reshape 改变数据的形状
a = np.arange(12).reshape((3,4)) # 3行4列,0到11
"""
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
"""
用 linspace 创建线段型数据:
a = np.linspace(1,10,20) # 开始端1,结束端10,且分割成20个数据,生成线段
"""
array([ 1. , 1.47368421, 1.94736842, 2.42105263,
2.89473684, 3.36842105, 3.84210526, 4.31578947,
4.78947368, 5.26315789, 5.73684211, 6.21052632,
6.68421053, 7.15789474, 7.63157895, 8.10526316,
8.57894737, 9.05263158, 9.52631579, 10. ])
"""
同样也能进行 reshape 工作:
a = np.linspace(1,10,20).reshape((5,4)) # 更改shape
"""
array([[ 1. , 1.47368421, 1.94736842, 2.42105263],
[ 2.89473684, 3.36842105, 3.84210526, 4.31578947],
[ 4.78947368, 5.26315789, 5.73684211, 6.21052632],
[ 6.68421053, 7.15789474, 7.63157895, 8.10526316],
[ 8.57894737, 9.05263158, 9.52631579, 10. ]])
"""
参考博客:https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/2-2-np-array/