Implementing Linear Regression with scikit-learn and Comparing to Gradient Descent

This article demonstrates how to build a multivariate linear regression model using scikit-learn's LinearRegression class, visualizes the results, and then compares the implementation, parameter optimization, prediction accuracy, and visualization with a manually coded gradient descent approach.

YiSu Grain
YiSu Grain
YiSu Grain
Implementing Linear Regression with scikit-learn and Comparing to Gradient Descent

Multivariate Linear Regression with scikit‑learn

This guide demonstrates a complete workflow for building a multivariate linear regression model using sklearn.linear_model.LinearRegression. It covers importing libraries, loading data from Delivery.csv, training the model, making a prediction, visualizing the fitted plane, and comparing the approach with a manual gradient‑descent implementation.

Import required libraries

import numpy as np
from numpy import genfromtxt
from sklearn import linear_model
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

Load and preprocess the dataset

data = genfromtxt(r"Delivery.csv", delimiter=",")
print(data)
# Features: mileage and number of deliveries
x_data = data[:, :-1]
# Target: delivery time
y_data = data[:, -1]
print(x_data)
print(y_data)

The CSV file contains two feature columns – Miles and Number of Deliveries – and a target column Delivery Time . x_data holds the features, y_data holds the target.

Create and train the LinearRegression model

model = linear_model.LinearRegression()
model.fit(x_data, y_data)
print("coefficients:", model.coef_)
print("intercept:", model.intercept_)

The fit call computes the optimal coefficients and intercept using the ordinary‑least‑squares closed‑form solution.

Make a prediction

x_test = [[102, 4]]  # mileage = 102, deliveries = 4
predict = model.predict(x_test)
print("predict:", predict)

The model returns the estimated delivery time for the test input.

Visualize the results in 3‑D

ax = plt.figure().add_subplot(111, projection='3d')
ax.scatter(x_data[:, 0], x_data[:, 1], y_data, c='r', marker='o', s=100)
# Build a meshgrid for the regression plane
x0 = x_data[:, 0]
x1 = x_data[:, 1]
x0, x1 = np.meshgrid(x0, x1)
z = model.intercept_ + x0 * model.coef_[0] + x1 * model.coef_[1]
ax.plot_surface(x0, x1, z)
ax.set_xlabel('Miles')
ax.set_ylabel('Num of Deliveries')
ax.set_zlabel('Time')
plt.show()

The red points represent the original observations; the surface shows the regression plane derived from the learned parameters.

Comparison with a manual gradient‑descent implementation

Implementation differences

Gradient descent: manually computes the loss gradient, updates parameters iteratively, and requires explicit learning‑rate ( lr) and epoch ( epochs) settings.

scikit‑learn LinearRegression: solves the ordinary least‑squares problem directly with a single fit call; no learning‑rate or epoch configuration needed.

Parameter optimization

Gradient descent arrives at the final parameters after many iterations, each requiring a loss computation.

LinearRegression computes the optimal parameters analytically, which is faster for typical dataset sizes.

Prediction results

Both methods aim to fit the same linear model, so their final predictions are expected to be identical or very close.

Visualization

Both approaches produce similar 3‑D scatter plots and regression planes, illustrating comparable model performance.

Key takeaways

Use cases : Gradient descent is useful for educational purposes or very small datasets where manual control of the optimization process is desired. For larger datasets and production scenarios, scikit‑learn’s LinearRegression offers higher efficiency and simplicity.

Accuracy and robustness : The two methods yield comparable results; the library implementation is generally more robust and faster.

Visualization and model interpretation : Either method can be visualized with 3‑D plots to explore the relationship between features and the target.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythongradient descentvisualizationscikit-learnmultivariate regressionLinearRegression
YiSu Grain
Written by

YiSu Grain

A fleeting mayfly in the world, a single grain in the boundless sea.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.