Numpy snippets

Jérémie Decock (www.jdhp.org)

This notebook is inspired by the following page: https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html

Import directives

In [2]:
import numpy as np

Create arrays

In [3]:
np.array([1, 2, 3])
Out[3]:
array([1, 2, 3])
In [4]:
np.array([[1, 2, 3],[4, 5, 6]])
Out[4]:
array([[1, 2, 3],
       [4, 5, 6]])

Special matrices

In [5]:
np.zeros(3)
Out[5]:
array([ 0.,  0.,  0.])
In [6]:
np.zeros((3, 4))
Out[6]:
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
In [7]:
np.ones(3)
Out[7]:
array([ 1.,  1.,  1.])
In [8]:
np.ones((3, 4))
Out[8]:
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])
In [9]:
np.eye(3)
Out[9]:
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

Arange

In [10]:
np.arange(10)
Out[10]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [11]:
np.arange(10, 20)
Out[11]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
In [12]:
np.arange(10, 20, 2)
Out[12]:
array([10, 12, 14, 16, 18])

Linspace

In [13]:
np.linspace(0., 2., 5)
Out[13]:
array([ 0. ,  0.5,  1. ,  1.5,  2. ])

Meshgrid

In [14]:
xx, yy = np.meshgrid([1, 2, 3], [4, 5, 6])

print(xx)
print()
print(yy)
[[1 2 3]
 [1 2 3]
 [1 2 3]]

[[4 4 4]
 [5 5 5]
 [6 6 6]]

Random

Uniform distribution in [0, 1]

In [15]:
np.random.rand(3)
Out[15]:
array([ 0.51498028,  0.62514479,  0.72957466])
In [16]:
np.random.rand(3, 4)
Out[16]:
array([[ 0.46188211,  0.77342569,  0.18078838,  0.12440576],
       [ 0.76509152,  0.61070724,  0.77116347,  0.47833324],
       [ 0.75990474,  0.3869403 ,  0.50191423,  0.22838685]])

Poisson distribution

In [17]:
np.random.poisson(10, size=[3, 4])
Out[17]:
array([[ 5,  9, 10, 10],
       [16, 10, 10, 14],
       [12, 13, 10, 14]])

Multivariate normal distribution

In [18]:
mu = np.array([0., 0.])
cov = np.array([[1., 0.],
                [1., 1.]])
num_points = 10

np.random.multivariate_normal(mu, cov, num_points)
Out[18]:
array([[-0.25951125, -1.248422  ],
       [-1.69499599, -0.58937619],
       [ 0.06690751, -2.56550358],
       [ 0.48857787,  0.35063807],
       [ 0.43200808, -0.42062445],
       [-0.96498925,  0.03767726],
       [-1.25939788, -0.08747101],
       [ 0.26148065,  1.24200725],
       [-1.2198893 ,  1.05582199],
       [ 1.47421706,  0.62854289]])
In [19]:
np.get_printoptions()
Out[19]:
{'edgeitems': 3,
 'formatter': None,
 'infstr': 'inf',
 'linewidth': 75,
 'nanstr': 'nan',
 'precision': 8,
 'suppress': False,
 'threshold': 1000}
In [20]:
default_threshold = np.get_printoptions()["threshold"]
default_threshold
Out[20]:
1000

Arrays with more than default_threshold elements are truncated.

In [21]:
max_size = math.ceil(math.sqrt(default_threshold))
max_size
Out[21]:
32
In [22]:
a = np.random.randint(1, size=[max_size + 1, max_size + 1])
a
Out[22]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]])

Print the full array (set threshold to infinity):

In [23]:
np.set_printoptions(threshold=np.inf)
a
Out[23]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

Go back to the default threshold:

In [24]:
np.set_printoptions(threshold=default_threshold)
a
Out[24]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]])

Dimension and shape

In [25]:
a = np.array([[1, 2, 3],[4, 5, 6]])
In [26]:
a.ndim
Out[26]:
2
In [27]:
a.size
Out[27]:
6
In [28]:
a.shape
Out[28]:
(2, 3)

Convert

In [29]:
l = [[1, 2, 3],[4, 5, 6]]
a = np.array([[1, 2, 3],[4, 5, 6]])

Python list to Numpy array

In [30]:
np.array(l)
Out[30]:
array([[1, 2, 3],
       [4, 5, 6]])

Numpy array to Python list

In [31]:
a.tolist()
Out[31]:
[[1, 2, 3], [4, 5, 6]]

Copy

np.copy()

In [32]:
a = np.array([[1, 2, 3],[4, 5, 6]])
a
Out[32]:
array([[1, 2, 3],
       [4, 5, 6]])
In [33]:
b = a.copy()
b
Out[33]:
array([[1, 2, 3],
       [4, 5, 6]])
In [34]:
a[0,0] = 10
print(a)
print(b)
[[10  2  3]
 [ 4  5  6]]
[[1 2 3]
 [4 5 6]]

np.astype()

In [35]:
a = np.array([[1, 2, 3],[4, 5, 6]])
a
Out[35]:
array([[1, 2, 3],
       [4, 5, 6]])
In [36]:
b = a.astype('float64', copy=True)
b
Out[36]:
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])
In [37]:
a[0,0] = 10
print(a)
print(b)
[[10  2  3]
 [ 4  5  6]]
[[ 1.  2.  3.]
 [ 4.  5.  6.]]

Access elements

In [38]:
a = np.arange(6)
In [39]:
a
Out[39]:
array([0, 1, 2, 3, 4, 5])
In [40]:
a[0]
Out[40]:
0
In [41]:
a[-1]
Out[41]:
5

Slices

In [42]:
a[1:4]
Out[42]:
array([1, 2, 3])
In [43]:
a = np.array([[1, 2, 3, 4, 5, 6],
              [10, 20, 30, 40, 50, 60],
              [100, 200, 300, 400, 500, 600]])
a
Out[43]:
array([[  1,   2,   3,   4,   5,   6],
       [ 10,  20,  30,  40,  50,  60],
       [100, 200, 300, 400, 500, 600]])
In [44]:
a[0,1]
Out[44]:
2
In [45]:
a[1, :]
Out[45]:
array([10, 20, 30, 40, 50, 60])
In [46]:
a[1, ::2]
Out[46]:
array([10, 30, 50])
In [47]:
a[:, 1]
Out[47]:
array([  2,  20, 200])
In [48]:
a[0:2, 2:4]
Out[48]:
array([[ 3,  4],
       [30, 40]])
In [49]:
a[1:, 1:]
Out[49]:
array([[ 20,  30,  40,  50,  60],
       [200, 300, 400, 500, 600]])
In [50]:
a[:-1, :-1]
Out[50]:
array([[ 1,  2,  3,  4,  5],
       [10, 20, 30, 40, 50]])

Ellipsis

"The ellipsis is used to slice high-dimensional data structures.

It's designed to mean at this point, insert as many full slices (:) to extend the multi-dimensional slice to all dimensions."

https://stackoverflow.com/questions/118370/how-do-you-use-the-ellipsis-slicing-syntax-in-python

In [51]:
a = np.arange(2**3).reshape(2, 2, 2)
a
Out[51]:
array([[[0, 1],
        [2, 3]],

       [[4, 5],
        [6, 7]]])

To select all first elements in the last (3rd) dimension

In [52]:
a[..., 0]
Out[52]:
array([[0, 2],
       [4, 6]])

is equivalent to

In [53]:
a[:, :, 0]
Out[53]:
array([[0, 2],
       [4, 6]])

To select all first elements in the first (1st) dimension

In [54]:
a[0, ...]
Out[54]:
array([[0, 1],
       [2, 3]])

is equivalent to

In [55]:
a[0, :, :]
Out[55]:
array([[0, 1],
       [2, 3]])

Filter

