Visualize a Machine Learning Algorithm using Python

A machine learning algorithm is used to find relationships between features and labels. Features are the independent variables that we feed into an algorithm to train a machine learning model, and labels are the dependent variables that we aim to predict using the machine learning algorithm. If you have trained a machine learning model before but have never visualized a machine learning algorithm, then this article is for you. In this article, I will take you through how to visualize a machine learning algorithm using Python.

Visualize a Machine Learning Algorithm using Python

Visualizing a machine learning algorithm means visualizing a trendline of the predicted values by the machine learning algorithm. What happens is that when we train a machine learning model using an algorithm, we feed the data into the algorithm, the algorithm finds the relationship between features and labels. Features are independent variables that we use to train the model, and labels are dependent variables that we aim to predict.

So when we visualize a machine learning algorithm, it shows the trendline of the values of the predicted labels according to the features. Now, I will show you how to train a simple machine learning algorithm and visualize it using Python. Here I will first train a linear regression model to predict the sales of a product based on the amount spent on its TV advertising and, then I will visualize the predictions of the linear regression algorithm by using the plotly library in Python:

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

data = pd.read_csv("https://raw.githubusercontent.com/amankharwal/Website-data/master/Advertising.csv")
print(data.head())
x = data["TV"].values.reshape(-1, 1)
y = data["Sales"]

model = LinearRegression()
model.fit(x, y)
x_range = np.linspace(x.min(), x.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))

import plotly.express as px
import plotly.graph_objects as go
fig = px.scatter(data, x='TV', y='Sales', opacity=0.65)
fig.add_traces(go.Scatter(x=x_range, y=y_range, 
                          name='Linear Regression'))
fig.show()
Visualize a Machine Learning Algorithm

In the above figure, the red line is a trendline that shows the predictions made by the linear regression algorithm, which is nothing but the relationship between the amount spent on advertising on television and the sales of the product.

Below is how you can have a look at all the predicted values by the machine learning algorithm:

print(y_range)
[ 7.0658692   7.2078549   7.3498406   7.49182631  7.63381201  7.77579771
  7.91778341  8.05976912  8.20175482  8.34374052  8.48572623  8.62771193
  8.76969763  8.91168333  9.05366904  9.19565474  9.33764044  9.47962614
  9.62161185  9.76359755  9.90558325 10.04756896 10.18955466 10.33154036
 10.47352606 10.61551177 10.75749747 10.89948317 11.04146888 11.18345458
 11.32544028 11.46742598 11.60941169 11.75139739 11.89338309 12.0353688
 12.1773545  12.3193402  12.4613259  12.60331161 12.74529731 12.88728301
 13.02926871 13.17125442 13.31324012 13.45522582 13.59721153 13.73919723
 13.88118293 14.02316863 14.16515434 14.30714004 14.44912574 14.59111145
 14.73309715 14.87508285 15.01706855 15.15905426 15.30103996 15.44302566
 15.58501136 15.72699707 15.86898277 16.01096847 16.15295418 16.29493988
 16.43692558 16.57891128 16.72089699 16.86288269 17.00486839 17.1468541
 17.2888398  17.4308255  17.5728112  17.71479691 17.85678261 17.99876831
 18.14075401 18.28273972 18.42472542 18.56671112 18.70869683 18.85068253
 18.99266823 19.13465393 19.27663964 19.41862534 19.56061104 19.70259675
 19.84458245 19.98656815 20.12855385 20.27053956 20.41252526 20.55451096
 20.69649667 20.83848237 20.98046807 21.12245377]

Summary

A machine learning algorithm is used to find relationships between features and labels. Visualizing a machine learning algorithm means visualizing a trendline of the predicted values by the machine learning algorithm. I hope you liked this article on how to visualize a machine learning algorithm using Python. Feel free to ask valuable questions in the comments section below.

Default image
Aman Kharwal
Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder
Articles: 1207

Leave a Reply