Car Price Prediction Using Machine Learning
1. Introduction
Car Price Prediction is a beginner-friendly Machine Learning project where the goal is to predict the selling price of a car based on different characteristics of the vehicle.
The price of a used car can depend on many factors. Some important factors include the age of the car, engine size, total mileage, and the number of previous owners. Machine Learning can analyze historical car information and learn the relationship between these features and the selling price.
This is a Supervised Machine Learning problem because the model is trained using historical car data where the actual selling prices are already known.
The target variable in this project is Price, which is a continuous numerical value. Therefore, this is a Regression problem.
In this project, we use Linear Regression, a popular and beginner-friendly regression algorithm for predicting continuous numerical values.
2. Objective
The main objective of this project is to build a Machine Learning model that learns the relationship between car features and selling price, then uses the learned relationship to predict the price of a new car.
The project demonstrates important Machine Learning concepts such as:
Creating a car dataset
Selecting input features
Selecting a continuous target variable
Regression
Linear Regression
Model training
Making predictions
Interpreting numerical results
3. Features Used
The model uses four input features.
Car Age
Car_Age represents the age of the car in years. Generally, older cars may have a lower market value compared with newer vehicles.
Engine Size
Engine_Size represents the approximate engine capacity in cubic centimeters (cc). Engine size can be associated with the characteristics and value of a vehicle.
Mileage
Mileage represents the total distance traveled by the car in kilometers. Cars with higher mileage may have lower resale prices.
Number of Owners
Owners represents the number of previous owners of the car. The number of previous owners can be considered as one of the factors when estimating a used car's value.
The target variable is:
Price – The estimated selling price of the car.
4. Technologies Used
The following technologies are used:
Python – Main programming language.
Pandas – Used to create and manage the car dataset.
Scikit-learn – Used to implement the Machine Learning algorithm.
Linear Regression – Used to predict the car's selling price.
5. Dataset
The project uses a small sample dataset containing seven cars.
Each record contains:
Car age
Engine size
Mileage
Number of previous owners
Selling price
Dataset
Car_Age Engine_Size Mileage Owners Price
0 1 1500 10000 1 25000
1 2 1600 20000 1 23000
2 3 1800 30000 2 21000
3 5 2000 50000 2 17000
4 6 2200 60000 3 15000
5 8 2500 80000 3 12000
6 10 3000 100000 4 9000
The model learns patterns from these historical car records.
6. Working Principle
The Car Price Prediction system follows a basic supervised Machine Learning workflow.
First, a sample car dataset is created using Python and Pandas. The dataset contains car characteristics along with their known selling prices.
Next, the four input features—Car_Age, Engine_Size, Mileage, and Owners—are selected and stored in X.
The Price column is selected as the target variable and stored in y.
A Linear Regression model is then created using Scikit-learn.
The model is trained using the historical car data. During training, the algorithm learns the relationship between the car features and its price.
After training, information about a new car is provided to the model. The trained model uses the learned relationship to estimate the selling price of that car.
7. Python Implementation
The following Python program implements the Car Price Prediction system.
# Import required libraries
import pandas as pd
from sklearn.linear_model import LinearRegression
# --------------------------------------------
# Step 1: Create the dataset
# --------------------------------------------
# Sample car data
data = {
"Car_Age": [1, 2, 3, 5, 6, 8, 10],
"Engine_Size": [1500, 1600, 1800, 2000, 2200, 2500, 3000],
"Mileage": [10000, 20000, 30000, 50000, 60000, 80000, 100000],
"Owners": [1, 1, 2, 2, 3, 3, 4],
"Price": [25000, 23000, 21000, 17000, 15000, 12000, 9000]
}
# Convert dictionary into DataFrame
df = pd.DataFrame(data)
# Display dataset
print("Dataset:")
print(df)
# --------------------------------------------
# Step 2: Select input features (X)
# --------------------------------------------
X = df[
[
"Car_Age",
"Engine_Size",
"Mileage",
"Owners"
]
]
# --------------------------------------------
# Step 3: Select target variable (y)
# --------------------------------------------
y = df["Price"]
# --------------------------------------------
# Step 4: Create Linear Regression model
# --------------------------------------------
model = LinearRegression()
# --------------------------------------------
# Step 5: Train the model
# --------------------------------------------
model.fit(X, y)
# --------------------------------------------
# Step 6: Predict car price
# --------------------------------------------
# New car details:
# Age = 4 years
# Engine Size = 1900cc
# Mileage = 40000 km
# Owners = 2
new_car = [[4, 1900, 40000, 2]]
predicted_price = model.predict(new_car)
# --------------------------------------------
# Step 7: Display prediction
# --------------------------------------------
print("\nPredicted Car Price:")
print(f"${predicted_price[0]:,.2f}")
8. Explanation of the Python Program
The program begins by importing Pandas and Linear Regression from Scikit-learn.
Pandas is used to create the sample car dataset, while Linear Regression is used to predict the continuous price value.
Step 1: Create the Dataset
A Python dictionary is created containing information about seven cars.
For example:
Car Age = 1 year
Engine Size = 1500cc
Mileage = 10,000 km
Owners = 1
Price = $25,000
The dictionary is converted into a Pandas DataFrame.
Step 2: Select Input Features
The four car-related features are selected:
X = df[
[
"Car_Age",
"Engine_Size",
"Mileage",
"Owners"
]
]
These features are used by the model to predict the car price.
Step 3: Select Target Variable
The Price column is selected as the target:
y = df["Price"]
The target is a continuous numerical value, making this a regression problem.
Step 4: Create the Model
A Linear Regression model is created:
model = LinearRegression()
Linear Regression is suitable because the objective is to predict a numerical value.
Step 5: Train the Model
The model is trained using:
model.fit(X, y)
During training, the model learns the relationship between car age, engine size, mileage, number of owners, and price.
Step 6: Predict a New Car's Price
The new car has the following details:
Car Age = 4 years
Engine Size = 1900cc
Mileage = 40,000 km
Owners = 2
The information is provided to the trained model:
new_car = [[4, 1900, 40000, 2]]
predicted_price = model.predict(new_car)
The model then calculates the estimated selling price.
9. Prediction
Input
Car Age = 4 years
Engine Size = 1900cc
Mileage = 40,000 km
Owners = 2
Output
Predicted Car Price:
$18,500.00
Based on the patterns learned from the sample dataset, the model predicts that the new car has an estimated price of $18,500.00.
This is only an estimated value generated by the Machine Learning model and does not represent an actual market price.
10. How Linear Regression Works
Linear Regression is a supervised Machine Learning algorithm used to predict continuous numerical values.
In this project, the target variable is the car price.
The model attempts to find a mathematical relationship between the input features and the price.
The relationship can be represented conceptually as:
Price =
(Car Age × Weight)
+ (Engine Size × Weight)
+ (Mileage × Weight)
+ (Owners × Weight)
+ Intercept
During training, the algorithm calculates suitable coefficients for the input features.
The model learns how changes in the car's characteristics are associated with changes in its price.
When a new car is provided, the learned coefficients are used to calculate an estimated selling price.
11. Advantages
The Car Price Prediction project has several advantages:
Simple and beginner-friendly.
Demonstrates regression concepts.
Uses multiple input features.
Easy to implement using Python.
Demonstrates continuous value prediction.
Linear Regression is computationally efficient.
Easy to understand and interpret.
Can be extended with additional vehicle features.
12. Limitations
The biggest limitation of this project is the very small sample dataset. Only seven cars are used for training, which is not sufficient for reliable real-world price prediction.
Actual car prices depend on many additional factors that are not included in this example.
These factors may include:
Car brand
Car model
Manufacturing year
Vehicle condition
Fuel type
Transmission type
Service history
Accident history
Location
Market demand
Optional features
Current market conditions
Engine size may also have different effects depending on the car's brand and model.
The model also has not been evaluated using a separate test dataset.
13. Future Improvements
The project can be improved by using a larger and more diverse car dataset containing thousands of real-world vehicle records.
Additional features can be included, such as:
Brand
Model
Manufacturing year
Fuel type
Transmission
Vehicle condition
Service history
Number of previous owners
Location
Categorical features such as brand and fuel type can be converted into numerical representations using appropriate encoding techniques.
The dataset can also be divided into training and testing datasets to measure model performance.
Regression metrics can be used to evaluate the model, including:
Mean Absolute Error (MAE)
Mean Squared Error (MSE)
Root Mean Squared Error (RMSE)
R² Score
Other regression algorithms can also be compared with Linear Regression, including:
Decision Tree Regression
Random Forest Regression
Gradient Boosting Regression
Support Vector Regression
A more advanced system could also use current market data to provide more realistic price estimates.
14. Real-World Applications
Car price prediction systems can be useful in several areas.
Used Car Marketplaces
Online car marketplaces can estimate the expected selling price of a vehicle based on its characteristics.
Car Dealerships
Dealerships can use historical sales data to help estimate suitable prices for used vehicles.
Vehicle Valuation
Financial and automotive businesses can use Machine Learning models to support vehicle valuation.
Buying and Selling Decisions
A prediction system can provide buyers and sellers with an estimated market value to help them make more informed decisions.
Price Comparison
Users can compare the predicted value of a vehicle with its listed selling price.
However, real-world pricing should consider current market conditions and professional valuation rather than relying only on a Machine Learning prediction.
15. Conclusion
The Car Price Prediction Using Machine Learning project demonstrates how supervised Machine Learning can be used to predict the selling price of a vehicle.
The project uses Linear Regression with four input features: car age, engine size, mileage, and number of previous owners. The model learns from historical car data where the actual prices are already known.
For the new car with:
Car Age = 4 years
Engine Size = 1900cc
Mileage = 40,000 km
Owners = 2
the model produces the following prediction:
Predicted Car Price:
$18,500.00
This project provides a simple introduction to regression, feature selection, model training, and numerical prediction. With a larger real-world dataset, additional vehicle features, proper model evaluation, and more advanced algorithms, the system can be developed into a more accurate car valuation and price prediction application.
