Machine Learning (ML) is transforming industries by enabling systems to learn from data and make intelligent predictions. In this post, we will walk through a beginner-friendly example of implementing machine learning in Python using linear regression โ one of the simplest yet powerful ML algorithms.
We’ll build a predictive model that estimates a student’s exam score based on the number of hours they studied. This use case is ideal for beginners who are starting their machine learning journey with Python.
๐งพ Objective
To implement a linear regression model using Python and scikit-learn that can predict student performance based on study hours.
๐งฎ The Python Code
from sklearn.linear_model import LinearRegression
import numpy as np
# Sample dataset: hours studied vs scores obtained
hours = np.array([[1], [2], [3], [4], [5]])
scores = np.array([20, 40, 50, 65, 80])
# Initialize and train the model
model = LinearRegression()
model.fit(hours, scores)
# Make prediction based on user input
h = float(input('Enter hours studied: '))
prediction = model.predict([[h]])
print(prediction)
๐ Understanding the Code
Importing Libraries
We use numpy
for data handling and LinearRegression
from sklearn.linear_model
to create and train our model.
Training Data
A small dataset of hours studied vs scores is used to train the model.
Model Fitting
The .fit()
method teaches the model the relationship between input (hours) and output (scores).
Prediction
The trained model predicts a score for the number of hours entered by the user.
๐ฏ Sample Output
Enter hours studied: 3.5
[57.5]
๐ค Why Use Linear Regression in Machine Learning?
Linear regression is a supervised learning algorithm used for predicting a continuous value. It is widely used in business, healthcare, education, and more to model relationships between variables and forecast outcomes.
In our example:
- Input (X): Hours studied
- Output (Y): Marks scored
- The model tries to fit the best straight line (y = mx + c) that maps X to Y.
๐ก What You Can Do Next
- Visualize the data and regression line using
matplotlib
- Collect real student performance data for better accuracy
- Deploy this model as a web app using Flask or Streamlit