DEV Community

Nazim Shaikh
Nazim Shaikh

Posted on Edited on

LINEAR REGRESSION

INTRODUCTION

Linear Regression is one of the most popular supervised machine learning algorithms. It is used to predict numerical values by identifying relationships between independent and dependent variables.
For example, it can predict a student's marks based on the number of hours they study.

WHAT IS LINEAR REGRESSION?

Linear Regression is a statistical and machine learning technique that finds the best-fitting straight line through a dataset to predict continuous numerical values.

MATHEMATICAL FORMULA

y = mx + c

Where:

  • y = Predicted output
  • x = Input variable
  • m = Slope
  • c = Intercept

HOW DOES IT WORK?

  1. Collect the required dataset.
  2. Identify input and output variables.
  3. Analyse the relationship between variables.
  4. Find the best-fitting line.
  5. Use the model to predict new values.

PYTHON IMPLEMENTATION

Install the required libraries:

pip install numpy matplotlib scikit-learn
Enter fullscreen mode Exit fullscreen mode

Python Code:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Sample dataset
hours = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
marks = np.array([20, 30, 40, 50, 60])

# Create and train model
model = LinearRegression()
model.fit(hours, marks)

# Predict marks
prediction = model.predict([[6]])

print("Predicted Marks:", prediction[0])

# Visualize results
plt.scatter(hours, marks)
plt.plot(hours, model.predict(hours))
plt.xlabel("Study Hours")
plt.ylabel("Marks")
plt.title("Linear Regression")
plt.show()
Enter fullscreen mode Exit fullscreen mode

OUTPUT

Predicted Marks: 70.0

APPLICATIONS

  1. House price prediction.
  2. Sales forecasting.
  3. Salary estimation.
  4. Student performance prediction.
  5. Business analysis.

ADVANTAGES

  • Easy to understand and implement.
  • Requires less computational power.
  • Useful for numerical predictions.
  • Helps identify relationships between variables.

LIMITATIONS

  • Works best with approximately linear relationships.
  • Sensitive to outliers.
  • May not perform well with complex data patterns.

CONCLUSION

Linear Regression is an important machine learning algorithm used for predicting numerical values. It helps analyse relationships between variables and supports data-driven decisions in education, business, finance and many other fields.

Top comments (0)