简体   繁体   English

我如何 plot 神经网络的实际值与预测值?

[英]How I can plot the actual vs predicted values for the neural network?

I am trying to plot the actual vs predicted values of my neural network which I created using keras.我正在尝试 plot 我使用 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

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:-根据我对您问题的理解,这将为您提供实际_y 值的分散 plot 和predicted_y _y 值的actual_y线:-

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()

predict for both x_train and x_test by the model, and then try out to draw sns.regplot() by import seaborn as sns , for the horizontal x = actual 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.通过 model 预测 x_train 和 x_test,然后尝试通过import seaborn as sns来绘制sns.regplot() ,对于水平 x = 实际 y_values,垂直 y = 预测值,训练和测试集的两个分离图,那么它会 plot 分散点,甚至线进行回归,这意味着如果斜率等于 1 并且截距等于 0 或接近这些值,则 model 会非常好。

for more options it would be better to calculate 'scipy.stats.linregress' for both train and test set to derive the slope and intercept.对于更多选项,最好为训练集和测试集计算“scipy.stats.linregress”以得出斜率和截距。

Thanks @Soroush Mirzaei谢谢@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.请问您是否可以使用sns.regplot()对数据集进行 plot 以及这两个数据集的最佳拟合曲线。

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例如:我有两个数据集和两条拟合线,如下图 [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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 seaborn 绘制实际值与预测值的散点图 - Plot scatter with actual vs predicted values with seaborn 如何 plot 预测值与真实值? - How to plot predicted values vs the true value? 如何检查受过训练的神经网络的预测输出 - How to check the predicted output of a trained Neural Network 当我们有多索引时如何用实际值绘制预测值 - How to plot predicted values with actual values when we have multi-index 如何确定由 Keras 上的卷积神经网络预测的二元类? - How do I determine the binary class predicted by a convolutional neural network on Keras? 我的实际和预测的原始数据集有超过 5000 多个值我只想 plot 150 个值 - My orginal data set of actual and predicted has over 5000+ values i want to only plot 150 values 如何在 python 中找到分类神经网络的预测 output? - How to find the predicted output of a classification neural network in python? 如何在python中绘制预期值与实际值的图? - How to plot a graph of expected vs actual values in python? 如何 plot python 中的实际值与预测值的图表? - How to plot a graph of actual vs predict values in python? 实施人工神经网络时,如何减少训练值中的误差? - How can I reduce the error in my trained values while implementing Artificial Neural Network?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM