Linear Regression: A Beginner's Guide with Python

2026-12-01来源 dev.toEnglish

1. Introduction

In Data Science, predicting future outcomes using existing data is an important task. Businesses may want to estimate sales, banks may need to understand financial trends, and educators may want to study student performance.

Machine Learning provides techniques that help us make these predictions.

One of the simplest and most widely used techniques is Linear Regression. It is a useful starting point for understanding machine learning because its predictions and relationships are relatively easy to interpret.

2. What is Linear Regression?

Linear Regression is a supervised machine learning algorithm used to predict a continuous numerical value.

It identifies the relationship between one or more input variables and an output variable.

Example:

A model can study the relationship between the number of hours a student studies and the marks they receive. It can then estimate the marks of another student based on their study hours.

3. How Does Linear Regression Work?

Linear Regression finds a line that best represents the relationship between the data points.

It compares actual values with predicted values and tries to minimise the differences, commonly using the Least Squares Method.

Mathematical Equation

Y = mX + C

Where:

  • Y: Predicted output
  • X: Input variable
  • m: Slope of the line
  • C: Intercept (predicted value when X is zero)

Basic Working Steps

  1. Collect the input data.
  2. Identify the relationship between variables.
  3. Fit the best possible line through the data points.
  4. Train the model.
  5. Use the trained model to make predictions.

Example: As study hours increase, student marks may also increase. Linear Regression learns this general trend.

4. Types of Linear Regression

A. Simple Linear Regression

Uses one independent variable to predict an output.

Example: Predicting student marks using study hours.

B. Multiple Linear Regression

Uses two or more independent variables to predict an output.

Example: Predicting house prices using area, age and number of rooms.

5. Python Implementation

We can implement Simple Linear Regression in Python using the Scikit-learn library.

In this example, we will predict student marks based on study hours.

Step 1: Install Required Libraries

pip install numpy scikit-learn matplotlib 

Step 2: Python Program

import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression # Input: hours studied X = np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1) # Output: marks obtained y = np.array([35, 40, 50, 55, 65, 70]) # Create and train the model model = LinearRegression() model.fit(X, y) # Predict marks for 7 study hours prediction = model.predict([[7]]) print("Predicted marks:", round(prediction[0], 2)) # Draw the data and regression line plt.scatter(X, y, color="blue", label="Actual marks") plt.plot(X, model.predict(X), color="red", label="Regression line") plt.xlabel("Study Hours") plt.ylabel("Marks") plt.title("Linear Regression: Study Hours vs Marks") plt.legend() plt.show() 

Step 3: Sample Output

Predicted marks: 77.0 

Output Explanation:

The model predicts that a student who studies for 7 hours may score approximately 77 marks based on the sample dataset.

The graph displays:

  • Blue points: Actual student marks.
  • Red line: Predicted regression line.

6. Real-Life Applications

Linear Regression is used in many fields:

  1. Predicting house prices using area and location.
  2. Estimating student marks based on study hours.
  3. Forecasting sales and business revenue.
  4. Estimating product demand.
  5. Analysing trends and relationships between variables.

7. Advantages of Linear Regression

  • Easy to understand and implement.
  • Requires relatively low computational power.
  • Helps explain relationships between variables.
  • Provides an interpretable baseline for prediction tasks.

8. Limitations of Linear Regression

  • Works best when relationships are approximately linear.
  • Can be affected by outliers.
  • May not capture complex patterns.
  • Its assumptions should be checked before relying on predictions.

9. Conclusion

Linear Regression is an important starting point for understanding Machine Learning.

Although it is a basic algorithm, it is useful in many industries for prediction and data analysis.

Learning Linear Regression helps students understand how a machine learning model identifies patterns in data and uses them to estimate numerical outcomes.

10. References and Further Reading

  1. Scikit-learn – Linear Models
  2. Google – Linear Regression
  3. An Introduction to Statistical Learning

Created for learning and educational purposes.

Topic: Linear Regression in Machine Learning

原文:https://www.a8q9.cn/en/article/linear-regression-a-beginner-s-guide-with-python-67f847.html

相关文章