我怎样才能得到这个列表的等效形式?

How can I get the equivalent form of this list?

我在 Python 中有以下列表:

M = np.array([image_array_to_vector1,image_array_to_vector2,image_array_to_vector3,image_array_to_vector4])

其中每个项目代表一个已使用 ravel() 函数转换为矢量的图像。

M 在这种情况下看起来如下:

[[165 176 186 ...,   0   1   1]
 [ 46  44  46 ...,  57  49  44]
 [ 97 113 109 ...,  46  49  69]
 [139 111 101 ..., 244 236 236]]

我没有像上面那样手动执行此操作,而是执行了以下操作:

for root, dirs, files in os.walk(path):
    for file in files:
        image = Image.open(root + '/' + file)
        image_array = np.array(image)
        image_array_to_vector = image_array.ravel()
        X.append(image_array_to_vector)

当我 print X 时,我得到以下信息:

[array([165, 176, 186, ...,   0,   1,   1], dtype=uint8), array([46, 44, 46, ..., 57, 49, 44], dtype=uint8), array([ 97, 113, 109, ...,  46,  49,  69], dtype=uint8), array([139, 111, 101, ..., 244, 236, 236], dtype=uint8)]

第二种形式是否与第一种形式相同?由于第二种形式在输出中包含 arraydtype

谢谢。

M 是一个 NumPy 数组,X 是一个 NumPy 数组列表。它们是不同的。 一个区别是 X 将具有列表的方法(例如 appendremoveextend),而 M 具有 NumPy 的方法数组(例如 reshapesizesearchsorted)。

虽然某些 NumPy 函数可能会像 M 一样对 X 进行操作(因为在幕后,该函数在其参数上调用 np.arraynp.asarray) ,你可能不应该指望这个。如果您希望 X 成为 NumPy 数组,请将其明确定义为一个数组。

假设 X 中的所有数组具有相同的形状,您可以使用 [=29= 将 X 变成一个 NumPy 数组(应该与 M 相同) ].

import os
X  = []
for root, dirs, files in os.walk(path):
    for file in files:
        image = Image.open(os.path.join(root, file))
        image_array = np.array(image)
        image_array_to_vector = image_array.ravel()
        X.append(image_array_to_vector)
X = np.array(X)