Numpy


In [3]:
import numpy as np

Get info

In [11]:
np.info(np.array)
array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)

Create an array.

Parameters
----------
object : array_like
    An array, any object exposing the array interface, an object whose
    __array__ method returns an array, or any (nested) sequence.
dtype : data-type, optional
    The desired data-type for the array.  If not given, then the type will
    be determined as the minimum type required to hold the objects in the
    sequence.  This argument can only be used to 'upcast' the array.  For
    downcasting, use the .astype(t) method.
copy : bool, optional
    If true (default), then the object is copied.  Otherwise, a copy will
    only be made if __array__ returns a copy, if obj is a nested sequence,
    or if a copy is needed to satisfy any of the other requirements
    (`dtype`, `order`, etc.).
order : {'K', 'A', 'C', 'F'}, optional
    Specify the memory layout of the array. If object is not an array, the
    newly created array will be in C order (row major) unless 'F' is
    specified, in which case it will be in Fortran order (column major).
    If object is an array the following holds.

    ===== ========= ===================================================
    order  no copy                     copy=True
    ===== ========= ===================================================
    'K'   unchanged F & C order preserved, otherwise most similar order
    'A'   unchanged F order if input is F and not C, otherwise C order
    'C'   C order   C order
    'F'   F order   F order
    ===== ========= ===================================================

    When ``copy=False`` and a copy is made for other reasons, the result is
    the same as if ``copy=True``, with some exceptions for `A`, see the
    Notes section. The default order is 'K'.
subok : bool, optional
    If True, then sub-classes will be passed-through, otherwise
    the returned array will be forced to be a base-class array (default).
ndmin : int, optional
    Specifies the minimum number of dimensions that the resulting
    array should have.  Ones will be pre-pended to the shape as
    needed to meet this requirement.

Returns
-------
out : ndarray
    An array object satisfying the specified requirements.

See Also
--------
empty_like : Return an empty array with shape and type of input.
ones_like : Return an array of ones with shape and type of input.
zeros_like : Return an array of zeros with shape and type of input.
full_like : Return a new array with shape of input filled with value.
empty : Return a new uninitialized array.
ones : Return a new array setting values to one.
zeros : Return a new array setting values to zero.
full : Return a new array of given shape filled with value.


Notes
-----
When order is 'A' and `object` is an array in neither 'C' nor 'F' order,
and a copy is forced by a change in dtype, then the order of the result is
not necessarily 'C' as expected. This is likely a bug.

Examples
--------
>>> np.array([1, 2, 3])
array([1, 2, 3])

Upcasting:

>>> np.array([1, 2, 3.0])
array([ 1.,  2.,  3.])

More than one dimension:

>>> np.array([[1, 2], [3, 4]])
array([[1, 2],
       [3, 4]])

Minimum dimensions 2:

>>> np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])

Type provided:

>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j,  2.+0.j,  3.+0.j])

Data-type consisting of more than one element:

>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
>>> x['a']
array([1, 3])

Creating an array from sub-classes:

>>> np.array(np.mat('1 2; 3 4'))
array([[1, 2],
       [3, 4]])

>>> np.array(np.mat('1 2; 3 4'), subok=True)
matrix([[1, 2],
        [3, 4]])

Creating arrays

Create numpy array from python array

In [13]:
mylist = [1, 2, 3]
np.array(mylist)
Out[13]:
array([1, 2, 3])

Create an array of evenly (integers)

In [15]:
np.arange(0, 11)
Out[15]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
In [16]:
np.arange(0, 11, 2) # step size = 2
Out[16]:
array([ 0,  2,  4,  6,  8, 10])

Create an array of zeros

In [19]:
np.zeros(3)
Out[19]:
array([0., 0., 0.])
In [20]:
np.zeros((3,5))
Out[20]:
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])

Create an array of ones

In [22]:
np.ones((3,3))
Out[22]:
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

Create an array of evenly spaced values (number of samples)

In [25]:
np.linspace(0, 10, 3) # strat, end, numOfelements
Out[25]:
array([ 0.,  5., 10.])
In [29]:
np.linspace(0, 10, 20)
Out[29]:
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.        ])

Create an identity matrix

In [30]:
np.eye(3)
Out[30]:
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

Create an array with random values

In [32]:
np.random.rand(4) # uniform dist between 0..1
Out[32]:
array([0.22385888, 0.07560157, 0.87288616, 0.08668345])
In [33]:
np.random.rand(4, 3)
Out[33]:
array([[0.91094676, 0.52603371, 0.02150228],
       [0.75817747, 0.32939823, 0.56249911],
       [0.39122791, 0.35520005, 0.01180465],
       [0.27087758, 0.94996242, 0.62230727]])
In [34]:
np.random.randn(3, 3) # standard normal dist
Out[34]:
array([[ 2.46787661,  0.47370137, -1.19064979],
       [-0.42796227, -0.45917261, -0.38259313],
       [-1.18313231,  0.46606724,  1.47803722]])
In [38]:
np.random.randint(1, 100, 5) # start, end, numOfElements
Out[38]:
array([53, 33, 42, 23, 96])
In [40]:
np.random.randint(1, 100, (2,3))
Out[40]:
array([[42, 21, 96],
       [69, 22, 75]])

Reshaping arrays

In [46]:
arr = np.arange(25)
arr.shape
Out[46]:
(25,)
In [44]:
arr = arr.reshape(5, 5)
arr.shape
Out[44]:
(5, 5)
In [45]:
arr
Out[45]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
In [47]:
arr.dtype
Out[47]:
dtype('int64')

Slicing, Indexing

In [48]:
arr = np.arange(1, 11)
arr
Out[48]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

Selecting a simple value

