Presentation of Results
Get some hands-on experience on how to present and analyze results in Python.
Results analysis
Visualization of results helps us understand and interpret the models’ performance. We can use Python resources, such as the seaborn and Matplotlib libraries, for this purpose. Scatterplots are good for visualizing relationships and checking for patterns. We can use them to see how our models predict the outcome based on a single variable of the data. Additionally, we’ll create a best-fit line on each scatterplot, with the x-axis representing the actual tip amount and the y-axis representing the predicted amounts.
We’ll analyze the results by generating scatterplots that utilize two variables from the tips
dataset: the time
variable and the sex
variable. Additionally, we’ll produce a histogram to directly compare predictions with the actual data.
Remember: When we execute any of the code below again, we might have different results because the models will be trained again from scratch.
Based on the time
variable
Because this column only had Lunch
or Dinner
as options, we’ll see how the predicted values align with the original values for each option.
#Import the necessary librariesimport matplotlib.pyplot as pltimport seaborn as sns# Flatten the neural network predictions to make them compatible with the plotting functionnn_predictions = nn_predictions.flatten()time_mapping = {1: 'Lunch', 0: 'Dinner'}# Combine the actual and predicted tip values with the 'time' feature for each modellr_df = pd.DataFrame({'Actual Tip': y_test, 'Predicted Tip (LR)': lr_predictions, 'Time': X_test['time_Lunch'].map(time_mapping)})rf_df = pd.DataFrame({'Actual Tip': y_test, 'Predicted Tip (RF)': rf_predictions, 'Time': X_test['time_Lunch'].map(time_mapping)})nn_df = pd.DataFrame({'Actual Tip': y_test, 'Predicted Tip (NN)': nn_predictions, 'Time': X_test['time_Lunch'].map(time_mapping)})# Create separate scatterplots for each modelsns.lmplot(x='Actual Tip', y='Predicted Tip (LR)', data=lr_df, hue='Time', markers=['o', 'x'])plt.title('Linear Regression Predictions')plt.xlabel('Actual Tip')plt.ylabel('Predicted Tip')plt.savefig("output/plot2.png", bbox_inches='tight')sns.lmplot(x='Actual Tip', y='Predicted Tip (RF)', data=rf_df, hue='Time', markers=['o', 'x'])plt.title('Random Forest Predictions')plt.xlabel('Actual Tip')plt.ylabel('Predicted Tip')plt.savefig("output/plot1.png", bbox_inches='tight')sns.lmplot(x='Actual Tip', y='Predicted Tip (NN)', data=nn_df, hue='Time', markers=['o', 'x'])plt.title('Neural Network Predictions')plt.xlabel('Actual Tip')plt.ylabel('Predicted Tip')plt.savefig("output/plot.png", bbox_inches='tight')
Code explanation
Lines 2–3: We import the necessary libraries.
Line 6: We flatten the neural network predictions to ensure compatibility with the plotting function.
Line 8: We create a mapping dictionary to convert binary values to
Lunch
andDinner
for thetime
variable.Lines 11–13: We create DataFrames combining the actual and predicted tip values for ...