Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/ML/Notebook/POC CO2 emission.ipynb
3087 views
Kernel: Python 3

Technical Problem Statement

Climate change due to carbon dioxide (CO 2) emissions is one of the most complex challenges threatening our planet. This issue considered as a great and international Concern that primary attributed from different fossil fuels.

image.png

  • To reduce the effect of Global Warming caused mainly due to cars(CO 2) manufacturing companies are pre-estimating the CO2 emissions for a newly manufactured car that is about to launch in the near future based on the features of the car.

  • This project deals with estimating the CO2 Emissions for a newly manufactured car by making Regression models which can accurately predetermine the car CO2 emissions before it is being launched.

image.png

We have a sample dataset of year 2014 manufactured cars with their brands & other important specifications.lets see how we can design predetermine the co2 emision of a car.

About Data Set

The Data given here is for the Year 2014 manufactured Cars.

  • YEAR – Year of manufacturing of car.

  • MAKE – Manufacturing company name.

  • VEHICLECLASS – Type of vehicles like SUV or medium-sized etc.

  • ENGINESIZE – Size of the car’s engine (expressed in cc or cubic centimetre).

  • CYLINDERS – Number of Cylinders in the engine.

  • TRANSMISSION – Automatic or manual transmission with the number of gears.

  • FUELTYPE – It indicates the type of fuel car use i.e. Diesel, Petrol, Z (Unleaded Petrol) etc.

  • FUELCONSUMPTION_CITY – Fuel consumption or Fuel economy of car while running in city expressed in miles per gallon.

  • FUELCONSUMPTION_HWY - Fuel Consumption or Fuel economy of car on highway expressed in miles per gallon.

  • FUELCONSUMPTION_COMB – Net or combination of Fuel Economy expressed in miles per gallon.

  • FUELCONSUMPTION_COMB_MPG – Total fuel economy expressed in miles per gallon.

  • CO2EMISSIONS – The CO2 emitted by the car expressed in grams.

Importing Data Set

import pandas as pd import numpy as np from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, r2_score import matplotlib.pyplot as plt import pandas_profiling
c=pd.read_excel("F:\ML & Data Visualization\CO2 emission.xlsx")
c.head()
# Data Profiling DataPre_report = pandas_profiling.ProfileReport(c) DataPre_report.to_file(outputfile="myoutputfile.html")
## Identify the target variable c['FUELTYPE'],a = pd.factorize(c['FUELTYPE']) c['VEHICLECLASS'],a = pd.factorize(c['VEHICLECLASS']) c['TRANSMISSION'],a = pd.factorize(c['TRANSMISSION']) c['MODEL'],a = pd.factorize(c['MODEL']) c.head()
# Dropping feilds MODEL YEAR & CYLINDERS c = c.drop(['MODELYEAR','CYLINDERS','MAKE'], axis=1) X = c.drop('CO2EMISSIONS', axis=1) y = c[['CO2EMISSIONS']]
## we’ll split the dataset into a train set and a test set. ## Scikit-learn has a very straightforward train_test_split function for that. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

Lets build the regression model. First, let’s try a model with only one variable

#CO2 EMISSION VERSES FUEL TYPE reg = LinearRegression() reg.fit(X_train[['ENGINESIZE']], y_train)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
# Evaluation metrics for linear regression are mean squared error and the R² score. y_predicted = reg.predict(X_test[['ENGINESIZE']]) print("Mean squared error: %.2f" % mean_squared_error(y_test, y_predicted)) print('R²: %.2f' % r2_score(y_test, y_predicted))
Mean squared error: 955.37 R²: 0.76
except e as exception try :

Insights

  • The best possible score is 1.0, We get a model with a mean squared error of 955.37 and an R² of0.76.

reg = LinearRegression() reg.fit(X_train[['ENGINESIZE','FUELCONSUMPTION_CITY','FUELCONSUMPTION_COMB']], y_train) y_predicted = reg.predict(X_test[['ENGINESIZE','FUELCONSUMPTION_CITY','FUELCONSUMPTION_COMB']]) print("Mean squared error: %.2f" % mean_squared_error(y_test, y_predicted)) print('R²: %.2f' % r2_score(y_test, y_predicted))
Mean squared error: 590.67 R²: 0.85