Numpy Cheat Sheet - Python for Data Science

Image Credit - Wikimedia Commons

            Numpy is the library used for scientific computing in python. It provides the ability to work with data at speed. It is the first library that you have to know in your Data Science learning journey.

      It is very obvious that you don't remember everything when you are learning anything for the first time. That's why i have bring Numpy Cheat Sheet for you guys. If you want more information you can refer numpy documentation.


A)  Array Creation

1. numpy.array -  you can create an array from a python list or tuple using array function.

Syntax:- numpy.array(objectdtype=None*copy=Trueorder='K'subok=Falsendmin=0like=None) 
>>> import numpy as np
>>> d1=np.array([2,3,4])   # Using List
>>> d1
array([2, 3, 4])
>>> t1=np.array((2,3,4,5))    # Using Tuple
>>> t1
array([2, 3, 4, 5])
>>>
>>> ######### 2D Array ############
>>> d2=np.array([[2,4,5],[6,7,8]])
>>> d2
array([[2, 4, 5],
       [6, 7, 8]])

2. numpy.arange - It creates an instance of ndarray with numerical ranges and evenly spaced values.

Syntax:- numpy.arange([start]stop[step]dtype=None*like=None)
>>> r1=np.arange(4)
>>> r1
array([0, 1, 2, 3])
>>> r2=np.arange(3,10)
>>> r2
array([3, 4, 5, 6, 7, 8, 9])
>>> r3=np.arange(1,10,2)
>>> r3
array([1, 3, 5, 7, 9])


3.numpy.linspace - It creates a ndarray of evenly spaced numbers over a specified interval.

Syntax:- numpy.linspace(startstopnum=50endpoint=Trueretstep=Falsedtype=Noneaxis=0)
>>> l1=np.linspace(2,4,num=8)
>>> l1
array([2.        , 2.28571429, 2.57142857, 2.85714286, 3.14285714,
       3.42857143, 3.71428571, 4.        ])
>>> l2=np.linspace(2,4,num=8,endpoint=False)
>>> l2
array([2.  , 2.25, 2.5 , 2.75, 3.  , 3.25, 3.5 , 3.75])
>>> l3=np.linspace(2,4,num=8,retstep=True)
>>> l3
(array([2.        , 2.28571429, 2.57142857, 2.85714286, 3.14285714,
       3.42857143, 3.71428571, 4.        ]), 0.2857142857142857)


4. numpy.zeros - It creates a ndarray of given shape and type filled with zeros.

Syntax:- numpy.zeros(shapedtype=floatorder='C'*like=None)
>>> z1=np.zeros(5)
>>> z1
array([0., 0., 0., 0., 0.])
>>> z2=np.zeros([2,3], dtype=int)
>>> z2
array([[0, 0, 0],
       [0, 0, 0]])
>>> z3=np.zeros([2,3,2], dtype=int)
>>> z3
array([[[0, 0],
        [0, 0],
        [0, 0]],

       [[0, 0],
        [0, 0],
        [0, 0]]])
>>> z4=np.zeros([2,3], dtype=[('x','float'), ('y','int')])
>>> print(z4)
[[(0., 0) (0., 0) (0., 0)]
 [(0., 0) (0., 0) (0., 0)]]

5. numpy.ones - It creates a ndarray of given shape and type filled with ones.

Syntax:- numpy.ones(shapedtype=Noneorder='C'*like=None)
>>> on1=np.ones(5)
>>> on1
array([1., 1., 1., 1., 1.])
>>> on2=np.ones([2,3], dtype=int)
>>> on2
array([[1, 1, 1],
       [1, 1, 1]])
>>> on3=np.ones([2,3], dtype=[('x','float'), ('y','int')])
>>> print(on3)
[[(1., 1) (1., 1) (1., 1)]
 [(1., 1) (1., 1) (1., 1)]]

6. numpy.full - It creates a ndarray of given shape and type with specified fill_value.

Syntax:- numpy.full(shapefill_valuedtype=Noneorder='C'*like=None)

>>> f1=np.full([2,2],50,dtype=int)
>>> f1
array([[50, 50],
       [50, 50]])
>>> f2=np.full([2,2],74.87)
>>> f2
array([[74.87, 74.87],
       [74.87, 74.87]])

7. numpy.eye - It creates a 2D array with ones on diagonal and zeros elsewhere.

Syntax:- numpy.eye(NM=Nonek=0dtype <class 'float'>order='C'*like=None)
>>> e1=np.eye(2,2, dtype=int)
>>> e1
array([[1, 0],
       [0, 1]])
>>> e2=np.eye(4,4,k=1)
>>> e2
array([[0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.],
       [0., 0., 0., 0.]])

8. Using random function - 

8.1 numpy.random.randint- It creates a ndarray of given shape and type from a random sample of given range.

Syntax:- random.randint(lowhigh=Nonesize=Nonedtype=int)

>>> r1=np.random.randint(100, size=10)
>>> r1
array([99, 70, 13, 92, 22, 16, 89, 52, 51, 53])
>>> r2=np.random.randint(100, size=(2,4))
>>> r2
array([[17, 11, 58, 88],
       [68, 48,  6, 35]])
>>> r3=np.random.randint(50,120, size=(2,4))
>>> r3
array([[ 50, 104,  72, 105],
       [115,  65, 111,  77]])

8.2 numpy.random.rand - It creates a ndarray of given shape with random samples from a uniform distribution over (0,1).

Syntax:- random.rand(d0d1...dn)

>>> f1=np.random.rand(3,4)
>>> f1
array([[0.60414313, 0.37280543, 0.04522737, 0.97094511],
       [0.79127581, 0.10914187, 0.92900321, 0.11353921],
       [0.5991773 , 0.91489596, 0.53929646, 0.44421889]])

8.3 numpy.random.choice - It creates a ndarray of given shape from a random samples of given 1-D array.

Syntax:- random.choice(asize=Nonereplace=Truep=None)

>>> c1=np.random.choice([6,10,99,4,6])
>>> c1
6
>>> c2=np.random.choice([6,10,99,4,6], size=(3,4))
>>> c2
array([[10, 99, 99, 10],
       [ 6, 99, 99,  6],
       [99,  6, 10,  4]])

9. using loadtxt - It creates a ndarray from data of the text file. Each row of the text file must have the same number of values.

Syntax:- numpy.loadtxt(fnamedtype=<class 'float'>comments='#'delimiter=Noneconverters=Noneskiprows=0usecols=Noneunpack=Falsendmin=0encoding='bytes'max_rows=None*like=None)
>>> import numpy as np
>>> from io import StringIO
>>> str1=StringIO("3 4 \n 5 6 \n 7 8")
>>> s1=np.loadtxt(str1)
>>> s1
array([[3., 4.],
       [5., 6.],
       [7., 8.]])


B) Attributes

1. ndarray.ndim - It returns the number of dimensions of the array.

>>> import numpy as np
>>> ar1=np.array([[2,3,4,5],[6,7,8,9]])
>>> ar1
array([[2, 3, 4, 5],
       [6, 7, 8, 9]])
>>> ar1.ndim
2


2. ndarray.shape - It returns the shape( Number of rows & coulmns) of an array.

>>> ar1.shape
(2, 4)


3. numpy.size - It returns the number of elements of an array. If axis=0, It returns the number of rows and If axis=1 then it returns the number fo coulmns.


Syntax:- numpy.size(arr, axis=None)

>>> np.size(ar1)
8
>>> np.size(ar1, axis=0)
2
>>> np.size(ar1, axis=1)
4


4. ndarray.type - It returns the data type of array's elements.

>>> ar1.dtype
dtype('int32')


5. ndarray.itemsize - It returns the size of one array element in bytes.

>>> ar1.itemsize
4


6. ndarray.nbytes - It returns total bytes consumed by the array.

>>> ar1.nbytes
32


C) Reshaping

1. ndarray.reshape - It returns an array of new shape with the same elements.

Syntax:- ndarray.reshape(shapeorder='C')
>>> import numpy as np
>>> ar1=np.arange(20)
>>> ar1
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
>>> d2_ar=ar1.reshape(4,5)
>>> d2_ar
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
>>> d3_ar=ar1.reshape(2,2,5)
>>> d3_ar
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9]],

       [[10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]]])


D) Joining

1. numpy.concatenate - It joins the sequence of array along a given axis. By default axis is Zero.

Syntax:- numpy.concatenate((a1a2...)axis=0out=Nonedtype=Nonecasting="same_kind")

>>> import numpy as np
>>> ar1=np.array([2,3,4])
>>> ar2=np.array([7,9,0])
>>> arr=np.concatenate((ar1,ar2))
>>> arr
array([2, 3, 4, 7, 9, 0])
>>>
>>>
>>> ar1=np.array([[2,4],[6,7]])
>>> ar2=np.array([[8,9],[2,1]])
>>> arr=np.concatenate((ar1,ar2), axis=1)
>>> arr
array([[2, 4, 8, 9],
      [6, 7, 2, 1]])


2. numpy.stack - It also joins the sequence of array along a given axis. By default axis is Zero.

Syntax:- numpy.stack(arraysaxis=0out=None)

>>> ar1=np.array([[2,1],[9,0]])
>>> ar2=np.array([[5,6],[3,8]])
>>> arr=np.stack((ar1,ar2), axis=1)
>>> arr
array([[[2, 1],
        [5, 6]],

       [[9, 0],
        [3, 8]]])


3. numpy.vstack - It joins the sequence of array's vertically.

Syntax:- numpy.vstack(tup)

>>> ar1=np.array([[2,1],[9,0]])
>>> ar2=np.array([[5,6],[3,8]])
>>> v_arr=np.vstack((ar1,ar2))
>>> v_arr
array([[2, 1],
       [9, 0],
       [5, 6],
       [3, 8]])


4. numpy.hstack - It joins the sequence of arrays horizontally.

Syntax:- numpy.hstack(tup)
>>> ar1=np.array([[2,1],[9,0]])
>>> ar2=np.array([[5,6],[3,8]])
>>> h_arr=np.hstack((ar1,ar2))
>>> h_arr
array([[2, 1, 5, 6],
       [9, 0, 3, 8]])


E) Splitting

1. numpy.vsplit - It divides the array into multiple sub arrays vertically ( row wise ).

Syntax:- numpy.vsplit(aryindices_or_sections)

>>> arr=np.array([[2,4,5],[0,1,7]])
>>> sp_ar=np.vsplit(arr,2)
>>> sp_ar
[array([[2, 4, 5]]), array([[0, 1, 7]])]


2. numpy.hsplit - It divides the array into multiple sub arrays horizontally ( Column wise ).

Syntax:- numpy.vsplit(aryindices_or_sections)
>>> arr=np.array([[2,4,5],[0,1,7]])
>>> h_ar=np.hsplit(arr,3)
>>> h_ar
[array([[2],
       [0]]), array([[4],
       [1]]), array([[5],
       [7]])]

F) Basic Operations

               Basic Operation includes arithmetic operations, scalar multiication and all.

>>> import numpy as np
>>> ar1=np.array([[2,1],[9,0]])
>>> ar2=np.array([[5,6],[3,8]])
>>> arr_sum=ar1+ar2
>>> arr_sum
array([[ 7,  7],
       [12,  8]])
>>> arr_sub=ar1-ar2
>>> arr_sub
array([[-3, -5],
       [ 6, -8]])
>>> arr_mul=ar1*ar2
>>> arr_mul
array([[10,  6],
       [27,  0]])


G) Universal Function

1. numpy.power - In this first array elements raised to powers from second array, element-wise.

Syntax:- numpy.power(x1x2/out=None*where=Truecasting='same_kind'order='K'dtype=Nonesubok=True[signatureextobj]) = <ufunc 'power'>
>>> import numpy as np
>>> ar1=np.array([2,2,2])
>>> ar2=np.array([2,3,4])
>>> p_arr=np.power(ar1,ar2)
>>> p_arr
array([ 4,  8, 16])


2. numpy.sum - It returns the sum of array elements over a given axis.

Syntax:- numpy.sum(aaxis=Nonedtype=Noneout=Nonekeepdims=<no value>initial=<no value>where=<no value>)
>>> ar2=np.array([2,3,4])
>>> s_arr=np.sum(ar2)
>>> s_arr
9


3. numpy.square - It returns element-wise square array of given array.

Syntax:- numpy.square(x/out=None*where=Truecasting='same_kind'order='K'dtype=Nonesubok=True[signatureextobj]) = <ufunc 'square'>
>>> ar2=np.array([2,3,4])
>>> sq_arr=np.square(ar2)
>>> sq_arr
array([ 4,  9, 16])


4. numpy.sqrt - It returns an array contains of non-negative square root of given array, element-wise.

Syntax:- numpy.sqrt(x/out=None*where=Truecasting='same_kind'order='K'dtype=Nonesubok=True[signatureextobj]) = <ufunc 'sqrt'>
>>> sqrt_arr=np.sqrt(sq_arr)
>>> sqrt_arr
array([2., 3., 4.])


H) Indexing

        Indexing helps us to access array elements by referring their index number. 
It creates an index object for every dimension.            That's why we need only one Index object for accessing elements of 1-D array.
For 2D array, we need 2 index objects. Likewise for ndarray, we need n index objects.
>>> ar1=np.array([2,3,4])
>>> ar1[0]
2
>>> ar2=np.array([[1,2,3],[4,5,6]])
>>> ar2[1,2]
6
>>> ar3=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
>>> ar3[1,1,2]
12


I) Slicing

       Slicing means taking a part of array from given one Index number to another index number.
  [start:end:step]
If we don't pass start its considered 0.
If we don't pass step its considered 1.

>>> ar1=np.arange(10)
>>> ar1[3:8]
array([3, 4, 5, 6, 7])
>>> ar1[1:8:2]
array([1, 3, 5, 7])
>>> ar2=np.array([[2,3,4,5],[6,7,8,9]])
>>> ar2[0:2,1:2]
array([[3],
       [7]])