Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 三维Numpy阵列到阵列转换列表_Python_Arrays_Numpy - Fatal编程技术网

Python 三维Numpy阵列到阵列转换列表

Python 三维Numpy阵列到阵列转换列表,python,arrays,numpy,Python,Arrays,Numpy,我正在使用来自PIL的图像,打开图像并将其加载到维度(300300,3)的numpy数组中。这些数组被附加到一个列表中,然后我想把这个列表转换成一个numpy数组。凡是应该起作用的东西,都没有。我不断地得到一个奇怪的错误: ValueError:无法将输入数组从形状(300,300,3)广播到形状(300,300) 下面是一个小示例代码: 循环前 training_data=[] 循环中 image=image.open(path).resize((300300),image.ANT

我正在使用来自PIL的图像,打开图像并将其加载到维度(300300,3)的numpy数组中。这些数组被附加到一个列表中,然后我想把这个列表转换成一个numpy数组。凡是应该起作用的东西,都没有。我不断地得到一个奇怪的错误:

ValueError:无法将输入数组从形状(300,300,3)广播到形状(300,300)
下面是一个小示例代码:

  • 循环前
training_data=[]
  • 循环中
image=image.open(path).resize((300300),image.ANTIALIAS)
training_data.append(np.asarray(图像))
  • 外环
training\u data=np.数组(training\u数据)

简单的问题是我得到了上面提到的错误。非常感谢您的帮助。

您很可能已经收集了不同大小的阵列列表,可能有些是黑白的,有些是彩色的:

In [17]: alist = []                                                                                  
In [18]: alist.append(np.ones((300,300)))        # bw                                                            
In [19]: alist.append(np.ones((300,300,3)))      # color                                               
In [20]: np.array(alist)                                                                             
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  #!/usr/bin/python3
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-7512d762195a> in <module>
----> 1 np.array(alist)

ValueError: could not broadcast input array from shape (300,300,3) into shape (300,300)

阅读您的答案后,我决定将所有形状数据拉入一个文件以查看形状,我发现列表中所有元素的形状都100%一致。您可以尝试
np.stack(training\u data)
In [21]: np.stack(alist)                                                                             
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-724d9c1d0554> in <module>
----> 1 np.stack(alist)

<__array_function__ internals> in stack(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/numpy/core/shape_base.py in stack(arrays, axis, out)
    425     shapes = {arr.shape for arr in arrays}
    426     if len(shapes) != 1:
--> 427         raise ValueError('all input arrays must have the same shape')
    428 
    429     result_ndim = arrays[0].ndim + 1

ValueError: all input arrays must have the same shape