0
$\begingroup$

I am new at this and am pretty sure this is a stupid question, but here it goes:

Where can I see the results of a model's prediction?

I did this course about deep learning, followed the tutorial, ran all the code, and trained this artificial neural network to perform customer churn prediction.

In the "Predicting the Test set results". It prints the y_pred as below:

y_pred = ann.predict(X_test)
y_pred = (y_pred > 0.5)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

Output:

[[0 0]
 [1 1]
 [1 1]
 ...
 [0 0]
 [1 1]
 [1 0]]

I thought it was the y_pred data that would show the prediction for each customer, but it only has 756 results and the dataset has more than 3000 customers.

Where can I see the prediction for each customer in the dataset?

Here is the whole code, in case, you guys need to check it:

import numpy as np
import pandas as pd
import tensorflow as tf

tf.__version__


dataset = pd.read_csv('Churn_Modelling2.csv', sep=';')
X = dataset.iloc[:, 3:-1].values
y = dataset.iloc[:, -1].values
print(X)
print(y)

print(X)

print(y)



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)



### Initializing the ANN
"""

ann = tf.keras.models.Sequential()

"""### Adding the input layer and the first hidden layer"""

ann.add(tf.keras.layers.Dense(units=6, activation='relu'))

"""### Adding the second hidden layer"""

ann.add(tf.keras.layers.Dense(units=6, activation='relu'))

"""### Adding the output layer"""

ann.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))

"""## Part 3 - Training the ANN

### Compiling the ANN
"""

ann.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

"""### Training the ANN on the Training set"""

ann.fit(X_train, y_train, batch_size = 32, epochs = 100)

"""## Part 4 - Making the predictions and evaluating the model

### Predicting the Test set results
"""

y_pred = ann.predict(X_test)
y_pred = (y_pred > 0.5)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

pred=np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1)
np.savetxt("predicitons.csv", pred, delimiter=",")

"""### Making the Confusion Matrix"""

from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)

Thank you in advance.

$\endgroup$

1 Answer 1

1
$\begingroup$

The y_pred vector should hold all predictions for your observations present in the X_test dataset, which should be 756 observations. If you want to use your model on the whole dataset you can simply use the .predict() method on your X_train dataset:

# predict on your training dataset
ann.predict(X_train)
# predict on your test dataset
ann.predict(X_test)
$\endgroup$
2
  • $\begingroup$ Thank you! I search a lot for an answer and didn't find it. I suspected it was something simple. If you dont mind, I have another question: How do i save in a csv file the predictons next to correspondent customer id (which is the second columns in the dataset)? $\endgroup$ Commented Sep 18, 2021 at 19:14
  • $\begingroup$ There are probably several ways, but I personally would find it easiest to convert the numpy array to a pandas dataframe and save that to a csv file (or excel file). Have a look at the pandas library and the .to_csv() method. $\endgroup$
    – Oxbowerce
    Commented Sep 18, 2021 at 20:28

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.