Linear Regression with Python – Predict Values Using Machine Learning
Published: 15 Oct, 2026 | Author: CCI Admin | Category: Machine Learning / Python
Machine Learning helps computers learn patterns from data and make predictions. One of the simplest and most widely used Machine Learning algorithms is Linear Regression. It is used to predict numerical values such as house prices, sales, and monthly expenses.
In this guide from CCI Computer Education, Pudukkottai, you will learn the basic concept of Linear Regression and build a simple prediction model using Python.
1. What Is Linear Regression?
Linear Regression is a supervised Machine Learning algorithm that learns the relationship between input data and a numerical output. It finds a line that best represents the relationship between the variables in the training data.
For simple linear regression, the equation is:
y = mx + b
- x: Input variable or feature
- y: Predicted output
- m: Slope of the line
- b: Intercept of the line
For example, a model can learn how house size relates to house price and use that relationship to estimate the price of another house.
2. How Does Linear Regression Work?
Linear Regression follows a simple process:
- Collect historical data.
- Choose the input features and numerical target.
- Train the model using the available data.
- Allow the model to learn the relationship between inputs and outputs.
- Use the trained model to predict values for new inputs.
- Evaluate predictions against actual values to understand model performance.
3. Install the Required Python Library
We will use scikit-learn, a popular Python library for Machine Learning.
pip install scikit-learn
Run this command in your terminal or command prompt. If you are using a virtual environment, activate it before installing the library.
4. Simple Linear Regression Example in Python
Let us create a model that learns the relationship between hours studied and exam marks using a small sample dataset.
from sklearn.linear_model import LinearRegression
# Training data
hours = [[1], [2], [3], [4], [5]]
marks = [20, 40, 60, 80, 100]
# Create the model
model = LinearRegression()
# Train the model
model.fit(hours, marks)
# Predict marks for 6 hours of study
prediction = model.predict([[6]])
print("Predicted Marks:", prediction[0])
Output:
Predicted Marks: 120.0
In this sample, the data follows a perfect linear relationship, so the model predicts 120 for six hours. In real life, exam marks have limits and may not follow a perfect straight-line relationship. This example is intended to demonstrate how the algorithm works.
5. Understanding the Python Code
| Code | Purpose |
|---|---|
LinearRegression() |
Creates the Linear Regression model. |
model.fit() |
Trains the model using the input and target data. |
model.predict() |
Predicts an output for new input data. |
model.coef_ |
Returns the learned coefficient or slope for each feature. |
model.intercept_ |
Returns the learned intercept. |
6. Real-World Example: House Price Prediction
Imagine that a real estate company wants to estimate house prices based on their size. We can train a simple Linear Regression model using sample house-size and price data.
In this example, house sizes are measured in square feet and prices are represented in lakhs of rupees. The values are illustrative training data, not actual market prices.
from sklearn.linear_model import LinearRegression
# House sizes in square feet
sizes = [[500], [800], [1000], [1200], [1500]]
# Sample prices in lakhs
prices = [15, 24, 30, 36, 45]
# Create and train the model
model = LinearRegression()
model.fit(sizes, prices)
# Predict price for a 1100 sq. ft. house
predicted_price = model.predict([[1100]])
print("Estimated Price:", predicted_price[0], "lakhs")
Output:
Estimated Price: 33.0 lakhs
The model learns a relationship from the sample data and estimates the price of a house measuring 1,100 square feet. Actual prices depend on location, building condition, amenities, and many other factors, so a real application needs suitable data and careful evaluation.
7. Simple Linear Regression vs Multiple Linear Regression
| Feature | Simple Linear Regression | Multiple Linear Regression |
|---|---|---|
| Input Features | One | Two or more |
| Example | House price based on size | House price based on size, location features, and age |
| Complexity | Usually simpler to understand | Uses several input variables |
8. Advantages of Linear Regression
- Easy to understand and implement.
- Useful for predicting numerical values.
- Helps identify linear relationships between variables.
- Provides interpretable coefficients.
- Works well as a baseline model for many prediction problems.
9. Limitations of Linear Regression
- It may perform poorly when the relationship is strongly non-linear.
- Outliers can influence the fitted line.
- Poor-quality or insufficient training data can lead to unreliable predictions.
- A model trained on a small sample may not generalize well to new data.
10. Faculty Tip
At CCI Computer Education, students can understand Linear Regression by collecting a small dataset, training the model, and predicting a new value. Encourage them to change the training data and compare how the prediction changes.
11. Practice Task for Students
- Create a dataset containing hours studied and marks scored.
- Train a Linear Regression model using scikit-learn.
- Predict marks for a new number of study hours.
- Create a sample dataset of house sizes and prices and make a prediction.
- Print
model.coef_andmodel.intercept_to understand the fitted equation.
12. Frequently Asked Questions (FAQs)
1. What is Linear Regression in Machine Learning?
Linear Regression is a supervised learning algorithm that models the relationship between input features and a numerical target using a linear equation.
2. Which Python library is used for Linear Regression?
Scikit-learn provides the LinearRegression class for building Linear Regression models in Python.
3. Is Linear Regression used for classification?
Linear Regression is primarily used to predict continuous numerical values. Classification tasks generally use algorithms designed for predicting categories.
4. What does model.fit() do?
The fit() method learns the model parameters from the training data.
5. What does model.predict() do?
The predict() method uses the trained model to estimate outputs for new input data.
Conclusion
Linear Regression is a useful starting point for learning Machine Learning with Python. It teaches how a model learns from examples and uses the learned relationship to predict numerical values.
Practise the examples, experiment with different datasets, and continue building your Machine Learning skills with CCI Computer Education, Pudukkottai.
What to Learn Next
- Logistic Regression with Python
- Train-Test Split in Machine Learning
- Model Evaluation Using Mean Squared Error
Related CCI Blogs
- Machine Learning Explained with Real-World Examples
- Supervised vs Unsupervised Learning
- How a Loan Approval Prediction System Works
Learn Python and Machine Learning with CCI Computer Education
Want to learn Python programming and Machine Learning through practical examples and projects? Join CCI Computer Education, Pudukkottai, and develop skills for your technology career.
✨✨Start your AI&ML journey with CCI
👆Click here
Call: 98427 97945
Website: www.ccipudukkottai.comWhatsApp Channel: Follow CCI Computer Education
CCI Computer Education – Learn Today, Build Tomorrow.