Numpy exercices


In [1]:
import numpy as np

Working with arrays

Create the following array (without typing the elements!):

 array([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])

In [ ]:

Extending arrays

Because arrays do no have the append method np.concatenate, np.vstack and np.hstack functions are very useful.

  1. Create: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
  2. add the raw [10, 20, 30]
  3. add [100, 200, 300, 400] as a column to the new a in 2.

In [3]:
a = np.arange(9).reshape(3,3)

In [7]:
a.mean?

Saving and retrieving arrays

Let's create a couple of large arrays:

a=random.random([1000, 1000])
b=arange([1000])

Save them to disk with save with savez and with savetxt. Delete a and b (del a,b) and recover them from disk. Check the size of the saved text file. Now append a .gz to the filename when saving with savetxt and see what happens. How do you load this file?


In [ ]:

Removing negative numbers with Fancy Indexing

We have an array that has small negative numbers and we want to remove them, converting them to zero. First, we generate a random array.


In [ ]:
np.random.seed(3)
a = np.random.random((4,3))-.2

In [ ]:
a

Find the indices where this array is negative:


In [ ]:
index = #Finish

Convert those indices into a zero:


In [ ]:

We can also generte the indices in the same expression where we set a


In [ ]:
np.random.seed(3) #This way we can reproduce the same "random" array as before.
a = np.random.random((4,3))-.2
a[a<0] = 0
a

We could have also used the where method. But this method does not change a in place.


In [ ]:
np.random.seed(3) #This way we can reproduce the same "random" array as before.
a = np.random.random((4,3))-.2
np.where(a<0,0, a) #This does not change a

More on slicing

Now we have an array and we need to substract the minimum value of each of the columns.


In [ ]:
a = np.arange(12).reshape((4,3))
a

we can use the 'min method (or numpy function)


In [ ]:
amin = a.min(axis=0)
amin

Finally we substract this from a:


In [ ]:

Imagine we want to do the same for rows:


In [ ]:
a = np.arange(12).reshape((4,3))
a

In [ ]:
amin = a.min(axis=1)

In [ ]:
a-amin

How can we solve this? We could transpose the arrays to get the right dimensions for broadcasting, or extend the dimensions of amin get again the right dimensions for broadcastinc


In [ ]:
#Transposing (use .T method):

In [ ]:
#Extending axis

In [ ]:
#Extending axis

dot product and Outer product

The dot product is so common that it is implemented in numpy. But if it was not there, could you code it?


In [ ]:
a = np.arange(5)
b = np.arange(5)+10

In [ ]:
result = #Finish

In [ ]:
assert result == a.dot(b)

Let's check which is faster for large vectors:


In [ ]:
a = np.random.random(10000)
b = np.random.random(10000)

In [ ]:
%timeit a.dot(b)

In [ ]:
%timeit #Your code

Try writing the code in a for loop (if you haven't) and check its performance with the %timeit magic function.


In [ ]:

The dot vector can be represented as $\vec x^T \vec y$ if vectors are matrices of dimensions $(N, 1)$. As such we could also mupliply them this way $\vec x \vec y^T$. This returns a matrix of dimensions $(N, N)$, where each element $i, j$ of this matrix is $x_i y_j$. Can you code that?


In [ ]:
a = np.arange(5)
b = np.arange(5)+10
a,b

In [7]:
m=np.random.random((10,10))-np.random.normal(scale=0.2, size=(10,10))


Out[7]:
array([[ 0.36873081,  0.72011549,  0.62800143,  0.08537417,  0.74020582,
        -0.08401784,  0.93977809, -0.02062952,  0.33880919,  0.59753261],
       [ 0.38228122,  1.20726815,  0.45929903,  0.43048193,  0.19279779,
         0.71438802,  1.00554436,  0.74165518,  0.80788075,  0.24475675],
       [ 0.72796771,  0.37634381,  0.53725671, -0.11657096,  0.13999787,
         0.33417107,  0.39791197,  0.52035295,  0.58182513,  0.82315915],
       [ 0.21587885,  0.2610651 ,  0.66376969,  0.8137709 ,  0.75600012,
         0.7134234 ,  0.10770219,  0.31022349,  0.34997016, -0.0336475 ],
       [ 0.60450379,  0.54275676,  0.87502377,  1.27187025,  1.01828454,
         0.93165396,  0.12340618,  1.06040378,  0.1476069 ,  0.98249825],
       [ 1.02905158,  0.66099979, -0.16184452,  0.96919091,  0.0989624 ,
         0.56817581,  0.86711513,  0.53569094,  0.46162896,  0.67829717],
       [ 1.09955574,  0.75872486,  0.72445248,  1.01146433,  0.47912199,
         0.80007685,  0.35555064,  0.92132599,  0.29970717,  0.64606451],
       [ 0.96316178,  1.18989259,  0.58743376,  0.40634885, -0.22511437,
         0.53089497,  0.67538086,  0.21885454,  0.4902112 ,  1.12808555],
       [-0.07923765,  0.29343402,  0.11534585,  0.91297467,  0.55880474,
         0.65083601,  0.41461239,  0.7677817 ,  0.32281172,  0.64491225],
       [ 0.84371556,  0.16483102,  0.40629409,  0.5098535 ,  1.00262379,
         0.32523128,  0.37379695,  0.12765442,  1.08222311,  0.50941952]])

In [4]:
np.random.normal?

In [ ]: