ValueError: cannot reshape array of size 49 into shape (224,224,1)

ValueError Traceback (most recent call last)
Cell In[14], line 6
4 for i, j in loo.split(X):
5 X_train, X_test = X.iloc[i,:], X.iloc[j,:]
----> 6 X_train=X_train.values.reshape(-1,224,224,1)
7 X_test=X_test.values.reshape(-1,224,224,1)
8 y_train, y_test = y[i], y[j]
I am getting error when i am trying to resize X value.

import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from sklearn.model_selection import LeaveOneOut
from sklearn.preprocessing import MinMaxScaler

dataset=pd.read_csv(“C:/Users/User/Documents/Data/image.csv”)
image_paths = dataset[‘image_path’].values
cholesterol_levels = dataset[‘cholesterol’].values
loo = LeaveOneOut()

Define the CNN model

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation=‘relu’, input_shape=(224, 224, 1
)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=(3, 3), activation=‘relu’))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation=‘relu’))
model.add(Dense(1))

Compile the model

model.compile(loss=‘mean_squared_error’, optimizer=‘adam’)

X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]

Perform LOOCV

predictions =

for i, j in loo.split(X):
X_train, X_test = X.iloc[i,:], X.iloc[j,:]
X_train=X_train.values.reshape(-1,224,224,1)
X_test=X_test.values.reshape(-1,224,224,1)
y_train, y_test = y[i], y[j]

   model.fit(X_train, y_train, batch_size=32, epochs=10, verbose=0)    
   prediction = model.predict(X_test)
   predictions.append(prediction)

Evaluate the predictions

predictions = np.array(predictions).flatten()
mse = np.mean((predictions - y) ** 2)
mae = np.mean(np.abs(predictions - y))
r_squared = np.corrcoef(predictions, y)[0, 1] ** 2

print(‘Mean Squared Error (MSE):’, mse)
print(‘Mean Absolute Error (MAE):’, mae)
print(‘R-squared:’, r_squared)

please suggest me

Thanks & Regards

Hi @Sumana_Naskar, You are getting this error because X_train has only 49 values but you are trying to reshape it as (224,224,1) which will have 50,176 values. You cannot represent 49 values as 50,176 values. Thank You.

I am having only 50images of patients.please suggest how i can set size for 50images only?

Thanks & Regards

Hi @Sumana_Naskar, I think your X_train does not have image data because if it has image data then the shape of X_train will be (Height_of_image, Width_of_image, No,of channels) which will be 3D array but you have only single dimension . And again as you said you have only 50 images, which will be very less to train your model and you won’t get the best predictions if you train on very less images. Thank You.