In [56]:
a = np.array([[1, 2, 3, 4, 5, 6],
              [10, 20, 30, 40, 50, 60],
              [100, 200, 300, 400, 500, 600]])
a
Out[56]:
array([[  1,   2,   3,   4,   5,   6],
       [ 10,  20,  30,  40,  50,  60],
       [100, 200, 300, 400, 500, 600]])

Boolean matrix whose i,jth element is (a_ij > 5)

In [57]:
(a>5)
Out[57]:
array([[False, False, False, False, False,  True],
       [ True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True]], dtype=bool)

Find the indices where (a > 5)

In [58]:
np.nonzero(a>5)
Out[58]:
(array([0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]),
 array([5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]))

Set or copy a with elements greater than 5 zeroed out

In [59]:
a * (a<=5)
Out[59]:
array([[1, 2, 3, 4, 5, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])
In [60]:
a[a>5] = 0
a
Out[60]:
array([[1, 2, 3, 4, 5, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])

Select indices satisfying multiple conditions

Short version

In [61]:
a = np.array([[-1, 7, 3], [-11, -5, 20]])
a
Out[61]:
array([[ -1,   7,   3],
       [-11,  -5,  20]])
In [62]:
a[(a > -10) & (a < 10)] = 0
a
Out[62]:
array([[  0,   0,   0],
       [-11,   0,  20]])
In [63]:
a[(a < -10) | (a > 10)] = 1
a
Out[63]:
array([[0, 0, 0],
       [1, 0, 1]])

Detailed version

In [64]:
a = np.array([[-1, 7, 3], [-11, -5, 20]])
a
Out[64]:
array([[ -1,   7,   3],
       [-11,  -5,  20]])
In [65]:
m1 = (a > -10)
m2 = (a < 10)

print(m1)
print(m2)
print(m1 & m2)

a[m1 & m2] = 0
a
[[ True  True  True]
 [False  True  True]]
[[ True  True  True]
 [ True  True False]]
[[ True  True  True]
 [False  True False]]
Out[65]:
array([[  0,   0,   0],
       [-11,   0,  20]])

Concatenate

Concatenate vectors

In [66]:
a = np.zeros(3)
b = np.ones(3)
In [67]:
a
Out[67]:
array([ 0.,  0.,  0.])
In [68]:
b
Out[68]:
array([ 1.,  1.,  1.])
In [69]:
np.concatenate([a, b])
Out[69]:
array([ 0.,  0.,  0.,  1.,  1.,  1.])
In [70]:
np.hstack([a, b])
Out[70]:
array([ 0.,  0.,  0.,  1.,  1.,  1.])

Concatenate matrices

In [71]:
a = np.zeros([2, 3])
b = np.ones([2, 3])
In [72]:
a
Out[72]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
In [73]:
b
Out[73]:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

On the first dimension

In [74]:
np.vstack([a, b])
Out[74]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
In [75]:
np.vstack([a, b]).shape
Out[75]:
(4, 3)
In [76]:
np.stack([a, b], 0)
Out[76]:
array([[[ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]]])
In [77]:
np.stack([a, b], 0).shape
Out[77]:
(2, 2, 3)

On the second dimension

In [78]:
np.hstack([a, b])
Out[78]:
array([[ 0.,  0.,  0.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  1.,  1.,  1.]])
In [79]:
np.hstack([a, b]).shape
Out[79]:
(2, 6)
In [80]:
np.stack([a, b], 1)
Out[80]:
array([[[ 0.,  0.,  0.],
        [ 1.,  1.,  1.]],

       [[ 0.,  0.,  0.],
        [ 1.,  1.,  1.]]])
In [81]:
np.stack([a, b], 1).shape
Out[81]:
(2, 2, 3)

On the third dimension

In [82]:
np.stack([a, b], 2)
Out[82]:
array([[[ 0.,  1.],
        [ 0.,  1.],
        [ 0.,  1.]],

       [[ 0.,  1.],
        [ 0.,  1.],
        [ 0.,  1.]]])
In [83]:
np.stack([a, b], 2).shape
Out[83]:
(2, 3, 2)

Tile

In [84]:
a = np.array([[1, 2, 3], [4, 5, 6]])
np.tile(a, (2, 3))
Out[84]:
array([[1, 2, 3, 1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6, 4, 5, 6],
       [1, 2, 3, 1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6, 4, 5, 6]])

Reshape or transpose

In [85]:
a = np.array([[1, 2, 3], [4, 5, 6]])

Transpose

In [86]:
a.T
Out[86]:
array([[1, 4],
       [2, 5],
       [3, 6]])

Flatten

In [87]:
a.flatten()
Out[87]:
array([1, 2, 3, 4, 5, 6])

Reshape

In [88]:
a = np.arange(6)
a
Out[88]:
array([0, 1, 2, 3, 4, 5])

Row vector to column vector

In [89]:
a.reshape([-1, 1])
Out[89]:
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5]])

