``````

In [1]:

import numpy as np

``````

### Simple numpy examples

``````

In [2]:

a = [1, 2, 3]

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

In [3]:

a

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

Out[3]:

[1, 2, 3]

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

In [4]:

a_array = np.array(a)

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

In [5]:

a_array.shape

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

Out[5]:

(3,)

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

In [6]:

# sum, min, max
a_array.sum(), a_array.min(), a_array.max()

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

Out[6]:

(6, 1, 3)

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

In [7]:

np.atleast_2d(a_array), np.atleast_2d(a_array).shape

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

Out[7]:

(array([[1, 2, 3]]), (1, 3))

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

In [8]:

np.atleast_1d(a_array), np.atleast_1d(a_array).shape

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

Out[8]:

(array([1, 2, 3]), (3,))

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

In [9]:

np.atleast_3d(a_array), np.atleast_3d(a_array).shape

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

Out[9]:

(array([[[1],
[2],
[3]]]), (1, 3, 1))

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

In [10]:

# flatten the array
np.atleast_2d(a_array).reshape(-1)

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

Out[10]:

array([1, 2, 3])

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

In [11]:

np.atleast_2d(a_array).flatten()

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

Out[11]:

array([1, 2, 3])

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

In [12]:

# identity matrix
id_3 = np.identity(3)
id_3

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

Out[12]:

array([[ 1.,  0.,  0.],
[ 0.,  1.,  0.],
[ 0.,  0.,  1.]])

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

In [13]:

# trace operator
id_3.trace() == 3

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

Out[13]:

True

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

In [14]:

# matrix * scalar
id_3 * 3

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

Out[14]:

array([[ 3.,  0.,  0.],
[ 0.,  3.,  0.],
[ 0.,  0.,  3.]])

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

In [15]:

# matrix + scalar
id_3 + 1

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

Out[15]:

array([[ 2.,  1.,  1.],
[ 1.,  2.,  1.],
[ 1.,  1.,  2.]])

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

In [16]:

# matrix / scalar
id_3 / 2

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

Out[16]:

array([[ 0.5,  0. ,  0. ],
[ 0. ,  0.5,  0. ],
[ 0. ,  0. ,  0.5]])

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

In [17]:

# dot product id_3^T a_vector
np.dot(id_3, a_array)

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

Out[17]:

array([ 1.,  2.,  3.])

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

In [18]:

# random variables
np.random.randn(4)

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

Out[18]:

array([ 0.75886891,  0.86179416,  0.48905969,  0.66290265])

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

In [19]:

np.random.random((2, 3))

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

Out[19]:

array([[ 0.32480787,  0.29697068,  0.43656527],
[ 0.25677007,  0.46790961,  0.54044141]])

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

In [20]:

# uniform distribution between [a, b]
np.random.randint(10)

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

Out[20]:

9

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

In [21]:

np.ones((3,3))

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

Out[21]:

array([[ 1.,  1.,  1.],
[ 1.,  1.,  1.],
[ 1.,  1.,  1.]])

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

In [22]:

np.ones(3)

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

Out[22]:

array([ 1.,  1.,  1.])

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

In [23]:

np.ones((3,3)).reshape(-1)

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

Out[23]:

array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])

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

In [24]:

# stacking
np.hstack((np.ones(3), np.ones(3)))

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

Out[24]:

array([ 1.,  1.,  1.,  1.,  1.,  1.])

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

In [25]:

np.vstack((np.ones(3), np.ones(3)))

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

Out[25]:

array([[ 1.,  1.,  1.],
[ 1.,  1.,  1.]])

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

In [26]:

# similar to hstack
np.concatenate((np.ones(3), np.ones(3)))

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

Out[26]:

array([ 1.,  1.,  1.,  1.,  1.,  1.])

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

In [ ]:

``````