Lesson 3

In this lesson, we explore how to make predictions using a trained machine learning model and visualize the results. Understanding predictions and visualizations helps interpret model performance and make informed decisions.

Our goal is to build on the trained **linear regression** model from the last lesson, use it to predict, and visualize these predictions against actual data. This will provide a clear picture of your model's performance.

First, let's recap. In the last lesson, we trained a linear regression model to understand the relationship between the area of a house and its price using synthetic data. We will use the same code snippet for generating the data as we used in the previous lesson:

Python`1import numpy as np 2import pandas as pd 3 4np.random.seed(42) 5num_samples = 100 6area = np.random.uniform(500, 3500, num_samples) # House area in square feet 7 8# Assume a linear relationship: price = base_price + (area * price_per_sqft) 9base_price = 50000.00 10price_per_sqft = 200.00 11noise = np.random.normal(0, 25000, num_samples) # Adding noise 12price = base_price + (area * price_per_sqft) + noise 13 14# Create DataFrame 15df = pd.DataFrame({'Area': np.round(area, 2), 'Price': np.round(price, 2)}) 16X = df[['Area']] 17y = df['Price']`

Now, imagine you have new data, like areas of new houses, and want to predict their prices using your trained model. This is where the `predict`

method from `Scikit-Learn`

comes into play.

The `predict`

method takes input data and generates the predicted output based on the model.

We'll start by importing essential libraries, including `NumPy`

, `Pandas`

, `Matplotlib`

, and `Scikit-Learn`

. We'll use the same data generation script as in the previous lesson.

First, we initialize and train the model using our data.

Python`1from sklearn.linear_model import LinearRegression 2 3model = LinearRegression() 4model.fit(X, y)`

Then, let's make predictions with our trained model. Suppose you want to predict prices for houses with areas of 200 and 3500 square feet.

Python`1X_new = np.array([[200.00], [3500.00]]) 2# Converting to a DataFrame to include feature name 3X_new = pd.DataFrame(X_new, columns=['Area']) 4y_predict = model.predict(X_new) 5print("Predicted prices:", np.round(y_predict, 2)) # [ 96526.95 743883.09]`

In real life, you might need to predict prices for several new houses, not just two.

**Visualizing** your predictions against actual data helps to understand your model's performance. We'll use `Matplotlib`

to create a scatter plot for the actual data and a line plot for predicted data.

A scatter plot is ideal for visualizing individual data points, perfect for plotting actual house prices against their areas.

Python`1import matplotlib.pyplot as plt 2 3plt.scatter(X, y.round(), color='blue', alpha=0.5) 4plt.xlabel('Area (sq ft)') 5plt.ylabel('Price ($)') 6plt.title('House Prices vs. Area') 7plt.grid() 8plt.show()`

Next, we'll draw a line plot to visualize the predictions. This line shows how the predicted prices vary with house areas.

Python`1plt.scatter(X, y, color='blue') # Plot actual data 2plt.plot(X_new, np.round(y_predict, 2), color='red', linewidth=2) # Plot predicted line 3plt.xlabel('Area (sq ft)') 4plt.ylabel('Price ($)') 5plt.title('Linear Regression: Area vs. Price') 6plt.grid() 7plt.show()`

In this plot:

- Blue points represent actual data.
- The red line represents the model's predictions.

The closer the points are to the red line, the better your model's predictions. In this case, we can see that the line and the points are well-aligned. In reality, data could me much noisier.

Congratulations! You've learned how to make predictions using a trained machine-learning model and visualize results. In this lesson, we covered:

- Generating and preparing data.
- Using the
`predict`

method to generate new predictions. - Visualizing actual data and predicted results using
`Matplotlib`

.

Visualizing predictions helps you understand and evaluate your model's performance. Good models have predicted values close to the actual values, making the plot look smoother.

Now, it's time to use what you've learned in practice. In the practice session, you'll generate your predictions and visualize them. This hands-on experience will reinforce your skills and make you more comfortable with making predictions and visualizing results. Happy coding!