Numpy for:

• linear algebra
• image processing
• signal processing
• ..

``````

In [1]:

import numpy as np

``````
``````

In [2]:

my_list = np.array(range(10))
print(type(my_list))
my_list

``````
``````

<class 'numpy.ndarray'>

Out[2]:

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

``````
``````

In [3]:

np.arange(10)

``````
``````

Out[3]:

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

``````
``````

In [4]:

np.linspace(0, 10, 20)

``````
``````

Out[4]:

array([  0.        ,   0.52631579,   1.05263158,   1.57894737,
2.10526316,   2.63157895,   3.15789474,   3.68421053,
4.21052632,   4.73684211,   5.26315789,   5.78947368,
6.31578947,   6.84210526,   7.36842105,   7.89473684,
8.42105263,   8.94736842,   9.47368421,  10.        ])

``````

### Indexing & slicing

``````

In [5]:

my_list[0]

``````
``````

Out[5]:

0

``````
``````

In [6]:

my_list[0:2]

``````
``````

Out[6]:

array([0, 1])

``````

### Operations

``````

In [9]:

my_list * 2

``````
``````

Out[9]:

array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

``````
``````

In [10]:

my_list[ my_list % 2 == 0 ] * 2

``````
``````

Out[10]:

array([ 0,  4,  8, 12, 16])

``````
``````

In [12]:

my_list[ (my_list < 3) | (my_list > 6) ]

``````
``````

Out[12]:

array([0, 1, 2, 7, 8, 9])

``````

### Universal functions (ufunc)

``````

In [14]:

my_list_one = np.arange(0, 10)
my_list_two = np.arange(10, 20)

print(my_list_one)
print(my_list_two)
my_list_one + my_list_two

``````
``````

[0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]

Out[14]:

array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28])

``````

### Shape

``````

In [16]:

my_list.shape

``````
``````

Out[16]:

(10,)

``````

### Fancy Indexing

``````

In [17]:

my_list.reshape(2, 5)

``````
``````

Out[17]:

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

``````
``````

In [18]:

my_list.reshape(5, 2)

``````
``````

Out[18]:

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

``````
``````

In [19]:

### Stats

``````
``````

In [20]:

my_list.mean()

``````
``````

Out[20]:

4.5

``````
``````

In [21]:

np.mean(my_list)

``````
``````

Out[21]:

4.5

``````
``````

In [22]:

my_list.sum()

``````
``````

Out[22]:

45

``````

### Work with nan (not a number)

``````

In [23]:

np.mean([0, 1, 2, 3, np.nan])

``````
``````

Out[23]:

nan

``````
``````

In [25]:

np.nanmean([1, 2, 3, np.nan])

``````
``````

Out[25]:

2.0

``````
``````

In [26]:

np.nanmax([1, 2, 3, np.nan])

``````
``````

Out[26]:

3.0

``````
``````

In [27]:

np.nanmin([1, 2, 3, np.nan])

``````
``````

Out[27]:

1.0

``````

### Randomization

``````

In [28]:

[np.random.rand() for x in range(10)]

``````
``````

Out[28]:

[0.11417637488232857,
0.16953968214996096,
0.8060580229610691,
0.49552351558559204,
0.7666045886200948,
0.13917592678058777,
0.8437397145144154,
0.7794950766849901,
0.4841293664074877,
0.2149788938404672]

``````
``````

In [31]:

np.mean([np.random.rand() for x in range(100000)])

``````
``````

Out[31]:

0.50042409821794409

``````
``````

In [34]:

[np.random.rand() for x in range(10)]

``````
``````

Out[34]:

[0.5042523582139986,
0.30120690235513814,
0.12109979557652817,
0.8149755582447495,
0.3475941996751155,
0.3598559123066033,
0.7743055994007709,
0.11831590155119198,
0.14645077644303428,
0.2015445773962391]

``````
``````

In [36]:

np.random.seed(2017)
[np.random.rand() for x in range(10)]

``````
``````

Out[36]:

[0.020960225406117416,
0.7670701646824878,
0.44791979980060215,
0.12054161556730447,
0.9307729610869862,
0.6495504104423961,
0.14067106021701203,
0.23159433754757908,
0.22647529063859462,
0.2600549453493213]

``````
``````

In [37]:

np.random.rand(5, 2)

``````
``````

Out[37]:

array([[ 0.11287225,  0.63190484],
[ 0.38730813,  0.31624562],
[ 0.63089059,  0.29465108],
[ 0.94486453,  0.15162571],
[ 0.07616901,  0.70390961]])

``````

### Math things

``````

In [38]:

np.math.log(10)
np.log(10)

``````
``````

Out[38]:

2.3025850929940459

``````
``````

In [39]:

np.math.log2(10)

``````
``````

Out[39]:

3.321928094887362

``````
``````

In [40]:

np.exp( np.log(10) )

``````
``````

Out[40]:

10.000000000000002

``````
``````

In [ ]:

``````