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 for loop to print values from 0 to 4.

  • Combine two lists ['apple', 'orange'] and ['banana'] using +.

  • Sort the list ['apple', 'orange', 'banana'] using sorted().

Exercise 1.4 (Play with list, dict and pandas.)

Please complete the following tasks.

  • Create a new dictionary people with two keys name and age. The values are all empty list.

  • Add Tony to the name list in people.

  • Add Harry to the name list in people.

  • Add number 100 to the age list in people.

  • Add number 10 to the age list in people.

  • Find all the keys of people and save them into a list namelist.

  • Convert the dictionary people to a Pandas DataFrame df.

Exercise 1.5 (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 into X as an numpy array.

  • What is the meaning of these features?

  • Please get the labels for iris and save it into y as an numpy array.

  • What is the meaning of labels?

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_csv to read the dataset and save it as a dataframe object df.

  • Change the values of the Sex column that male is 0 and female is 1.

  • Pick the columns Pclass, Sex, Age, Siblings/Spouses Aboard, Parents/Children Aboard and Fare and transform them into a 2-dimensional numpy.ndarray, and save it as X.

  • Pick the column Survived and transform it into a 1-dimensional numpy.ndarray and save it as y.