Visualize the model predictions using matplotlib.pyplot.scatter()
In this final step, we will create a visualization to better understand our model's performance. Visualization is crucial in machine learning as it helps us see patterns and relationships that might not be obvious from raw numbers.
We will create a scatter plot that compares the actual house prices (y_test) with our model's predictions. This type of plot is called a "predictions vs actual" scatter plot. If our model were perfect, all points would lie on a diagonal line (45-degree line) where predicted values equal actual values.
We will use matplotlib to create this visualization and save it as an image file.
Append the following code to your main.py file:
import matplotlib.pyplot as plt
## Create a scatter plot comparing actual vs predicted values
plt.figure(figsize=(10, 6))
plt.scatter(y_test, predictions, alpha=0.5, color='blue', edgecolors='black')
## Add a diagonal line showing perfect predictions (where predicted = actual)
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()],
'r--', linewidth=2, label='Perfect Prediction')
## Add labels and title
plt.xlabel('Actual House Prices ($100,000)')
plt.ylabel('Predicted House Prices ($100,000)')
plt.title('Linear Regression: Actual vs Predicted House Prices')
plt.legend()
plt.grid(True, alpha=0.3)
## Save the plot to a file
plt.savefig('housing_predictions.png', dpi=300, bbox_inches='tight')
plt.close()
print("\n--- Visualization Complete ---")
print("Plot saved to housing_predictions.png")
Now, run the complete script from the terminal:
python3 main.py
You will see a confirmation message that the plot has been saved.
--- Visualization Complete ---
Plot saved to housing_predictions.png

Figure 2: Scatter plot showing actual vs predicted house prices. Points closer to the red diagonal line indicate better predictions.
This visualization will help you understand:
- Points near the diagonal line: Good predictions where the model was accurate
- Points far from the diagonal line: Poor predictions where the model made larger errors
- Overall pattern: Whether the model tends to over-predict or under-predict certain price ranges
You can double-click the housing_predictions.png file in the file explorer to view your visualization.
Congratulations! You have successfully built, trained, tested, and visualized a linear regression model with scikit-learn.