Reputation: 83
I am doing churn prediction using keras. I have used column transformer from Sklearn. My code is--
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
def keras_classifier_wrapper():
classifier = Sequential()
classifier.add(Dense(9, input_dim=13, activation='relu'))
classifier.add(Dense(8, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return clf
clf = KerasClassifier(keras_classifier_wrapper, epochs=20, batch_size=50, verbose=0)
categorical_pipe = Pipeline([
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
numerical_pipe = Pipeline([
('imputer', SimpleImputer(strategy='median'))
])
preprocessing = ColumnTransformer(
[('cat', categorical_pipe, cat_var1),
('num', numerical_pipe, num_var1)])
model3 = Pipeline([
('preprocess', preprocessing),
('keras_clf', clf)
])
model3.fit(X_train, y_train)
But it showing an error-
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-162-1f0472b386ae> in <module>()
----> 1 model3.fit(X_train, y_train)
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/wrappers/scikit_learn.py in fit(self, x, y, **kwargs)
157 self.model = self.build_fn(**self.filter_sk_params(self.build_fn))
158
--> 159 if (losses.is_categorical_crossentropy(self.model.loss) and
160 len(y.shape) != 2):
161 y = to_categorical(y)
AttributeError: 'KerasClassifier' object has no attribute 'loss'
Can you plz tell me why this error is showing and how to solve it.
Thanks in advance
Upvotes: 2
Views: 2097
Reputation: 113
problem is in your keras_classifier_wrapper function
def keras_classifier_wrapper():
classifier = Sequential()
classifier.add(Dense(9, input_dim=13, activation='relu'))
classifier.add(Dense(8, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return clf # should be return classifier
you are trying to return clf but there is no clf it is defined afterwards. try to return classifier then it will work
Upvotes: 2