0
votes

I have a problem about reshaping dataframe for implementing CNN.

My dataframe shape : train.shape -> (230, 67502).

Then I wrote a code shown below.

Y_train = train["Label"]
X_train = train.drop(labels = ["Label"],axis = 1) 

When I run this code below for plotting some images by iloc, It throws an error

img = X_train.iloc[0].to_numpy()
img = np.pad(img, (0, (67600-img.shape[0])), 'constant').reshape((260, 260))
plt.imshow(img,cmap='gray')
plt.title(train.iloc[0,0])
plt.axis("off")
plt.show()

Then I normalize X_train

X_train = X_train / 255.0
print("x_train shape: ",X_train.shape)

When I reshape X_train , it throws an error

X_train = X_train.values.reshape(-1, 260, 260)
print("x_train shape: ",X_train.shape)

ValueError: cannot reshape array of size 15525000 into shape (260,260)

How can I fix the issue?

3
260*260 doesn't equal 67500, so you would need to pad you array before you can reshape it. - Dan
@Dan How can I do pad process? - Tony Brand
@Ben.T Is it possible to share some codes related with it? - Tony Brand
@Ben.T How can I add more values by using np.pad in the dataframe? - Tony Brand
@TonyBrand try img = np.pad(img, (0, (67600-img.shape[0])), 'constant').reshape((260, 260))? - Ben.T

3 Answers

0
votes

Are you absolutely sure you need a 260 * 260 image? As 67500 == 270 * 250, you can try that and see how it looks! Otherwise you would need to PAD - how exactly would depend upon your image.

But, one of the simplest might be to add 100 more 0's at the end to make it 67600 - hence 260 * 260

0
votes

Well, 260*260 is 67600 and not 67500. So you can't cast your array into those dimensions.

To actually cast it into those dimensions you would need to pad or normalize the source image arrays. For example, check the Keras pad_sequences functionality documentation on dealing with this kind of issues.

0
votes

Solution :

X_train = np.pad(X_train, ((0,0), (0, (67600-X_train.shape[1]))), 'constant').reshape(-1, 260, 260)