我有一个图像列表,图像形状为(50,50,3)。如何转换为numpy.ndarray的形状(19929,50,50,3)

时间:2019-07-08 07:20:18

标签: arrays list numpy

train_x=[]
val_x=[]
test_x=[]
for image in train_list:
    train_x.append(skimage.data.imread(image))
for image in val_list:
    val_x.append(skimage.data.imread(image))
for image in test_list:
    test_x.append(skimage.data.imread(image)) 

如何将train_x列表转换为形状为(len(train_x),50,50,3)的ndarray。

3 个答案:

答案 0 :(得分:1)

您可以使用numpy.stack()

import numpy as np
arrs = [np.random.randn(10, 11, 3) for i in range(5)]
arr = np.stack(arrs, axis=0)
print(arr.shape)

答案 1 :(得分:0)

function clicked(d) {

  var bounds = path.bounds(d),
        width = bounds[1][0] - bounds[0][0],
        height = bounds[1][1] - bounds[0][1],
        centerX = (bounds[0][0] + bounds[1][0]) / 2,
        centerY = (bounds[0][1] + bounds[1][1]) / 2;

    var scale = Math.max(1, Math.min(this._maxZoom, 1 / Math.max(width / zoomArea.width, height / zoomArea.height))),
        translate = [zoomArea.width / 2 - scale * centerX, zoomArea.height / 2 - scale * centerY];

  var zoomTo = d3.zoomIdentity.translate(translate[0], translate[1]).scale(scale);

  svg.transition()
    .duration(750)
    .call(zoom.transform, zoomTo);
}

答案 2 :(得分:0)

您可以预分配一个空数组,并用train_x元素填充它(我想numpy.stack()函数在幕后作用相同)

import numpy as np

train_x = [np.random.randn(50, 50, 3) for _ in range(1000)] #dummy x_train

big_arr = np.empty([len(train_x), 50, 50, 3])
big_arr[:,...] = train_x[:]

在这种情况下,我之所以选择反对stack的原因是它的灵活性。可能无法同时存储train_xbig_arr(可能导致内存溢出)。因此,如果必须在内存中使用形状为(19929,50,50,3)的数组进行处理,请尝试这样做:

big_arr = np.empty([len(train_list), 50, 50, 3])
for i, image in enumerate(train_list):
    big_arr[i,:,:,:] = skimage.data.imread(image)  # read directly from hard disc and fill the array