Menu

Understanding Regression Algorithms

Understanding Regression Algorithms

Regression is a supervised learning technique used to predict continuous numerical values, such as prices, revenue, temperature, or delivery time.

Linear Regression

Linear regression models the relationship between input features and a numerical target.

For a single feature, the model can be expressed as:

[
y = mx + b
]

Here, (x) is the input, (y) is the predicted output, (m) is the slope, and (b) is the intercept.

The model estimates the parameters that best fit the training observations according to its optimization objective.

import numpy as np

from sklearn.linear_model import LinearRegression

X = np.array([[1], [2], [3], [4], [5]])

y = np.array([30, 40, 50, 60, 70])

model = LinearRegression()

model.fit(X, y)

predicted_score = model.predict([[6]])

print("Predicted score:", predicted_score[0])

print("Slope:", model.coef_[0])

print("Intercept:", model.intercept_)

In this example, the model learns a linear relationship between the input and output. It then uses that relationship to predict the output for an input of 6.

Multiple Linear Regression

When several features contribute to a prediction, multiple linear regression can be used.

import pandas as pd

from sklearn.linear_model import LinearRegression

df = pd.DataFrame({

"Area": [800, 1000, 1200, 1500, 1800, 2000],

"Bedrooms": [1, 2, 2, 3, 3, 4],

"Price": [2500000, 3200000, 3800000, 5000000, 6000000, 7000000]

})

X = df[["Area", "Bedrooms"]]

y = df["Price"]

model = LinearRegression()

model.fit(X, y)

new_house = pd.DataFrame({

"Area": [1600],

"Bedrooms": [3]

})

print("Predicted price:", model.predict(new_house)[0])

The model uses both area and bedroom count to estimate the price. In practical applications, model quality depends on factors such as dataset size, feature quality, and whether the relationship is sufficiently represented by a linear model.

When Should Regression Be Used?

Regression is appropriate when the target is numerical and the goal is to estimate a quantity. More complex algorithms may be necessary when relationships are nonlinear or when the data contains complicated interactions between features.


Python for Data Science

VA

Vishalini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details