sam*_*sam 47 python numpy list
如何创建一个数组到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)
Run Code Online (Sandbox Code Playgroud)
我收到错误了
AttributeError: 'list' object has no attribute 'shape'
Run Code Online (Sandbox Code Playgroud)
那么,我想我需要将我的X转换为numpy数组?
fal*_*tru 55
使用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)
Run Code Online (Sandbox Code Playgroud)
NOTE X.shape返回给定数组的3项元组; [n, T] = X.shape加油ValueError.
use*_*ica 12
import numpy
X = numpy.array(the_big_nested_list_you_had)
Run Code Online (Sandbox Code Playgroud)
它仍然不会做你想要的; 你有更多的错误,比如试图将三维形状解压缩成两个目标变量test.
max*_*max 10
?如果你有列表,你可以打印它的形状,就好像它被转换成数组一样
import numpy as np
print(np.asarray(X).shape)
Run Code Online (Sandbox Code Playgroud)
小智 6
python中的list对象没有'shape'属性,因为'shape'表示所有列(或行)沿特定维度的长度均相等。
假设列表变量a具有以下属性:
a = [[2, 3, 4]
[0, 1]
[87, 8, 1]]
不可能为变量“ a”定义“形状”。这就是为什么只能通过“数组”来确定“形状”的原因,例如
b = numpy.array([[2, 3, 4]
[0, 1, 22]
[87, 8, 1]])
Run Code Online (Sandbox Code Playgroud)
我希望这个解释能很好地阐明这个问题。
小智 5
如果类型是列表,则使用len(list)andlen(list[0])获取行和列。
l = [[1,2,3,4], [0,1,3,4]]
Run Code Online (Sandbox Code Playgroud)
len(l)将是 2。
len(l[0])将是 4。