1. 语言笔记

numpy.zeros和numpy.array

numpy.zeros定义某种形状为0的矩阵
ham=np.zeros((4,2,4,2))
print(ham)

>>
[[[[0. 0.]  
   [0. 0.]  
   [0. 0.]  
   [0. 0.]] 

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


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

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


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

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


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

  [[0. 0.]
   [0. 0.]
   [0. 0.]
   [0. 0.]]]]
np.array是定义一个矩阵,参数必须为矩阵的元素
ham=np.array(
[
[
   [ [1,2],
    [3,4],],
   [ [5,6],
    [7,8],]
],
[
   [ [9,10],
    [11,12],],
   [ [13,14],
    [15,16],]
],
]
)
print(ham)

>>
[[[[ 1  2]
   [ 3  4]]

  [[ 5  6]
   [ 7  8]]]


 [[[ 9 10]
   [11 12]]

  [[13 14]
   [15 16]]]]
通过reshape函数在不更改数据的情况下为数组赋予新形状
ham=np.array(
[
[
   [ [1,2],
    [3,4],],
   [ [5,6],
    [7,8],]
],
[
   [ [9,10],
    [11,12],],
   [ [13,14],
    [15,16],]
],
]
)
ham=ham.reshape((4,4))
print(ham)

>>
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
Comments to: numpy.zeros和numpy.array

您的邮箱地址不会被公开。 必填项已用 * 标注