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
importnumpyasnp
defprepare_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
returninput_array,inputs_minus_min,inputs_div_max
defmultiply_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]):
returnFalse
# 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]):
returnnp.matmul(m1,m2)
else:
returnnp.matmul(m2,m1)
deffind_mean(values):
# TODO: Return the average of the values in the given Python list
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
importnumpyasnp
np.set_printoptions(precision=2)
defBMI(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
importpandasaspd
# 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
importpandasaspd
# 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)))