1
$\begingroup$
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#Importing Dataset

dataset = pd.read_csv('C:/Users/Rupali Singh/Desktop/ML A-Z/Machine Learning A-Z Template Folder/Part 8 - Deep Learning/Section 39 - Artificial Neural Networks (ANN)/Churn_Modelling.csv')
print(dataset)
X = dataset.iloc[:, [3, 13]].values
Y = dataset.iloc[:, 13].values
print(X)

#Categorical Data

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder1 = LabelEncoder()
X[:, 1] = labelencoder1.fit_transform(X[:, 1])
try:
    labelencoder2 = LabelEncoder()
    X[:, 2] = labelencoder2.fit_transform(X[:, 2])
except IndexError: pass

#Dummy Variable
onehotencoder = OneHotEncoder(categorical_features=[1])
X = onehotencoder.fit_transform(X).toarray()

#Splitting the dataset into training set and test set

from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
print(Y_test)

#Feature Scaling

from sklearn.preprocessing import StandardScaler
feature_scaling = StandardScaler()
X_train = feature_scaling.fit_transform(X_train)
X_test = feature_scaling.transform(X_test)
print(X_train)

#Importing Keras libraries and packages

import keras
from keras.models import Sequential
from keras.layers import Dense

#Initialising the ANN

classifier = Sequential()

#Adding the input layer and hidden layer
classifier.add(Dense(input_dim=11, units=6, kernel_initializer='uniform', activation='relu'))

#Adding the second hidden layer
classifier.add(Dense(units=6, kernel_initializer='uniform', activation='relu'))

#Adding the Output Layer
classifier.add(Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))

#Compiling the ANN(Applying Stochastic Gradient)
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

#Fitting ANN to training set
classifier.fit(X_train, Y_train, batch_size=10, nb_epoch=100)
# predicting the test result
Y_pred = classifier.predict(X_test)
Y_pred = (Y_pred > 0.5)


# Making the confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(Y_test, Y_pred)
print(cm)

I am trying to fit the ANN model to my dataset, after preprocessing of data and importing the keras library, the following error comes during model fitting.

Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "C:\Users\Rupali Singh\PycharmProjects\Machine_Learning\venv\lib\site-packages\keras\engine\training.py", line 952, in fit
    batch_size=batch_size)
  File "C:\Users\Rupali Singh\PycharmProjects\Machine_Learning\venv\lib\site-packages\keras\engine\training.py", line 751, in _standardize_user_data
    exception_prefix='input')
  File "C:\Users\Rupali Singh\PycharmProjects\Machine_Learning\venv\lib\site-packages\keras\engine\training_utils.py", line 138, in standardize_input_data
    str(data_shape))
ValueError: Error when checking input: expected dense_1_input to have shape (11,) but got array with shape (3,)

I cannot understand what is wrong, please help.

$\endgroup$

3 Answers 3

1
$\begingroup$

You ANN is expecting an input of size 11 as you specified with the parmaters input_dim

classifier.add(Dense(input_dim=11, units=6, kernel_initializer='uniform', activation='relu'))

But your training set is sending array of size 3. It's probably due to tour dummy variables :

#Dummy Variable
onehotencoder = OneHotEncoder(categorical_features=[1])
X = onehotencoder.fit_transform(X).toarray()

you can set your ANN to get correct input:

classifier.add(Dense(input_dim=3, units=6, kernel_initializer='uniform', activation='relu'))
$\endgroup$
1
$\begingroup$

Adding to @vico 's answer:

In order to use binary crossentropy loss you need a one hot encoded target variable. Your output layer, instead, has only one node.

$\endgroup$
0
$\begingroup$

Thanks everyone for your help. The problem was with pre processing of my data. And after using the labelencoder and onehotencoder, I did'nt update my X array(Independent Variable).

#Importing Dataset

dataset = pd.read_csv('C:/Users/Rupali Singh/Desktop/ML A-Z/Machine Learning A-Z Template Folder/Part 8 - Deep Learning/Section 39 - Artificial Neural Networks (ANN)/Churn_Modelling.csv')
print(dataset)
X = dataset.iloc[:, 3:13].values
Y = dataset.iloc[:, 13].values
print(X)

#Categorical Data

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder1 = LabelEncoder()
X[:, 1] = labelencoder1.fit_transform(X[:, 1])
labelencoder2 = LabelEncoder()
X[:, 2] = labelencoder2.fit_transform(X[:, 2])

#Dummy Variable
onehotencoder = OneHotEncoder(categorical_features=[1])
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 1:]   #Updating X
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.