为什么我无法更改列表的形状或尺寸?

问题描述 投票:0回答:1
target = []
images = []

flattened_data =[]

这些是我在预处理后附加数据集的 3 个列表,但由于这些列表和我想要附加到这些列表中的列表的维度不同,所以到目前为止还无法这样做

flattened_data = np.array(flattened_data)
flattened_data = flattened_data.reshape(flat.shape)
for category in class_names:  # Iterate over the list of category names
    for img in os.listdir(path):
      
      np.vstack((flattened_data,flat))

当我尝试使用此方法附加时,我得到的错误如下

ValueError                                Traceback (most recent call last)
<ipython-input-29-f79792c8d9c0> in <cell line: 2>()
      1 flattened_data = np.array(flattened_data)
----> 2 flattened_data = flattened_data.reshape(flat.shape)
      3 for category in class_names:  # Iterate over the list of category names
      4     for img in os.listdir(path):
      5 

ValueError: cannot reshape array of size 0 into shape (67500,)

这里是我运行下面的代码后得到的列表

for category in class_names:  # Iterate over the list of category names
    for img in os.listdir(path):
        img_array = imread(os.path.join(path, img))
        img_resized = resize(img_array,(150,150,3))
        flat =  img_resized.flatten()

就像这种情况一样,我还有两个列表,我想分别附加到目标和图像中,但由于相同的错误而无法这样做,即形状或尺寸的差异

python-3.x numpy machine-learning image-classification supervised-learning
1个回答
0
投票
flattened_data = []

for category in class_names:  # Iterate over the list of category names
    for img in os.listdir(path):
        img_array = imread(os.path.join(path, img))
        img_resized = resize(img_array,(150,150,3))
        flattened_data.append(img_resized.flatten())

flattened_data = np.hstack(flattened_data)
# or
# flattened_data = np.vstack(flattened_data)
# if you want to add it as an extra dimension
© www.soinside.com 2019 - 2024. All rights reserved.