我想将3D列表转换为numpy数组。
当我尝试使用np.array(list)或np.asarray(list)时,它会给出2D numpy数组形状(6,10)。
我怎么能得到3维的numpy数组(比如形状应该是(6,10,10)。有人可以帮我解决这个问题。
我试图转换的列表如下所述
[[[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]]
[[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L] [4L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]]
[[0L, 1L, 2L] [0L, 1L, 2L] [0L, 1L, 2L] [0L, 1L, 2L, 3L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 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, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]]]
如果您构建一个2D列表并使用np.array()
将其转换为numpy,它将返回一个具有2维的numpy数组。
如果你构建一个3D列表并使用np.array()
将其转换为numpy,它将返回一个3维的numpy数组。
最小的工作示例:
my_list = [[[0,1,2],[0,1,2]],[[0,1,2],[0,1,2]]]
my_list
[[[0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2]]]
my_np_array = np.array(my_list)
my_np_array.shape
(2, 2, 3)
那是因为这不是3D列表。列表有格式[item,item]; 2D列表将是[[],[]]。上面的文件中没有内部级别分隔符(',')。将它加载到python中,你得到一个6,10元组,第一个有效的条目,之后没有逗号,所以错误输出。