Skip to content
Snippets Groups Projects
Commit cb79fc36 authored by Jeanette Lee's avatar Jeanette Lee
Browse files

Problem 1 solved

parent ee4e546c
No related branches found
No related tags found
No related merge requests found
Pipeline #327199 passed
%% Cell type:markdown id: tags:
# Problem 1 - Introduction Numpy
%% Cell type:markdown id: tags:
This is just a short programming quiz that asks you use a few NumPy features. It is meant to give you a little practice if you don't have NumPy experience
%% Cell type:code id: tags:
``` python
import numpy as np
def prepare_inputs(inputs):
# TODO: create a 2-dimensional ndarray from the given 1-dimensional list;
# assign it to input_array
input_array = None
input_array = np.reshape(inputs, (1, len(inputs)))
# TODO: find the minimum value in input_array and subtract that
# value from all the elements of input_array. Store the
# result in inputs_minus_min
inputs_minus_min = None
min_val = input_array.min()
inputs_minus_min = input_array - min_val
# TODO: find the maximum value in inputs_minus_min and divide
# all of the values in inputs_minus_min by the maximum value.
# Store the results in inputs_div_max.
inputs_div_max = None
max_val = inputs_minus_min.max()
inputs_div_max = inputs_minus_min / max_val
# return the three arrays we've created
return input_array, inputs_minus_min, inputs_div_max
def multiply_inputs(m1, m2):
# TODO: Check the shapes of the matrices m1 and m2.
# m1 and m2 will be ndarray objects.
#
# Return False if the shapes cannot be used for matrix
# multiplication. You may not use a transpose
pass
if (np.shape(m1)[1] != np.shape(m2)[0]) & (np.shape(m1)[0] != np.shape(m2)[1]):
return False
# TODO: If you have not returned False, then calculate the matrix product
# of m1 and m2 and return it. Do not use a transpose,
# but you swap their order if necessary
pass
elif (np.shape(m1)[1] == np.shape(m2)[0]):
return np.matmul(m1, m2)
else:
return np.matmul(m2, m1)
def find_mean(values):
# TODO: Return the average of the values in the given Python list
pass
return sum(values) / len(values)
input_array, inputs_minus_min, inputs_div_max = prepare_inputs([-1,2,7])
print("Input as Array: {}".format(input_array))
print("Input minus min: {}".format(inputs_minus_min))
print("Input Array: {}".format(inputs_div_max))
print("Multiply 1:\n{}".format(multiply_inputs(np.array([[1,2,3],[4,5,6]]), np.array([[1],[2],[3],[4]]))))
print("Multiply 2:\n{}".format(multiply_inputs(np.array([[1,2,3],[4,5,6]]), np.array([[1],[2],[3]]))))
print("Multiply 3:\n{}".format(multiply_inputs(np.array([[1,2,3],[4,5,6]]), np.array([[1,2]]))))
print("Mean == {}".format(find_mean([1,3,4])))
```
%% Output
Input as Array: None
Input minus min: None
Input Array: None
Input as Array: [[-1 2 7]]
Input minus min: [[0 3 8]]
Input Array: [[0. 0.375 1. ]]
Multiply 1:
None
False
Multiply 2:
None
[[14]
[32]]
Multiply 3:
None
Mean == None
[[ 9 12 15]]
Mean == 2.6666666666666665
%% Cell type:markdown id: tags:
# Problem 2 - Body Mass Index
%% Cell type:markdown id: tags:
The goal is to write a function which can calculate the Body Mass Index (BMI) of a given dataset. BMI is given by:
$$ BMI = \frac{Weight}{Length^2} $$
%% Cell type:code id: tags:
``` python
# import numpy
import numpy as np
np.set_printoptions(precision=2)
def BMI(weight, length):
# Function returning the BMI of input vectors m and l
return
m_example = [60, 72, 57, 90, 95, 72]
l_example = [1.75, 1.80, 1.65, 1.90, 1.74, 1.91]
print("The given weights and lengths result in BMI's: \n{}".format(BMI(m_example, l_example)))
```
%% Output
The given weights and lengths result in BMI's:
None
%% Cell type:markdown id: tags:
# Problem 3 - Using Pandas: Weather
%% Cell type:markdown id: tags:
In this problem you will have to make use of **Pandas** to load and analyse a small, fictional database weather.csv (the same as used in the Script).
The data containes the temperature in 4 cities over 6 months. Download the dataset and save it somewhere practically, possibly in the same folder as this notebook.
%% Cell type:code id: tags:
``` python
# First import Pandas
import pandas as pd
# load the database using pandas.read_csv. The file location is different for everyone. If you made a new folder in this folder named "data", the path would be ./data/weather.csv
# Example: data = pd.read_csv("/data/weather.csv")
data = None
data
```
%% Cell type:markdown id: tags:
### Viewing Data using Indices:
%% Cell type:markdown id: tags:
Using the names of rows and columns (not integer indices), find:
- Temperature in Basel for all months
- Temperature in Chur in February
%% Cell type:code id: tags:
``` python
t_basel = None
t_chur_feb = None
print("Temperature in Basel is:\n", t_basel, "\n")
print("Temperature in Chur in February is:\n", t_chur_feb)
```
%% Output
Temperature in Basel is:
None
Temperature in Chur in February is:
None
%% Cell type:markdown id: tags:
### Changing Indices
%% Cell type:markdown id: tags:
Now we will look at the indices themselves.
- Find the name of column 3 using Python
- Change "Basel" to "Bern"
%% Cell type:code id: tags:
``` python
# Find the name of column 3:
name_c3 = None
print(name_c3)
# Change "Basel" to "Bern"
data
```
%% Output
None
%% Cell type:markdown id: tags:
### .mean() Method:
Now find :
- the average temperature in Chur (for the given Period)
- the average temperature in Spring in Switzerland (spring is Mar, Apr, May, mean of all cities)
%% Cell type:code id: tags:
``` python
t_chur_mean = None
t_spring_mean = 0
# Hint: mean() on a DataFrame gives the result in a Serries. The axis of the function to be applied on can be set with axis={index (0), columns (1)}.
# To find the mean of the whole matrix, the mean method can be applied twice
print("Average temperature in Chur was: {}".format(t_chur_mean))
print("Average temperature in Spring was: {}".format(round(t_spring_mean, 1)))
```
%% Output
Average temperature in Chur was: None
Average temperature in Spring was: 0
%% Cell type:markdown id: tags:
### .sort_values() Method:
- Sort the data based on the temperature in Zürich
- Sort the data based on decreasing temperature in Basel
%% Cell type:code id: tags:
``` python
# Sort data based on Zurich
```
%% Cell type:code id: tags:
``` python
# Sort data based on Basel, descending with temperature
```
%% Cell type:markdown id: tags:
# Problem 4: Fuel Consumption
In this exercise, you will analyse a dataset containing information on Fuel consumption of cars in the eighties. The data contains 3 columns, the weight of the car in Pounds(1 Pound = 0.454 kg), the range in Miles per Gallon(1 mile=1.61 km; 1 gallon=3.79 l), and the type of car. Download the dataset and save it somewhere practically, possibly in the same folder as this notebook.
%% Cell type:code id: tags:
``` python
# First import Pandas
import pandas as pd
# load the database using pandas.read_csv. The file location is different for everyone. If you made a new folder in this folder named "data", the path would be ./data/d.fuel.dat
# Example: data = pd.read_csv("/data/d.fuel.dat")
data = None
data
```
%% Cell type:markdown id: tags:
To get a quick overview, we can view only the first 5 rows of the dataset. Print the first five rows using:
- **dataframe.loc**
- **DataFrame.head()**
%% Cell type:code id: tags:
``` python
# Print the first 5 rows using data.loc
```
%% Cell type:code id: tags:
``` python
# print the first 5 rows using data.head()
```
%% Cell type:markdown id: tags:
### .mean() Method:
Now find :
- the average range of all cars
- the average range of all cars with type "Medium" (hint, select all rows with a certain constraint using **DataFrame[DataFrame[** *column* **].isin([** *values* **])]**
%% Cell type:code id: tags:
``` python
avg_mpg = 0
avg_medium = 0
print("Average miles per galon is: \n{}".format(round(avg_mpg, 2)), "\nAverage miles per galon for all Medium type cars is: \n{}".format(round(avg_medium,2)))
```
%% Output
Average miles per galon is:
0
Average miles per galon for all Medium type cars is:
0
%% Cell type:markdown id: tags:
### Conversion to SI units
- Create a Series containing the range in km/l and another Series containing the weight in kg.
- Find the average of these new Vectors
%% Cell type:code id: tags:
``` python
t_kml = pd.DataFrame()
t_kg = pd.DataFrame()
print(t_kml.head())
print(t_kg.head())
avg_kml = t_kml.mean()
avg_kg = t_kg.mean()
print("\nAverage Kilometer per liter is: \n{}".format(round(avg_kml, 2)), "\nAverage weight in kilogram is: \n{}".format(round(avg_kg,2)))
```
%% Output
Empty DataFrame
Columns: []
Index: []
Empty DataFrame
Columns: []
Index: []
Average Kilometer per liter is:
Series([], dtype: float64)
Average weight in kilogram is:
Series([], dtype: float64)
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment