简体   繁体   中英

Incorrect shape of 2D numpy array

I want to create 5x5 numpy array. Declaration looks like:

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

but when i check it's size is 5x1.

Printed in console array looks like:

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,)

Does anyone see where I'm wrong?

You have six elements in the 4th line, [2.23606789, 1, 41421356, 1.0, 1.41421356, 2.23606789] assuming is 1.41421356 change to this:

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)

Output

(5, 5)

As @FHTMitchell mentioned numpy only auto-converts rectangular matrices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM