Exercises
Contents
1.4. Exercises#
These exercises are from [4], [2] and [3].
1.4.1. Python Notebook#
(Hello World!)
Please set up a Python Notebook environment and type print('Hello World!')
.
Please set up a Python Notebook and start a new virtual environment and type print('Hello World!')
.
1.4.2. Basic Python#
(Play with lists)
Please complete the following tasks.
Write a
for
loop 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.
(Play with list, dict and pandas.)
Please complete the following tasks.
Create a new dictionary
people
with two keysname
andage
. The values are all empty list.Add
Tony
to thename
list inpeople
.Add
Harry
to thename
list inpeople
.Add number 100 to the
age
list inpeople
.Add number 10 to the
age
list inpeople
.Find all the keys of
people
and save them into a listnamelist
.Convert the dictionary
people
to 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)
(The dataset iris)
from sklearn.datasets import load_iris
iris = load_iris()
Please explore this dataset.
Please get the features for
iris
and save it intoX
as an numpy array.What is the meaning of these features?
Please get the labels for
iris
and save it intoy
as 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.
(Play with Pandas)
Please download the Titanic data file from here
. Then follow the instructions to perform the required tasks.
Use
pandas.read_csv
to read the dataset and save it as a dataframe objectdf
.Change the values of the
Sex
column thatmale
is0
andfemale
is1
.Pick the columns
Pclass
,Sex
,Age
,Siblings/Spouses Aboard
,Parents/Children Aboard
andFare
and transform them into a 2-dimensionalnumpy.ndarray
, and save it asX
.Pick the column
Survived
and transform it into a 1-dimensionalnumpy.ndarray
and save it asy
.
Solution to Exercise 1.6 (Play with Pandas)
Not yet done!