Home > Back-end >  Expected shape=(None, 30), found shape=(None, 4) with sequential model
Expected shape=(None, 30), found shape=(None, 4) with sequential model

Time:01-23

My dataset is formed by 32 columns, but after seeking for the important features I found that 4 of them are the most important ones so I wanted to work just on them but this error faced me:

This is my code:

df = pd.read_csv("/content/drive/MyDrive/deepLearning/wisc_bc_data.csv")
df = df.rename(columns={'diagnosis':'Label'})


dff = pd.DataFrame(df, columns=['perimeter_worst', 'texture_worst', 'area_worst', 'area_mean','Label'])
x = pd.DataFrame(dff, columns=['perimeter_worst', 'texture_worst', 'area_worst', 'area_mean'])


y = dff["Label"].values

# Encoding categorical data from text (B and M) to integers (0 and 1)
from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()
Y = labelencoder.fit_transform(y) # M=1 and B=0
print("Labels after encoding are: ", np.unique(Y))


y_array = pd.DataFrame(Y, columns=['Label'])

#Split data into train and test to verify accuracy after fitting the model. 
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y_array, test_size=0.25, random_state=42)
print("Shape of training data is: ", X_train.shape)
print("Shape of testing data is: ", X_test.shape)

model_important = Sequential()
model_important.add(Dense(16, input_dim=30, activation='relu')) 
model_important.add(Dropout(0.2))
model_important.add(Dense(1)) 
model_important.add(Activation('sigmoid')) 
 
model_important.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model_important.summary())

But here is the error :

history = model_important.fit(X_train, y_train, verbose=1, epochs=100, batch_size=64,
                    validation_data=(X_test, y_test))

ValueError: Input 0 of layer "sequential_21" is incompatible with the layer: expected shape=(None, 30), found shape=(None, 4)

Can you help me

CodePudding user response:

You have this line of code

model_important.add(Dense(16, input_dim=30, activation='relu')) 

you set the input dimension to 30 change it to 4

  •  Tags:  
  • Related