0

I am trying to plot the actual vs predicted values of my neural network which I created using keras.

What I want exactly is to have my data scattered and to have the best fit curve for the training and testing data set?

below is the code:

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
from keras.models import Sequential
from keras.layers import Dense 
import os




#Load the dataset from excel
data = pd.read_csv('C:\\Excel Files\\Neural Network\\diabetes1.csv', sep=';') 

#Viewing the Data

data.head(5)

import seaborn as sns
data['Outcome'].value_counts().plot(kind = 'bar')

#Split into input(x) and output (y) variables 
predictiors = data.iloc[:,0:8]
response = data.iloc[:,8]

#Create training and testing vars 
X_train, X_test, y_train, y_test = train_test_split(predictiors, response, test_size=0.2)
print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)

# Define the keras model - Layer by Layer sequential model
kerasmodel = Sequential()
kerasmodel.add(Dense(12, input_dim=8, activation='relu')) #First Hidden Layer, 12 neurons in and 8 inputs with relu activation functions 
kerasmodel.add(Dense(8, activation='relu')) #Relu to avoid vanishing/exploding gradient problem -#
kerasmodel.add(Dense(1, activation='sigmoid')) #since output is binary so "Sigmoid" - #OutputLayer

#Please not weight and bias initialization are done by keras default nethods using "'glorot_uniform'"

# Compiling model
kerasmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

#fintting model
kerasmodel.fit(X_train, y_train, epochs=50, batch_size=10)

# Train accuracy
_, accuracy = kerasmodel.evaluate(X_train, y_train)
print('Train Accuracy: %.2f' % (accuracy*100))

I want to have plots like these:

1

2

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36

3 Answers3

0

From what i understood form your question this will give you a scatter plot of actual_y values and a line plot of predicted_y value:-

import matplotlib.pyplot as plt
x = []
plt.plot(predicted_y, linestyle = 'dotted')
for i in range(0,len(acutal_y):
    x.append(i)
plt.scatter(x, actual_y)
plt.show()
Naman
  • 64
  • 9
0

predict for both x_train and x_test by the model, and then try out to draw using sns.regplot() function by import seaborn as sns, for the horizontal x = actual and y_values, vertical y = predicted values, two separated plots for both train and test set, then it would plot scatter for points and even line for its regression which means if slope is equal to 1 and intercept equal to 0 or close to these values, the model would be very well.

for more options it would be better to calculate 'scipy.stats.linregress' for both train and test set to derive the slope and intercept.

Soroush Mirzaei
  • 331
  • 2
  • 12
0

Thanks @Soroush Mirzaei

Thank you so much! .. Your suggestion has solved my problem. May I ask you if is it possible to plot to data sets using sns.regplot() and the best fit curve for these two data sets.

For example: I got two Data sets and two fit lines for each one like the below image [1]: https://i.stack.imgur.com/avGR1.png

Instead, I want to get something two Data sets, the orange and the blue sets, and one fit curve for these two sets..

Below is the code that I am using :

sns.regplot(y_train,y_pred_train, label="Training Data")

plt.xlabel('Actual G*')
plt.ylabel('Predicted G*')
plt.title('Actual vs. Predicted')
plt.legend(loc="upper left")

sns.regplot(y_test,y_pred_test, label="Testing Data")