Exercises
Contents
1.4. Exercises#
These exercises are from [4], [2] and [3].
1.4.1. Python Notebook#
Exercise 1.1 (Hello World!)
Please set up a Python Notebook environment and type print('Hello World!').
Exercise 1.2
Please set up a Python Notebook and start a new virtual environment and type print('Hello World!').
1.4.2. Basic Python#
Exercise 1.3 (Play with lists)
Please complete the following tasks.
Write a
forloop to print values from 0 to 4.Combine two lists
['apple', 'orange']and['banana']using+.Sort the list
['apple', 'orange', 'banana']usingsorted().
Solution to Exercise 1.3 (Play with lists)
for i in range(5):
print(i)
newlist = ['apple', 'orange'] + ['banana']
sorted(['apple', 'orange', 'banana'])
Please be careful about the last line. sorted() doesn’t change the original list. It create a new list. There are some Python functions which change the inputed object in-place. Please read documents on all packages you use to get the desired results.
Exercise 1.4 (Play with list, dict and pandas.)
Please complete the following tasks.
Create a new dictionary
peoplewith two keysnameandage. The values are all empty list.Add
Tonyto thenamelist inpeople.Add
Harryto thenamelist inpeople.Add number 100 to the
agelist inpeople.Add number 10 to the
agelist inpeople.Find all the keys of
peopleand save them into a listnamelist.Convert the dictionary
peopleto a Pandas DataFramedf.
Solution to Exercise 1.4 (Play with list, dict and pandas.)
import pandas as pd
people = {'name': list(), 'age': list()}
people['name'].append('Tony')
people['name'].append('Harry')
people['age'].append(100)
people['age'].append(10)
namelist = people.keys()
df = pd.DataFrame(people)
Exercise 1.5 (The dataset iris)
from sklearn.datasets import load_iris
iris = load_iris()
Please explore this dataset.
Please get the features for
irisand save it intoXas an numpy array.What is the meaning of these features?
Please get the labels for
irisand save it intoyas an numpy array.What is the meaning of labels?
Solution to Exercise 1.5 (The dataset iris)
We first find that iris is a dictionary. Then we can look at all the keys by iris.keys(). The interesting keys are data, target, target_names and feature_names. We can also read the description of the dataset by looking at DESCR.
X = iris['data']
print(iris['feature_names'])
y = iris['target']
print(iris['target'])
Since the data is already saved as numpy arrays, we don’t need to do anything to change its type.
Exercise 1.6 (Play with Pandas)
Please download the Titanic data file from here. Then follow the instructions to perform the required tasks.
Use
pandas.read_csvto read the dataset and save it as a dataframe objectdf.Change the values of the
Sexcolumn thatmaleis0andfemaleis1.Pick the columns
Pclass,Sex,Age,Siblings/Spouses Aboard,Parents/Children AboardandFareand transform them into a 2-dimensionalnumpy.ndarray, and save it asX.Pick the column
Survivedand transform it into a 1-dimensionalnumpy.ndarrayand save it asy.
Solution to Exercise 1.6 (Play with Pandas)
Not yet done!