小能豆

“list”对象没有属性“shape”

py

如何创建一个数组到 numpy 数组?

def test(X, N):
    [n,T] = X.shape
    print "n : ", n
    print "T : ", T



if __name__=="__main__":

    X = [[[-9.035250067710876], [7.453250169754028], [33.34074878692627]], [[-6.63700008392334], [5.132999956607819], [31.66075038909912]], [[-5.1272499561309814], [8.251499891281128], [30.925999641418457]]]
    N = 200
    test(X, N)

我收到错误

AttributeError: 'list' object has no attribute 'shape'

那么,我认为我需要将我的 X 转换为 numpy 数组?


阅读 13

收藏
2024-10-26

共1个答案

小能豆

使用numpy.array使用shape属性。

>>> import numpy as np
>>> X = np.array([
...     [[-9.035250067710876], [7.453250169754028], [33.34074878692627]],
...     [[-6.63700008392334], [5.132999956607819], [31.66075038909912]],
...     [[-5.1272499561309814], [8.251499891281128], [30.925999641418457]]
... ])
>>> X.shape
(3L, 3L, 1L)

注意: X.shape返回给定数组的 3 项元组;[n, T] = X.shape引发ValueError

2024-10-26