This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 4: More iterable objects

zip Exercise

face Josiah Wang

It’s your turn now! Here’s a quick exercise to try to use zip.

One use case for zip() is that your machine learning data might have the feature vectors x and labels y represented as separate lists. These are usually easier represented separately for training your algorithms.

In the example below, sample x[0] has the class label 1 (y[0]), sample x[1] has the class label 2 (y[1]) etc.

>>> x = [[0.2, 0.3], [0.5, 0.4], [0.1, 0.2], [0.2, 0.2], [0.4, 0.5], [0.5, 0.5]]
>>> y = [1, 2, 1, 1, 2, 2]

Sometimes you might want to put them together. Maybe you need to see the labels together with the feature vectors to analyse the data.

Write a function concatenate_feature_labels() that takes two lists x and y as input. x is a nested list with N rows and K columns. y is a list with N elements. The function should return a nested list with N rows and K+1 columns, where each item in y is appended to the end of each row in x, e.g. [[x[0][0], x[0][1], y[0]], [x[1][0], x[1][1], y[1]], ...]. See the example below.

Practise using zip to solve this exercise!

Of course, once you start using numerical libraries like NumPy, you will be concatenating your arrays instead. But let’s not worry about that for the moment!

Sample usage

>>> x = [[0.2, 0.3], [0.5, 0.4], [0.1, 0.2], [0.2, 0.2], [0.4, 0.5], [0.5, 0.5]]
>>> y = [1, 2, 1, 1, 2, 2]
>>> dataset = concatenate_feature_labels(x, y)
>>> print(dataset)
[[0.2, 0.3, 1], 
 [0.5, 0.4, 2], 
 [0.1, 0.2, 1], 
 [0.2, 0.2, 1], 
 [0.4, 0.5, 2], 
 [0.5, 0.5, 2]]