In [49]:
arr[0]
Out[49]:
1

Value of a range

In [51]:
arr[2:5]
Out[51]:
array([3, 4, 5])
In [52]:
arr[:5] # start begining up to 5
Out[52]:
array([1, 2, 3, 4, 5])
In [53]:
arr[5:] # from index to the end
Out[53]:
array([ 6,  7,  8,  9, 10])

Broadcasting

In [54]:
arr
Out[54]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
In [56]:
arr[0:5] = 100
arr
Out[56]:
array([100, 100, 100, 100, 100,   6,   7,   8,   9,  10])

Slicing

In [58]:
arr = np.arange(1, 11)
arr
Out[58]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
In [61]:
slice_of_arr = arr[0:5] # a pointer to the original array
slice_of_arr
Out[61]:
array([1, 2, 3, 4, 5])
In [62]:
slice_of_arr[:] = 99 # indexing all elements
slice_of_arr
Out[62]:
array([99, 99, 99, 99, 99])
In [63]:
arr
Out[63]:
array([99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

Copy

In [65]:
arr_copy = arr.copy()
arr_copy
Out[65]:
array([99, 99, 99, 99, 99,  6,  7,  8,  9, 10])
In [66]:
arr_copy[:] = 100
arr_copy
Out[66]:
array([100, 100, 100, 100, 100, 100, 100, 100, 100, 100])
In [67]:
arr
Out[67]:
array([99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

More dimensions

In [68]:
arr_2d = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
arr_2d
Out[68]:
array([[ 5, 10, 15],
       [20, 25, 30],
       [35, 40, 45]])
In [69]:
arr_2d.shape # rows, cols
Out[69]:
(3, 3)
In [70]:
arr_2d[0] # first row
Out[70]:
array([ 5, 10, 15])
In [71]:
arr_2d[1, 1] # a value
Out[71]:
25
In [73]:
arr_2d[:2] # first two rows
Out[73]:
array([[ 5, 10, 15],
       [20, 25, 30]])
In [74]:
arr_2d[:2, 1:] # first two rows and last two cols
Out[74]:
array([[10, 15],
       [25, 30]])

Selecting more dimensions

In [95]:
arr = np.arange(32)
arr_3d = arr.reshape(8, 4)
arr_3d
Out[95]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23],
       [24, 25, 26, 27],
       [28, 29, 30, 31]])
In [98]:
arr_3d[0, ...] # first row and all the other dimensions
Out[98]:
array([0, 1, 2, 3])
In [100]:
arr_3d = arr.reshape(2, 4, 4)
arr_3d
Out[100]:
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15]],

       [[16, 17, 18, 19],
        [20, 21, 22, 23],
        [24, 25, 26, 27],
        [28, 29, 30, 31]]])
In [103]:
arr_3d[..., 0] # all dimensions and the first of the last one
Out[103]:
array([[ 0,  4,  8, 12],
       [16, 20, 24, 28]])

Conditional selection

In [76]:
arr = np.arange(1, 11)
arr
Out[76]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
In [77]:
arr > 4
Out[77]:
array([False, False, False, False,  True,  True,  True,  True,  True,
        True])
In [78]:
arr[arr > 4]
Out[78]:
array([ 5,  6,  7,  8,  9, 10])

Numpy operations

In [104]:
arr = np.arange(0, 10)
arr
Out[104]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [105]:
arr + 5
Out[105]:
array([ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
In [106]:
arr * 2
Out[106]:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
In [107]:
arr + arr
Out[107]:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
In [108]:
arr / arr
/usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in true_divide
  """Entry point for launching an IPython kernel.
Out[108]:
array([nan,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
In [109]:
1 / arr
/usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in true_divide
  """Entry point for launching an IPython kernel.
Out[109]:
array([       inf, 1.        , 0.5       , 0.33333333, 0.25      ,
       0.2       , 0.16666667, 0.14285714, 0.125     , 0.11111111])
In [110]:
np.sqrt(arr)
Out[110]:
array([0.        , 1.        , 1.41421356, 1.73205081, 2.        ,
       2.23606798, 2.44948974, 2.64575131, 2.82842712, 3.        ])
In [111]:
np.sin(arr)
Out[111]:
array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
       -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849])
In [112]:
arr.sum()
Out[112]:
45
In [113]:
arr.max()
Out[113]:
9

2 Dim

In [115]:
arr_2d  =np.arange(0, 25).reshape(5, 5)
arr_2d
Out[115]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
In [116]:
arr_2d.sum()
Out[116]:
300
In [119]:
arr_2d.sum(axis=0) # sum of the cols
Out[119]:
array([50, 55, 60, 65, 70])
In [120]:
arr_2d.sum(axis=1) # sum of the rows
Out[120]:
array([ 10,  35,  60,  85, 110])

Transpose

In [121]:
arr_2d.T # np.transpose(arr_2d) is the same
Out[121]:
array([[ 0,  5, 10, 15, 20],
       [ 1,  6, 11, 16, 21],
       [ 2,  7, 12, 17, 22],
       [ 3,  8, 13, 18, 23],
       [ 4,  9, 14, 19, 24]])

Flatten

In [122]:
np.ravel(arr_2d) # flatten
Out[122]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24])

Append

In [125]:
arr = np.arange(0, 5)
arr
Out[125]:
array([0, 1, 2, 3, 4])
In [128]:
np.append(arr, [5])
Out[128]:
array([0, 1, 2, 3, 4, 5])
In [129]:
np.insert(arr, 1, 5) # to the first index insert value 5
Out[129]:
array([0, 5, 1, 2, 3, 4])
In [130]:
np.delete(arr, [1])
Out[130]:
array([0, 2, 3, 4])