Matias Jimenez/

Competition - Everyone Can Learn Python Scholarship


Everyone Can Learn Python Scholarship

# Import the pandas and numpy packages
import pandas as pd
import numpy as np

# Load the data
cars = pd.read_csv('data/co2_emissions_canada.csv')

# create numpy arrays
cars_makes = cars['Make'].to_numpy()
cars_models = cars['Model'].to_numpy()
cars_classes = cars['Vehicle Class'].to_numpy()
cars_engine_sizes = cars['Engine Size(L)'].to_numpy()
cars_cylinders = cars['Cylinders'].to_numpy()
cars_transmissions = cars['Transmission'].to_numpy()
cars_fuel_types = cars['Fuel Type'].to_numpy()
cars_fuel_consumption = cars['Fuel Consumption Comb (L/100 km)'].to_numpy()
cars_co2_emissions = cars['CO2 Emissions(g/km)'].to_numpy()

# Preview the dataframe
median_engine_size = cars['Engine Size(L)'].median()
avg_fuel_consumption = cars[cars['Fuel Type'].isin(['X','Z','E','D'])].groupby('Fuel Type')['Fuel Consumption Comb (L/100 km)'].mean()
r_fuel_co2 = np.corrcoef(cars['Fuel Consumption Comb (L/100 km)'], cars['CO2 Emissions(g/km)'])[0,1]
less_CO2_class = cars[cars['Vehicle Class'].isin(['SUV - SMALL', 'MID-SIZE'])].groupby('Vehicle Class')['CO2 Emissions(g/km)'].mean().to_frame()
less_CO2_class_min = less_CO2_class[less_CO2_class['CO2 Emissions(g/km)'] == less_CO2_class['CO2 Emissions(g/km)'].min()]
CO2_avg = cars['CO2 Emissions(g/km)'].mean()
CO2_avg_small = cars[cars['Engine Size(L)'] <= 2]['CO2 Emissions(g/km)'].mean()

print('The median engine size in liters is :', median_engine_size)
print('Average consumption per fuel type:')
print('Correlation between fuel consumption and CO2: ', r_fuel_co2)
print('Vehicle class with lowest CO2 emissions: ')
print('Average CO2 emissions for all vehicles: ', CO2_avg, ', Average for 2 liters or smaller: ', CO2_avg_small)
print(np.corrcoef(cars['Engine Size(L)'],cars['CO2 Emissions(g/km)'])[0,1])

#Other data for the analysis:
#cars.groupby(['Fuel Type'])['CO2 Emissions(g/km)'].mean()
#cars.groupby(['Vehicle Class'])['Fuel Type'].value_counts(normalize = True)

From the results obtained it is possible to analyze that the Median engine size in liters is 3.0L.

As we keep analyzing the consumption by fuel type, we obtain that the best performer on average are the vehicles that uses diesel as fuel, with and average of 8.835429 l/100km. The average consumption per fuel type is listed below:

1.- Diesel: 8.83 l/100km. 2.- Ethanol: 16.86 l/100km. 3.- Premium: 11.42 l/100km. 3.- Regular: 10.08 l/100km.

The correlation between fuel consumption and CO2 emissions is 0.92, this is a strong positive correlation which means that we should expect more CO2 emissions as the fuel consumption increases. But this does not mean causation. If we consider that the fuel type with lowest average consumption is diesel, vehicles with this type of fuel not always are the most ecofriendly ones. In fact, if we take a look to the average CO2 emissions per fuel type, the regular type has a lower average (235.11 g/km) than diesel (237.54 g/km).

The vehicle class with the lowest emissions is MID-SIZE between mid-size and suv-small. This result is interesting considering that suv-small class has 73% of the cars with regular fuel type while in mid-size class only 52% of the cars use this type of fuel. Due to this, we should analyze other variables that should be afecting the averga CO2 emissions.

If we calculate the average CO2 emissions for all the vehicles we will obtain 250.58 g/km. If we consider only cars with 2 liter engine size or less we oobtain an average of 198.26 g/km of CO2 emissions. Probably the engine size is related to the amount of CO2 emissions. If we calculate the correlation between engine size and CO2 emissions, the correlation is positive and strong (0.85), but not as strong as the correlation between fuel consumption and CO2 emissions.

⌛️ Time is ticking. Good luck!

  • AI Chat
  • Code