0% found this document useful (0 votes)
30 views10 pages

Iml 51

Uploaded by

kariyasneha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views10 pages

Iml 51

Uploaded by

kariyasneha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Name : Dod Vishakha

Enrollment:226400307051

Topic : Fuel Consumption

Subject : IML(4350702)

Class : CO-51 (512)

Faculty : J.A.Suthar
Introduction:
 This project aims to predict vehicle fuel consumption using
machine learning, specifically **Polynomial Regression**. By
analyzing features like engine size, number of cylinders, highway
fuel efficiency, and emissions, the model can accurately forecast
fuel consumption. Additionally, it examines fuel consumption
trends over time, offering insights into changes in efficiency. This
model helps manufacturers, consumers, and policymakers make
better decisions, contributing to efforts in improving fuel efficiency
and reducing environmental impact.

Objectives of the Fuel Consumption Prediction Project

1. Predict Fuel Consumption: Develop a machine learning model that


accurately predicts fuel consumption based on vehicle characteristics,
including engine size, number of cylinders, highway and combined fuel
efficiency, and emissions.

2. Analyze Historical Trends : Examine historical data to identify trends


in fuel consumption over the years, helping to understand factors
influencing fuel efficiency.

3.Enhance Decision-Making : Provide insights that assist vehicle


manufacturers in designing more fuel-efficient models, inform
consumers in making better purchasing choices, and guide policymakers
in promoting sustainable transportation practices.

4.User-Friendly Interface : Create an interactive interface that allows


users to input vehicle specifications and obtain real-time fuel
consumption predictions.

5.Evaluate Model Performance : Assess the accuracy of the predictive


model using metrics such as R² score and RMSE, and compare actual
versus predicted fuel consumption to identify areas for improvement.

1. Problem Definition
The goal of the project is to create a predictive model that can estimate
fuel consumption for vehicles given a set of features, such as:
- Engine size
- Number of cylinders
- Highway fuel consumption
- Combined fuel consumption
- Emissions

2. Dataset
The dataset (`Fuel_Consumption.csv`) contains information on different
vehicles and their corresponding fuel consumption and emissions. Key
columns include:
- ENGINE SIZE : Size of the engine in liters.
- CYLINDERS :Number of cylinders in the vehicle.
- HWY (L/100 km) : Fuel consumption on the highway.
- COMB (L/100 km) : Combined fuel consumption.
- EMISSIONS : Carbon emissions of the vehicle.
- FUEL CONSUMPTION : The target variable to predict, representing
fuel consumption.
- YEAR : The year the vehicle was manufactured, used for trend
analysis.

3. Steps Involved in the Project

Step 1: Data Loading and Preview


- Load the dataset using pandas and display the first few rows to
understand the structure.

Step 2: Feature Selection


- Select features that are relevant to predicting fuel consumption:
- Engine Size
- Cylinders
- Highway Fuel Consumption
- Combined Fuel Consumption
- Emissions
- The **target** variable is the fuel consumption.

Step 3: Handling Missing Data


- Handle missing values by filling them with zeros (`X.fillna(0)`,
`y.fillna(0)`), ensuring no empty data disrupts the model training process.

Step 4: Exploratory Data Analysis (EDA)


- Fuel Consumption Trend Over the Years :
- Calculate the average fuel consumption for each year using
**groupby** and plot the trend. This helps identify if fuel consumption
has improved or worsened over time.

Step 5: Feature Engineering


- Polynomial Feature Expansion :
- Use PolynomialFeatures(degree=2)** to create higher-order polynomial
features that capture non-linear relationships between the features and
fuel consumption. This allows for more complex modeling beyond simple
linear regression.

Step 6: Train-Test Split


- Split the data into training and test sets using train_test_split. The
training set (80%) is used to train the model, and the test set (20%) is
used to evaluate its performance.

Step 7: Data Normalization


- Scale the features using StandardScaler to ensure all features have
the same scale, which is crucial for model performance, especially in
polynomial regression.

Step 8: Model Building


- Linear Regression: Fit the linear regression model on the transformed
(polynomial) features. The model learns the relationship between the
vehicle features and fuel consumption.

Step 9: Model Evaluation


- After the model is trained, it makes predictions on the test set. The
model’s performance is evaluated using:
- R² Score: Measures how well the model explains the variance in fuel
consumption.
- RMSE (Root Mean Squared Error) : Measures the average error
between predicted and actual fuel consumption.

Step 10: User Input for Predictions


- Allow the user to input vehicle characteristics (engine size, cylinders,
etc.) and make predictions based on the trained model. The input values
are transformed into polynomial features and scaled before being
passed to the model.

