2.2 Numpy 的创建 array

2019-02-21  本文已影响0人  吴国友

学习资料:

关键字

创建数组

a = np.array([2,23,4])  # list 1d
print(a)
# [2 23 4]

指定数据 dtype

a = np.array([2,23,4],dtype=np.int)
print(a.dtype)
# int 64

a = np.array([2,23,4],dtype=np.int32)
print(a.dtype)
# int32

a = np.array([2,23,4],dtype=np.float)
print(a.dtype)
# float64

a = np.array([2,23,4],dtype=np.float32)
print(a.dtype)
# float32

创建特定数据

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.        ]])
"""

上一篇下一篇

猜你喜欢

热点阅读