2D numpy数组的形状不正确

问题描述 投票:-3回答:1

我想创建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,)

有谁看到我错了?

python python-3.x numpy numpy-ndarray
1个回答
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只能自动转换矩形矩阵。

© www.soinside.com 2019 - 2024. All rights reserved.