This is a collection of exercises that have been collected in the numpy mailing list, on stack overflow and in the numpy documentation. The goal of this collection is to offer a quick reference for both old and new users but also to provide a set of exercices for those who teach.
If you find an error or think you've a better way to solve some of them, feel free to open an issue at https://github.com/rougier/numpy-100
In [11]:
import numpy as np
#print(np.show_config())
In [12]:
print(np.show_config())
In [8]:
Z = np.zeros(10)
print (Z)
In [13]:
Z = np.zeros((10,10))
print("%d bytes" % (Z.size * Z.itemsize))
In [14]:
python -c "import numpy;numpy.info(numpy.add)"
In [15]:
Z =np.zeros((10,10))
Z[4] = 1
print(Z)
In [15]:
z = np.arange(10,49)
print(z)
In [17]:
z = np.arange(50)
z[::-1]
Out[17]:
In [34]:
z = np.reshape(np.arange(9),(3,3))
print (z)
In [35]:
z = np.nonzero([1,2,0,0,4,0])
print (z)
In [39]:
z = np.eye(3)
print(z)
In [ ]:
z = np.random.random((3,3,3))
print(z)
In [44]:
z = np.random.random((10,10))
z_min,z_max = z.min(),z.max()
print(z_min)
print(z_max)
In [46]:
z = np.random.random(30)
print(z.mean())
In [49]:
Z = np.ones((10,10))
Z[1:-1,1:-1] = 0
print(Z)
In [ ]:
0 * np.nan
np.nan == np.nan
np.inf > np.nan
np.nan - np.nan
0.3 == 3 * 0.1
In [55]:
print(0 * np.nan)
print(np.nan == np.nan)
print(np.nan > np.nan)
print(np.nan - np.nan)
print(0.3 == 3 * 0.1)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
# Author: Jake VanderPlas
print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))
In [ ]:
Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z
In [ ]:
np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
np.sqrt(-1) == np.emath.sqrt(-1)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
1, 2, 3, 4, 5
6, , , 7, 8
, , 9,10,11
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: