Simple Linear Regression models the relationship between a single input feature and a continuous output by fitting a straight line through the data. The goal: find the optimal slope and intercept that minimize the prediction error across all data points. Use the controls below to adjust the model parameters and watch the error respond in real-time.
hθ(x) = θ0 + θ1xUse NumPy or Scikit-Learn to fit a line to the provided X and y arrays and print the slope and intercept.
To fit a model using Scikit-Learn, you first need to instantiate the LinearRegression class from sklearn.linear_model. Then, call the .fit() method passing in your features and target arrays.
model = LinearRegression()
model.fit(X, y)
After fitting, the parameters you need are stored in model.coef_ (slope) and model.intercept_ (bias). Print these out to complete the objective.
Note: In the real world, engineers don't calculate the math from scratch every time. They use highly optimized libraries. Here is how you summon a pre-built Machine Learning model.
import)numpy) for handling the numbers, and our machine learning toolbox (sklearn) to give us the actual algorithm.import numpy as np
from sklearn.linear_model import LinearRegression
.reshape(-1, 1) to stand our flat list upright into a single vertical column.X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([3.1, 4.9, 7.2, 8.9, 11.1])
LinearRegression())model = LinearRegression()
.fit())X) and our known answers (y) into the model. The algorithm does all the heavy calculus behind the scenes instantly.model.fit(X, y)
coef_ and intercept_)coef_ (coefficient) is our slope/weight, and intercept_ is our starting bias. Because coef_ is designed to hold multiple slopes for complex 3D math, it returns a list. We use [0] to grab the first and only slope in that list.print(f"Slope: {model.coef_[0]}")
print(f"Intercept: {model.intercept_}")
Waiting for execution...
Calculate the mean of X and Y, the covariance, and the variance to manually derive the weight (w) and bias (b) without external ML libraries.
To manually derive the model without ML libraries, utilize NumPy array operations.
np.mean(array) to find the mean of X and y.w = np.sum((X - X_mean) * (y - y_mean)) / np.sum((X - X_mean)**2)b = y_mean - w * X_meanCalculate the means first, plug them into the slope formula, then solve for the intercept.
w = Σ(X - X_mean) * (y - y_mean) / Σ(X - X_mean)²
Note: Don't let the math scare you. The Greek letter Sigma (Σ) just means "Sum everything up". We will make Python do this for us!
import)numpy (Numerical Python) is the ultimate math toolbox. We import it as np just to save typing.import numpy as np
array)array is simply a neat, organized list of numbers that our math toolbox knows how to read.X = np.array([1, 2, 3, 4, 5])y = np.array([3.1, 4.9, 7.2, 8.9, 11.1])
mean)X_mean = np.mean(X)y_mean = np.mean(y)
Σ(X - X_mean)² calculates this variance!
np.sum() is our Sigma (Σ) symbol!numerator = np.sum((X - X_mean) * (y - y_mean))
denominator = np.sum((X - X_mean)**2)
w = numerator / denominator
b = y_mean - (w * X_mean)
print(f"Slope: {w}")
print(f"Intercept: {b}")
Waiting for execution...