Step 11: Visualization


- Fuel Consumption Over the Years: A line plot showing the average fuel
consumption by year.
- Actual vs Predicted Fuel Consumption: A line plot comparing actual vs
predicted values for the test data. The blue line represents the actual
fuel consumption, while the green dashed line represents the predicted
values.

4.Tools and Libraries Used


- pandas : For data loading and manipulation.
- numpy : For numerical operations.
- scikit-learn : For machine learning tasks including:
- train_test_split : To split the dataset.
- PolynomialFeatures : For creating polynomial feature combinations.
- StandardScaler : For feature scaling.
- LinearRegression : For building the regression model.
- r2_score, mean_squared_error : For evaluating the model's accuracy.
- matplotlib : For visualizing fuel consumption trends and model
predictions.

5.Expected Outcome
- The model will be able to accurately predict fuel consumption based on
vehicle characteristics.
- Insights into how fuel consumption has changed over the years will be
derived from the trend analysis.
- By comparing actual vs predicted fuel consumption, the model's
performance can be analyzed, and further improvements (e.g., adjusting
polynomial degrees or using other algorithms) can be considered.

CODE:

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score, mean_squared_error
import matplotlib.pyplot as plt

csv_file_path = 'Fuel_Consumption.csv'
data = pd.read_csv(csv_file_path)
print("Data Preview:")
print(data.head())
features = ['ENGINE SIZE', 'CYLINDERS', 'HWY (L/100 km)', 'COMB
(L/100 km)', 'EMISSIONS']
target = 'FUEL CONSUMPTION'
year_column = 'YEAR'

if all(feature in data.columns for feature in features) and target in


data.columns:
if year_column in data.columns:
print("\nPlotting Fuel Consumption Over the Years...")
data_yearly_avg =
data.groupby(year_column)[target].mean().reset_index()
plt.figure(figsize=(10, 5))
plt.plot(data_yearly_avg[year_column], data_yearly_avg[target],
marker='o')
plt.xlabel('Year')
plt.ylabel('Fuel Consumption')
plt.title('Average Fuel Consumption by Year')
plt.grid(True)
plt.show()
else:
print(f"'{year_column}' column is missing from the dataset.")

X = data[features].copy()
y = data[target].copy()

X.fillna(0, inplace=True)
y.fillna(0, inplace=True)

poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(X_poly, y,


test_size=0.2, random_state=42)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

model = LinearRegression()
model.fit(X_train_scaled, y_train)

y_pred = model.predict(X_test_scaled)
# Calculate and display R² score and RMSE
r2 = r2_score(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print(f"\nModel Accuracy (R² Score): {r2}")
print(f"Model RMSE (Root Mean Squared Error): {rmse}")

print("\nMake Predictions")
feature_values = []
for feature in features:
value = input(f"Enter value for {feature}: ")
feature_values.append(value)

input_data = pd.DataFrame([feature_values], columns=features)

input_data_poly = poly.transform(input_data)

input_data_scaled = scaler.transform(input_data_poly)

prediction = model.predict(input_data_scaled)
print(f"\nPredicted {target}: {prediction[0]}")

print("\nPlotting Actual vs Predicted Fuel Consumption (Test Set)...")


plt.figure(figsize=(10, 6))

# Plotting actual values


plt.plot(range(len(y_test)), y_test, label='Actual Fuel Consumption',
color='blue', linestyle='-', marker='o')

# Plotting predicted values


plt.plot(range(len(y_pred)), y_pred, label='Predicted Fuel
Consumption', color='green', linestyle='--', marker='x')

plt.xlabel('Test Data Index')


plt.ylabel('Fuel Consumption')
plt.title('Actual vs Predicted Fuel Consumption')
plt.legend()
plt.grid(True)
plt.show()

else:
print(f"Required columns are missing. Please ensure the dataset
contains the following columns: {', '.join(features)}, {target}.")
OUTPUT:
Conclusion :

This project successfully demonstrates the application of machine


learning, particularly **Polynomial Regression**, for predicting vehicle
fuel consumption based on key features like engine size, cylinder count,
highway fuel efficiency, and emissions. The model provides accurate
predictions, and the analysis of historical trends helps identify changes
in fuel consumption patterns over time.

By using data-driven insights, this model can assist manufacturers in


designing more fuel-efficient vehicles, consumers in making informed
purchasing decisions, and policymakers in promoting environmentally
sustainable practices. Future improvements, such as incorporating more
features and refining the model, could further enhance prediction
accuracy and applicability in real-world scenarios.

You might also like