我想创建5x5 numpy数组。声明如下:
aa = np.array([[2.82842712, 2.23606798, 2.0, 2.23606798, 2.82842712],
[2.23606798, 1.41421356, 1.0, 1.41421356, 2.23606789],
[2.0, 1.0, 0.0, 1.0, 2.0],
[2.23606789, 1,41421356, 1.0, 1.41421356, 2.23606789],
[2.82842712, 2.23606789, 2.0, 2.23606798, 2.82842712]])
但当我检查它的大小是5x1。
控制台阵列中的打印如下:
MATRICE aa:
[[2.82842712, 2.23606798, 2.0, 2.23606798, 2.82842712]
[2.23606798, 1.41421356, 1.0, 1.41421356, 2.23606789]
[2.0, 1.0, 0.0, 1.0, 2.0]
[2.23606789, 1, 41421356, 1.0, 1.41421356, 2.23606789]
[2.82842712, 2.23606789, 2.0, 2.23606798, 2.82842712]]
Shape: (5,)
有谁看到我错了?
你在第4行有六个元素,[2.23606789, 1, 41421356, 1.0, 1.41421356, 2.23606789]
假设是1.41421356
改为:
import numpy as np
aa = np.array([[2.82842712, 2.23606798, 2.0, 2.23606798, 2.82842712],
[2.23606798, 1.41421356, 1.0, 1.41421356, 2.23606789],
[2.0, 1.0, 0.0, 1.0, 2.0],
[2.23606789, 1.41421356, 1.0, 1.41421356, 2.23606789],
[2.82842712, 2.23606789, 2.0, 2.23606798, 2.82842712]])
print(aa.shape)
产量
(5, 5)
正如@FHTMitchell提到的numpy只能自动转换矩形矩阵。