Skip to content
Snippets Groups Projects
Commit adafda90 authored by Simon van Hemert's avatar Simon van Hemert
Browse files

1st

parent 6284d5c3
No related branches found
No related tags found
No related merge requests found
File added
%% Cell type:markdown id: tags:
### Checking your installation
Please check that the notebook below runs smoothly.
%% Cell type:code id: tags:
``` python
import sys
sys.version #Should work and give 3.7.
```
%% Output
'3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)]'
%% Cell type:code id: tags:
``` python
import tensorflow as tf
tf.__version__ #Should work and give 2.1.0
```
%% Output
'2.1.0'
%% Cell type:code id: tags:
``` python
import numpy as np
np.__version__ #Should work and give something > 1.19
```
%% Output
'1.19.1'
%% Cell type:code id: tags:
``` python
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(range(100), np.sin(0.1 * np.array(range(100))))
```
%% Output
<matplotlib.collections.PathCollection at 0x17a7a929508>
%% 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
# 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
# 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
# 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
# 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
def find_mean(values):
# TODO: Return the average of the values in the given Python list
pass
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
Multiply 1:
None
Multiply 2:
None
Multiply 3:
None
Mean == None
%% 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
```
This diff is collapsed.
numpy==1.19.1
pandas==1.1.3
matplotlib==3.3.1
opencv-python==4.1.2.30
tensorflow==2.1.0
\ No newline at end of file
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