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

2024-04-25 14:12:44 发布

您现在位置:Python中文网/ 问答频道 /正文

如何创建数组到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数组?


Tags: nonametestnumpyifobjectmaindef
3条回答

或者,您可以使用np.shape(...)

例如:

import numpy as np

a=[1,2,3]

并且np.shape(a)将给出(3,)的输出

import numpy
X = numpy.array(the_big_nested_list_you_had)

它仍然不能满足您的需要;您有更多的错误,比如试图在test中将三维形状解压成两个目标变量。

使用^{}来使用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

相关问题 更多 >

    热门问题