Competition - Python Scholarship
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    Everyone Can Learn Python Scholarship by Yugo Saito

    📖 About me 😄

    I noticed "Everyone Can Learn Python" Scholarship on DataCamp and it was open for everyone. I decided to join this wonderful event beucase I wanted to know how much I improve my data sicience skills through using Datacamp so far, but also I thought it was great oppotunity for me to start my first programming competion.

    Since I graduated from an university with non-techinical degree and wants to purse Data science Masters program in the future, I belive I am eligible to be in this competion.

    In advance, thanks for reading my competion and I hope you can share your thoughts and comment so I can learn and become a better data scientist as well as data analyst.

    Let's get started! :)

    1️⃣ Python 🐍 - CO2 Emissions challenge!

    📖 Background

    You volunteer for a public policy advocacy organization in Canada, and your colleague asked you to help her draft recommendations for guidelines on CO2 emissions rules.

    After researching emissions data for a wide range of Canadian vehicles, she would like you to investigate which vehicles produce lower emissions.

    💾 The data I

    You have access to seven years of CO2 emissions data for Canadian vehicles (source):

    • "Make" - The company that manufactures the vehicle.
    • "Model" - The vehicle's model.
    • "Vehicle Class" - Vehicle class by utility, capacity, and weight.
    • "Engine Size(L)" - The engine's displacement in liters.
    • "Cylinders" - The number of cylinders.
    • "Transmission" - The transmission type: A = Automatic, AM = Automatic Manual, AS = Automatic with select shift, AV = Continuously variable, M = Manual, 3 - 10 = the number of gears.
    • "Fuel Type" - The fuel type: X = Regular gasoline, Z = Premium gasoline, D = Diesel, E = Ethanol (E85), N = natural gas.
    • "Fuel Consumption Comb (L/100 km)" - Combined city/highway (55%/45%) fuel consumption in liters per 100 km (L/100 km).
    • "CO2 Emissions(g/km)" - The tailpipe carbon dioxide emissions in grams per kilometer for combined city and highway driving.

    The data comes from the Government of Canada's open data website.

    # 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
    cars
    # Look at the first ten items in the CO2 emissions array
    cars_co2_emissions[:10]

    💪 Challenge I

    Help your colleague gain insights on the type of vehicles that have lower CO2 emissions. Include:

    1. What is the median engine size in liters?
    2. What is the average fuel consumption for regular gasoline (Fuel Type = X), premium gasoline (Z), ethanol (E), and diesel (D)?
    3. What is the correlation between fuel consumption and CO2 emissions?
    4. Which vehicle class has lower average CO2 emissions, 'SUV - SMALL' or 'MID-SIZE'?
    5. What are the average CO2 emissions for all vehicles? For vehicles with an engine size of 2.0 liters or smaller?
    6. Any other insights you found during your analysis?
    1. What is the median engine size in liters?
    • In order to answer this question, we need to import statistics and math first. And then, use a mean method to get mean from cars_engine_sizes.
    # Import all the libraries you need to use.
    import statistics
    import math
    
    # average engine size(L) for all cars.
    median_engine_size = round(statistics.mean(cars_engine_sizes),2)
    
    print("Median engine size is", median_engine_size)
    
    
    1. What is the average fuel consumption for regular gasoline (Fuel Type = X), premium gasoline (Z), ethanol (E), and diesel (D)?
    • In order to answer question number 2, we need to get each elements from cars_fuel_types.
    #an average fuel consumption for all cars. 
    all_cars_fuel_average = statistics.mean(cars_fuel_consumption)
    # Printing average of the list
    print ("all cars average fuel consumption")
    print(round(all_cars_fuel_average, 2))
    
    #The fuel type: X = Regular gasoline, Z = Premium gasoline, D = Diesel, E = Ethanol (E85), N = natural gas.
    
    #an average Regular gasoline fuel consumption. 
    X_cars_fuel_average = cars[cars_fuel_types == 'X']['Fuel Consumption Comb (L/100 km)'].mean()
    print ("Regular cars average fuel consumption")
    print (round(X_cars_fuel_average, 2))
    
    #an average Premium gasoline fuel consumption. 
    Z_cars_fuel_average = cars[cars_fuel_types == 'Z']['Fuel Consumption Comb (L/100 km)'].mean()
    print ("Premium gasoline cars average fuel consumption")
    print (round(Z_cars_fuel_average, 2))
    
    #an average Diesel gasoline fuel consumption. 
    D_cars_fuel_average = cars[cars_fuel_types == 'D']['Fuel Consumption Comb (L/100 km)'].mean()
    print ("Diesel cars average fuel consumption")
    print (round(D_cars_fuel_average, 2))
    
    #an average Ethanol gasoline fuel consumption. 
    E_cars_fuel_average = cars[cars_fuel_types == 'E']['Fuel Consumption Comb (L/100 km)'].mean()
    print ("Ethanol cars average fuel consumption")
    print (round(E_cars_fuel_average, 2))
    
    #an average natural gas fuel consumption. 
    N_cars_fuel_average = cars[cars_fuel_types == 'N']['Fuel Consumption Comb (L/100 km)'].mean()
    print ("natural gas cars average fuel consumption")
    print (round(N_cars_fuel_average, 2))
    

    After finishing a number 2 question, we can tell that Ethanol cars consume fuel the most. Natural gas-powered cars are on the second place. Cars with premium gasoline are on the third place. Regualr cars are on the fourth place. Diesel cars are the last place which means diseal cars burn less carbon dioxide (CO2) than other cars.

    3.What is the correlation between fuel consumption and CO2 emissions?

    • I believe that there is some correlation between fuel consumption and CO2 emissions. When cars produce more fuel, they emit more CO2.
    • In order to know whether or not if these two variables are related to each other, Scatter plot might be a great method to determine.
    # Import all the libraries you need to use.
    import matplotlib.pyplot as plt
    
    #visualizing a graph is one of the great way to know two variable's relationships.
    plt.scatter(cars_fuel_consumption, cars_co2_emissions)
    plt.xlabel("cars_fuel_consumption")
    plt.ylabel("cars_co2_emissions")
    plt.show()
    
    #identifying the relationship between two variable by using corrcoef function. 
    np.corrcoef(cars_fuel_consumption, cars_co2_emissions)
    

    As I expected, fuel consumption and co2 emissions are strongly related. They have strong postive relashionships.