I am building a ANN with Keras. The shape of my df is (120000,18). My goal is to predict based on 17 independent variables(X's) what my dependent variable(Y) will be. I have 2 questions which I added below. Here is my code:
Creating ANN
Question 1: How can all my values for y_pred_train for my Training set be the same value? Also, the predictions should show a binary result, meaning 0 or 1, meaning if prediction will be true vs false. Why am I getting 0.41542563?
Data Preprocessing
# Splitting the dataset into the 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.3, random_state = 0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
#ANN
import keras
from keras.models import Sequential
from keras.layers import Dense
# Initialising the ANN
classifier = Sequential()
# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'relu', input_dim = 17))
# Adding the second hidden layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'relu'))
# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)
Snippet of epoch at point of convergence: 7210/80030 [=>............................] - ETA: 8s - loss: 0.6822 - acc: 0.7046
# Classifying the Train set results
y_pred_train = classifier.predict(X_train)
y_pred_train
Out[50]:
array([[0.41542563],
[0.41542563],
[0.41542563],
...,
[0.41542563],
[0.41542563],
[0.41542563]], dtype=float32)
Creating the Confusion Matrix
Question 2: When I try to execute a Confusion matrix, I get an error.
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
ValueError-Traceback (most recent call last)
<command-207679> in <module>()
1 # Making the Confusion Matrix
2 from sklearn.metrics import confusion_matrix
----> 3 cm = confusion_matrix(y_test, y_pred)
/databricks/python/lib/python3.5/site-packages/sklearn/metrics/classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight)
238
239 """
--> 240 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
241 if y_type not in ("binary", "multiclass"):
242 raise ValueError("%s is not supported" % y_type)
/databricks/python/lib/python3.5/site-packages/sklearn/metrics/classification.py in _check_targets(y_true, y_pred)
70 y_pred : array or indicator matrix
71 """
---> 72 check_consistent_length(y_true, y_pred)
73 type_true = type_of_target(y_true)
74 type_pred = type_of_target(y_pred)
/databricks/python/lib/python3.5/site-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
179 if len(uniques) > 1:
180 raise ValueError("Found input variables with inconsistent numbers of"
--> 181 " samples: %r" % [int(l) for l in lengths])
182
183
ValueError: Found input variables with inconsistent numbers of samples: [34299, 22866]