Why can't i change the shape or dimensions of my list?

61 Views Asked by At
target = []
images = []

flattened_data =[]

these are 3 lists I made to append my dataset after the preprocessing, but haven't been able to do so until now because of the difference in the dimensions of these lists and the lists which I want to append into these lists.

flattened_data = np.array(flattened_data)
flattened_data = flattened_data.reshape(flat.shape)

When I try to append using this method this error below is what I get:

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,)

Here, flat is the list I get after running the code below.

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()

Just like this case, I have two more lists which I want to append in target and images respectively but haven't been able to so due to same error, i.e. difference in shape or dimension.

1

There are 1 best solutions below

0
9769953 On
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