Vector to matrix

In [90]:
a.reshape([2, 3])
Out[90]:
array([[0, 1, 2],
       [3, 4, 5]])
In [91]:
a.reshape([3, 2])
Out[91]:
array([[0, 1],
       [2, 3],
       [4, 5]])

Linear algebra

In [92]:
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
In [93]:
np.dot(a, b)
Out[93]:
140
In [94]:
a.dot(b)
Out[94]:
140

Data types

Get type

In [95]:
a = np.arange(-2., 2., 0.5)
a.dtype
Out[95]:
dtype('float64')

Init

In [96]:
a = np.zeros(3)
a.dtype
Out[96]:
dtype('float64')
In [97]:
a = np.zeros(3, dtype=np.bool)
a.dtype
Out[97]:
dtype('bool')
In [98]:
a = np.zeros(3, dtype=np.int)
a.dtype
Out[98]:
dtype('int64')
In [99]:
a = np.zeros(3, dtype=np.int8)
a.dtype
Out[99]:
dtype('int8')
In [100]:
a = np.zeros(3, dtype=np.uint8)
a.dtype
Out[100]:
dtype('uint8')

Conversions

In [101]:
a = np.arange(-2., 2., 0.5)
a
Out[101]:
array([-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5])
In [102]:
a.astype(np.bool)
Out[102]:
array([ True,  True,  True,  True, False,  True,  True,  True], dtype=bool)
In [103]:
a.astype(np.int)
Out[103]:
array([-2, -1, -1,  0,  0,  0,  1,  1])
In [104]:
a.astype(np.int8)
Out[104]:
array([-2, -1, -1,  0,  0,  0,  1,  1], dtype=int8)
In [105]:
a.astype(np.uint8)
Out[105]:
array([254, 255, 255,   0,   0,   0,   1,   1], dtype=uint8)

Masked arrays

Without masked array

In [106]:
a = np.array([[np.nan, 2, 3], [1, np.nan, 6]])
a
Out[106]:
array([[ nan,   2.,   3.],
       [  1.,  nan,   6.]])
In [107]:
a.min()
Out[107]:
nan
In [108]:
np.nanmin(a)
Out[108]:
1.0
In [109]:
a.max()
Out[109]:
nan
In [110]:
np.nanmax(a)
Out[110]:
6.0
In [111]:
a.mean()
Out[111]:
nan
In [112]:
np.nanmean(a)
Out[112]:
3.0
In [113]:
a.shape
Out[113]:
(2, 3)

With masked array

In [114]:
ma = np.ma.masked_where(np.isnan(a), a)
ma
Out[114]:
masked_array(data =
 [[-- 2.0 3.0]
 [1.0 -- 6.0]],
             mask =
 [[ True False False]
 [False  True False]],
       fill_value = 1e+20)
In [115]:
ma.min()
Out[115]:
1.0
In [116]:
ma.max()
Out[116]:
6.0
In [117]:
ma.mean()
Out[117]:
3.0
In [118]:
ma.shape
Out[118]:
(2, 3)