Visualizing Predicted vs Actual

tags: #ML/supervised/regression

To visualize the predicted values against the expected values, we can first create a pd.DataFrame() containing columns for the expected (y_test) and the predicted values (y_pred):

# create new dataframe
df = pd.DataFrame()

# add expected column
df["Expected"]=pd.Series(y_test)

# add predicted column
df["Predicted"]=pd.Series(y_pred)

To visualize - plot the data as a scatter plot with the expected (target) values along the x-axis and the predicted values along the y-axis:

# create instance of a figure
fig = plt.figure(figzize=(9,9))

# create instance of the plot itself
axes = sns.scatterplot(data=df, x='Expected', y='Predicted', hue='Predicted', palette = 'cool', legend=False)

# set axes
start = min(y_test.min(), y_pred.min())
end = min(y_test.max(), y_pred.max())

axes.set_xlim(start, end)
axes.set_ylim(start, end)

# plot line
line = plt.plot([start, end], [start, end], 'k--') # pass a list of [x,y] cordinates
Powered by Forestry.md