Commit b8c75e14 authored by Mirko Birbaumer's avatar Mirko Birbaumer
Browse files

Adapted Solution Basics notebook

parents c2fd0506 fc85c206
%% Cell type:markdown id: tags:
# Solution to Problem 1
%% Cell type:code id: tags:
``` python
# Use the numpy library
import numpy as np
np.set_printoptions(precision=2)
######################################################
#
# MESSAGE TO STUDENTS:
#
# This file contains a solution to the coding quiz. Feel free
# to look at it when you are stuck, but try to solve the
# problem on your own first.
#
######################################################
def prepare_inputs(inputs):
# TODO: create a 2-dimensional ndarray from the given 1-dimensional list;
# assign it to input_array
input_array = np.array([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
# We can use NumPy's min function and element-wise division
inputs_minus_min = input_array - np.min(input_array)
# 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.
# We can use NumPy's max function and element-wise division
inputs_div_max = inputs_minus_min / np.max(inputs_minus_min)
return input_array, inputs_minus_min, inputs_div_max
def multiply_inputs(m1, m2):
# 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
if m1.shape[0] != m2.shape[1] and m1.shape[1] != m2.shape[0]:
return False
# Have not returned False, so calculate the matrix product
# of m1 and m2 and return it. Do not use a transpose,
# but you swap their order if necessary
if m1.shape[1] == m2.shape[0]:
return np.matmul(m1, m2)
else:
return np.matmul(m2, m1)
def find_mean(values):
# Return the average of the values in the given Python list
# NumPy has a lot of helpful methods like this.
return np.mean(values)
```
%% Cell type:code id: tags:
``` python
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: stream
Input as Array: [[-1 2 7]]
Input minus min: [[0 3 8]]
Input Array: [[0. 0.38 1. ]]
Multiply 1:
False
Multiply 2:
[[14]
[32]]
Multiply 3:
[[ 9 12 15]]
Mean == 2.6666666666666665
%% Cell type:markdown id: tags:
# Solution to Problem 2 - Body Mass Index
%% 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
m = np.array(weight) # Create arrays from inputs
l = np.array(length)
# Calculate all BMI's simultaniously
BMI = np.divide(m, l**2)
# Or just
BMI = m/l**2
return BMI
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: stream
The given weights and lengths result in BMI's:
[19.59 22.22 20.94 24.93 31.38 19.74]
%% Cell type:markdown id: tags:
# Solution to Problem 3 - Using Pandas: Weather
%% Cell type:code id: tags:
``` python
# First import Pandas
import pandas as pd
# load the database using pandas.read_csv
data = pd.read_csv("./data/weather.csv")
data
```
%%%% Output: execute_result
Luzern Basel Chur Zurich
Jan 2 5 -3 4
Feb 5 6 1 0
Mar 10 11 13 8
Apr 16 12 14 17
May 21 23 21 20
Jun 25 21 23 27
%% Cell type:markdown id: tags:
### Viewing Data using Indices:
%% Cell type:code id: tags:
``` python
t_basel = data["Basel"]
t_chur_feb = data.loc["Feb", "Chur"]
print("Temperature in Basel is:\n", t_basel, "\n")
print("Temperature in Chur in February is:\n", t_chur_feb)
```
%%%% Output: stream
Temperature in Basel is:
Jan 5
Feb 6
Mar 11
Apr 12
May 23
Jun 21
Name: Basel, dtype: int64
Temperature in Chur in February is:
1
%% Cell type:markdown id: tags:
### Changing Indices
%% Cell type:code id: tags:
``` python
# Find the name of column 3:
all_columns = data.columns
name_c3 = all_columns[3]
# Oder
name_c3 = data.columns.values[3]
print(name_c3)
# Change "Basel" to "Bern"
data.columns.values[1] = "Bern"
data
```
%%%% Output: stream
Zurich
%%%% Output: execute_result
Luzern Bern Chur Zurich
Jan 2 5 -3 4
Feb 5 6 1 0
Mar 10 11 13 8
Apr 16 12 14 17
May 21 23 21 20
Jun 25 21 23 27
%% 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 = data.loc[:, "Chur"].mean()
t_spring_mean = data.loc["Mar":"May", :].mean().mean()
# 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: stream
Average temperature in Chur was: 11.5
Average temperature in Spring was: 15.5
%% 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
data.sort_values("Zurich")
```
%%%% Output: execute_result
Luzern Bern Chur Zurich
Feb 5 6 1 0
Jan 2 5 -3 4
Mar 10 11 13 8
Apr 16 12 14 17
May 21 23 21 20
Jun 25 21 23 27
%% Cell type:code id: tags:
``` python
data.sort_values("Basel", ascending=False)
```
%%%% Output: execute_result
Luzern Bern Chur Zurich
May 21 23 21 20
Jun 25 21 23 27
Apr 16 12 14 17
Mar 10 11 13 8
Feb 5 6 1 0
Jan 2 5 -3 4
%% Cell type:markdown id: tags:
# Solution to Problem 4: Fuel Consumption
%% Cell type:code id: tags:
``` python
# First import Pandas
import pandas as pd
# load the database using pandas.read_csv with options: sep="," and index_col=0
# data = None
data = pd.read_csv("./data/d.fuel.dat")
data
```
%%%% Output: execute_result
X weight mpg type
0 1 2560 33 Small
1 2 2345 33 Small
2 3 1845 37 Small
3 4 2260 32 Small
4 5 2440 32 Small
5 6 2285 26 Small
6 7 2275 33 Small
7 8 2350 28 Small
8 9 2295 25 Small
9 10 1900 34 Small
10 11 2390 29 Small
11 12 2075 35 Small
12 13 2330 26 Small
13 14 3320 20 Sporty
14 15 2885 27 Sporty
15 16 3310 19 Sporty
16 17 2695 30 Sporty
17 18 2170 33 Sporty
18 19 2710 27 Sporty
19 20 2775 24 Sporty
20 21 2840 26 Sporty
21 22 2485 28 Sporty
22 23 2670 27 Compact
23 24 2640 23 Compact
24 25 2655 26 Compact
25 26 3065 25 Compact
26 27 2750 24 Compact
27 28 2920 26 Compact
28 29 2780 24 Compact
29 30 2745 25 Compact
30 31 3110 21 Compact
31 32 2920 21 Compact
32 33 2645 23 Compact
33 34 2575 24 Compact
34 35 2935 23 Compact
35 36 2920 27 Compact
36 37 2985 23 Compact
37 38 3265 20 Medium
38 39 2880 21 Medium
39 40 2975 22 Medium
40 41 3450 22 Medium
41 42 3145 22 Medium
42 43 3190 22 Medium
43 44 3610 23 Medium
44 45 2885 23 Medium
45 46 3480 21 Medium
46 47 3200 22 Medium
47 48 2765 21 Medium
48 49 3220 21 Medium
49 50 3480 23 Medium
50 51 3325 23 Large
51 52 3855 18 Large
52 53 3850 20 Large
53 54 3195 18 Van
54 55 3735 18 Van
55 56 3665 18 Van
56 57 3735 19 Van
57 58 3415 20 Van
58 59 3185 20 Van
59 60 3690 19 Van
%% 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
data.loc[1:5,:]
```
%%%% Output: execute_result
X weight mpg type
1 2 2345 33 Small
2 3 1845 37 Small
3 4 2260 32 Small
4 5 2440 32 Small
5 6 2285 26 Small
%% Cell type:code id: tags:
``` python
# print the first 5 rows using data.head()
data.head()
```
%%%% Output: execute_result
X weight mpg type
0 1 2560 33 Small
1 2 2345 33 Small
2 3 1845 37 Small
3 4 2260 32 Small
4 5 2440 32 Small
%% 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
data.mean() # Gives all averages
avg_mpg = data.mean()["mpg"] # Gives the average mpg
avg_medium = data[data["type"].isin(["Medium"])].mean()["mpg"]
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: stream
Average miles per galon is:
24.58
Average miles per galon for all Medium type cars is:
21.77
%% 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 = data["mpg"]*1.61/3.79
t_kg = data["weight"]*0.454
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: stream
0 14.018470
1 14.018470
2 15.717678
3 13.593668
4 13.593668
Name: mpg, dtype: float64
0 1162.24
1 1064.63
2 837.63
3 1026.04
4 1107.76
Name: weight, dtype: float64
Average Kilometer per liter is:
10.44
Average weight in kilogram is:
1316.98
%% Cell type:code id: tags:
``` python
```
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment