Data analysis and visualization
Purpose
Gain experience in analyzing and visualizing data using NumPy and matplotlib and Jupyter notebooks.
Background
You will be given a dataset of several models and makes of cars as a CSV file [cars.csv]. Each car type is represented by a single row in the dataset with the following columns:
- column 0: number of cylinders (integer number)
- column 1: engine displacement (number, measured in ???ℎ2inch2)
- column 2: engine power (number, measured in ℎ?hp)
- column 3: gross weight (number, measured in ???lbs)
- column 4: acceleration (floating point number, number of seconds to reach 60 ??ℎmph)
- column 5: gas milage (number, measured in ???MPG)
You task is load the dataset, execute simple regression tasks and visualize the results. The steps are clearly explained with an empty notebook cell for each. You job is to implement the step in the given cell and evalute it. Finally, you need to upload your final notebook (.ipynb file) to Blackboard.
TASK 0
In the following code cell fill out your credentials.
In [3]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
TASK 1
Load the cars.csv file into a single NumPy array. You can use the csv Python module or the loadtxt NumPy function for this. The resulting NumPy array should have the following shape: (392, 6)
Points: 10
In [38]:
# Implement Task1
TASK 2
Create four scatter plots (using subplot, arranged on a 2 x 2 tile). The plots should visualize how the MPG parameter depends on:
- number of cylinders (plot 1)
- engine displacement (plot 2)
- horsepower (plot 3)
- gross weight (plot 4)
Use different colors for the different plots. For each subplot provide a proper title and axis labels (x and y).
Points: 40
In [34]:
# Implement Task2
TASK 3
Repeat the same four scatter plots, which were created in Task 2. This time also compute the linear regression between the MPG and the corresponding variable using the np.lstsq() approach. To each subplot add a red line visualizing the result (the line with the calculated slope and intercept).
Help: once you calculated the slope (m) and intercept (c) values, you can create the regression line by using the independent variable array (e.g. number of cylinders). E.g. if the indepent variable is in the x array, use plt.plot(x, m * x + c, ‘r-‘).
Points: 30
In [35]:
# Implement Task3
TASK 4
The previous task computed the linear regression between the MPG and four other parameters separately. In this task, create a multi-variate regression model, where all five parameters (number of cylinders, engine displacement, engine power, gross weight and acceleration) are the independent variables (x) and the MPG value is the dependent variable (y).
Based on the linear regression model, what is the expected MPG parameter of an unknown car with the following parameters:
- number of cylinders = 6
- engine displacement = 266
- engine power = 178
- gross weight = 4112
- acceleration = 14.4
You do not need to provide any visualization, only the final answer (estimated MPG).
Help: read the documentation of lstsq.
Points: 20
In [37]:
# Implement Task4