将数组的numpy数组转换为一个完整的numpy数组 [英] Converting numpy arrays of arrays into one whole numpy array
本文介绍了将数组的numpy数组转换为一个完整的numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将我的数组数组变成一个数组.来自类似
I want to turn my array of array into just a single array. From something like :
array([ array([[0, 0, 0, ..., 1, 0, 0],
[0, 1, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 2, 0, 0],
...,
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 8, 0, 2],
...,
[0, 0, 0, ..., 0, 0, 0],
[1, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 1, 0, 0]], dtype=uint8)], dtype=object)
大小为(10,)的3D numpy数组大小为(10,518,32)
which has size (10,) to just the 3D numpy array which is of size (10,518, 32)
array([[[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]]], dtype=uint8)
我尝试将所有内容转换为列表,然后执行np.asarray,还尝试将所有内容定义为相同的dtype = uint8,但我无法将其转换为3D形式.
I've tried converting everything into a list then do np.asarray and also tried defining everything as the same dtype=uint8 but I couldn't get it into the 3D form.
推荐答案
一种方法是分配目标数组并将对象复制为循环.
One way is to allocate the target array and copy the objects in as a loop.
import numpy as np
x = np.array([ np.array([[0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 3, 7, 0, 0],
[0, 0, 0, 2, 0, 0]], dtype=np.uint8),
np.array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 4, 8, 0, 0],
[0, 0, 0, 8, 0, 2]], dtype=np.uint8),
np.array([[0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0],
[0, 0, 5, 9, 0, 0],
[0, 0, 0, 1, 0, 0]], dtype=np.uint8)], dtype=object)
print len(x)
print x[0].shape
y=np.zeros([len(x),x[0].shape[0],x[0].shape[1]],dtype=np.uint8)
print y.shape
for i in range(len(x)):
y[i,:,:] = x[i]
print y
如果我了解您的要求,那么这是理想的结果:
If I understand what you're asking this is the desired result:
3
(4L, 6L)
(3L, 4L, 6L)
[[[0 0 0 1 0 0]
[0 1 0 0 0 0]
[0 0 3 7 0 0]
[0 0 0 2 0 0]]
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 4 8 0 0]
[0 0 0 8 0 2]]
[[0 0 0 0 0 0]
[1 0 0 0 0 0]
[0 0 5 9 0 0]
[0 0 0 1 0 0]]]
这篇关于将数组的numpy数组转换为一个完整的numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文