Sujay Goswami
9 min readAug 3, 2019

NumPy Library

NumPy is used for numerical and scientific calculation in Python(programming language). High performance array objects are provided by this library. It is a very important library on which almost every data science or machine learning Python packages such as SciPy (Scientific Python), Mat−plotlib (plotting library), Scikit-learn, etc depends on to a reasonable extent.

In NumPy, dimensions are called axes. The number of axes is called rank. NumPy’s array class is called ndarray. The representation of an array’s dimensions is cal

led its shape.

Examples:

[1][2]
[2][3]
[5][6]

Here the shape is (3,2). Code snippet-

import numpy as np

arr = np.array([[1,2],
[2,3],
[5,6]])

print("array is of type ", type(arr))
print("no. of dimensions ", arr.ndim)
print("shape of array ", arr.shape)
print("size of array ", arr.size)
print("array stores elements of type ", arr.dtype)
Output-
array is of type <class ‘numpy.ndarray’>
no. of dimensions 2
shape of array (3, 2)
size of array 6
array stores elements of type int32
Process finished with exit code 0

Creating Arrays

There are several ways to make a multi-dimensional array:

  1. Using Arange Function : We can create a one-dimensional array with the help of arange function(np.arange) such that the elements of it are are stored as whole numbers till the value we want. Code snippet-
import numpy as np
array = np.arange(20)
print(array)
Output-
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
Process finished with exit code 0

The shape is mentioned within brackets of function mentioned above.

2. Zero/One Matrices: These matrices are initially containing only one element in all entries. Sometimes, when the size of the array is known but it’s elements are unknown we use the functions np.zeros and np.ones resptectively. Code snippet-

import numpy as np
array0 = np.zeros(2)
arr0 = np.zeros((2, 2))

array1 = np.ones(2)
arr1 = np.ones((2, 2))


print(array0)
print(arr0)
print (array1)
print (arr1)
Output-
[0. 0.]
[[0. 0.]
[0. 0.]]
[1. 1.]
[[1. 1.]
[1. 1.]]
Process finished with exit code 0

As we can see, the shape is specified within brackets of the respective functions mentioned above.

3. Empty function array

An array can also be initialised with random values with the help of np.empty. Code snippet-

import numpy as np
array0 = np.empty(2)
arr0 = np.empty((2, 2))

print(array0)
print(arr0)
Output-
[2.66032208e-048 1.21828703e-304]
[[1.25972494e-311 1.25972494e-311]
[1.25972494e-311 1.25972494e-311]]
Process finished with exit code 0

The random elements are actually generated depending on the state of the memory.

4. Linspace

This function allows us to enter values at equal intervals. It requires starting and ending points with the number of intervals in between. For example: start=0, end=100, intervals= 5. The array would come out to be as[0, 25, 50, 75, 100]. Code snippet-

import numpy as np

array = np.linspace(0, 100, num=5)
print(array)
Output-
[ 0. 25. 50. 75. 100.]
Process finished with exit code 0

Reshape Function

This is used to covert a one dimension array into multidimensional arrays. The new array’s dimensions are factors of the number of elements from the 1D array (and when they are multiplied we get this number). Code snippet-

import numpy as np

array = np.arange(20)
array = array.reshape(5, 4)
print(array)
Output-
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
Process finished with exit code 0

Flatten function-

This function converts a multidimensional array into a one dimensional array. Code snippet-

import numpy as np

x = np.array([[1, 2], [3, 4]])
x = x.flatten()
print(x)
Output-
[1 2 3 4]
Process finished with exit code 0

Array Indexing

(1)Slicing- It is used to chop an array, specifying a start(included) point and an ending(excluded) point. Code snippet-

import numpy as np

x = np.array([[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12]])
print(x[1:4])
print(x[1:])
print(x[:4])
print(x[:-1])
print(x[:2, ::2])
Output-
[[ 7 8 9 10 11 12]]
[[ 7 8 9 10 11 12]]
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
[[1 2 3 4 5 6]]
[[ 1 3 5]
[ 7 9 11]] #alternate 2
Process finished with exit code 0

(2)Integer array indexing- In this, specific elements of arrays are displayed by mapping the required dimension. (one one mapping). Code snippet-

import numpy as np

x = np.array([[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12]])

print(x[[1, 0], [2, 3]])
Output-
[9 4]
Process finished with exit code 0

Basic Operations

Single array operations: We can perform any arithmetic operation on an array and it will apply the same on all individual elements of it. Code snippet-

import numpy as np

x = np.array([[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12]])

print(x+2, x-2, x*2, x**2)
Output-
[[ 3 4 5 6 7 8]
[ 9 10 11 12 13 14]] [[-1 0 1 2 3 4]
[ 5 6 7 8 9 10]] [[ 2 4 6 8 10 12]
[14 16 18 20 22 24]] [[ 1 4 9 16 25 36]
[ 49 64 81 100 121 144]]
Process finished with exit code 0

Unary Operators: These are used when only the array is the only oppernd. Many unary operations are provided as a method of ndarray class. Some functions are min, max, sum… Code snippet-

import numpy as np

x = np.array([[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12]])

print("max=", x.max(), "row-wise max=", x.max(axis=1), "columnwise max=", x.max(axis=0))
print("min=", x.min(), "row-wise min=", x.min(axis=1), "columnwise min=", x.min(axis=0))

print("sum=", x.sum(), "row-wise sum=", x.sum(axis=1), "columnwise sum=", x.sum(axis=0))
Output-
max= 12 row-wise max= [ 6 12] columnwise max= [ 7 8 9 10 11 12]
min= 1 row-wise min= [1 7] columnwise min= [1 2 3 4 5 6]
sum= 78 row-wise sum= [21 57] columnwise sum= [ 8 10 12 14 16 18]
Process finished with exit code 0

Binary Operations- These are used when two operands are present. Like sum, product of array and matrices. Code snippet-

import numpy as np

x = np.array([[1, 0],
[0, 1]])
y = np.array([[2, 1],
[4, 1]])

print("x+y=\n", x+y)
print("x-y=\n", x-y)
print("x*y=\n", x*y)
print("x.y=\n", x.dot(y))
Output-
x+y=
[[3 1]
[4 2]]
x-y=
[[-1 -1]
[-4 0]]
x*y=
[[2 0]
[0 1]]
x.y=
[[2 1]
[4 1]]
Process finished with exit code 0

Universal functions:

These are provided for some more mathematical functions. Trigonometric, rithmetic, compelex, statistical functions are covered by this. Numpy provides various such functions.

Trigonometric- sin(sine), cos(cosine), tan(tangent), arcsin(sin inverse), arccos(cos inverse), arctan(tan inverse). hypot(hypotenuse of triangle), sinh(hyperbolic sin), cosh(hyperbolic cos), tanh(hyperbolic tan), deg2rad and rad2deg (for conversions from degrees to radians and vice versa). Code snippet-

import numpy as np
#array of angles
ang = np.array([0, 30, 45, 60, 90])
val = np.array([0, 1])
print('Radian angle array=', np.deg2rad(ang))
print('sin of angle array=', np.sin(ang))
print('sin inverse array=', np.rad2deg(np.arcsin(val)))
print('hypotenuse of right triangle is ', np.hypot(3, 4))
Output-
Radian angle array= [0. 0.52359878 0.78539816 1.04719755 1.57079633]
sin of angle array= [ 0. -0.98803162 0.85090352 -0.30481062 0.89399666]
sin inverse array= [ 0. 90.]
hypotenuse of right triangle is 5.0
Process finished with exit code 0

Statistical- There are functions used to calculate mean, median, variance, etc…


import numpy as np

# construct a weight array
weight = np.array([50.7, 52.5, 50, 58, 55.63, 73.25, 49.5, 45])

# minimum and maximum
print('Minimum and maximum weight of the students: ')
print(np.amin(weight), np.amax(weight))

# range of weight i.e. max weight-min weight
print('Range of the weight of the students: ')
print(np.ptp(weight))

# percentile
print('Weight below which 70 % student fall: ')
print(np.percentile(weight, 70))

# mean
print('Mean weight of the students: ')
print(np.mean(weight))

# median
print('Median weight of the students: ')
print(np.median(weight))

# standard deviation
print('Standard deviation of weight of the students: ')
print(np.std(weight))

# variance
print('Variance of weight of the students: ')
print(np.var(weight))

# average
print('Average weight of the students: ')
print(np.average(weight))
Output-
Minimum and maximum weight of the students:
45.0 73.25
Range of the weight of the students:
28.25
Weight below which 70 % student fall:
55.317
Mean weight of the students:
54.3225
Median weight of the students:
51.6
Standard deviation of weight of the students:
8.052773978574091
Variance of weight of the students:
64.84716875
Average weight of the students:
54.3225
Process finished with exit code 0

Bitwise functions — These accept integer values as input and perform bitwise operations on binary representation of those elements.

import numpy as np

even = np.array([0, 2, 4, 6, 8, 16, 32])
odd = np.array([1, 3, 5, 7, 9, 17, 33])

# bitwise_and
print('bitwise_and of two arrays: ')
print(np.bitwise_and(even, odd))

# bitwise_or
print('bitwise_or of two arrays: ')
print(np.bitwise_or(even, odd))

# bitwise_xor
print('bitwise_xor of two arrays: ')
print(np.bitwise_xor(even, odd))

# invert or not
print('inversion of even no. array: ')
print(np.invert(even))

# left_shift
print('left_shift of even no. array: ')
print(np.left_shift(even, 1))

# right_shift
print('right_shift of even no. array: ')
print(np.right_shift(even, 1))
Output-
bitwise_and of two arrays:
[ 0 2 4 6 8 16 32]
bitwise_or of two arrays:
[ 1 3 5 7 9 17 33]
bitwise_xor of two arrays:
[1 1 1 1 1 1 1]
inversion of even no. array:
[ -1 -3 -5 -7 -9 -17 -33]
left_shift of even no. array:
[ 0 4 8 12 16 32 64]
right_shift of even no. array:
[ 0 1 2 3 4 8 16]
Process finished with exit code 0

Broadcasting

Usually operations between two arrays take place when both have the same shape. But this constraint can be overcome with the help of broadcasting or treatment of arrays with two different sizes. Rule- The size of the trailing axes for both arrays in operation must be either the same size or one of them must be one.

import numpy as np

a = np.array([1.0, 2.0, 3.0])

b = 3
print(a * b)
c = [3, 3, 3]
print(a * c)

a1 = np.array([0.0, 10.0, 20.0, 30.0])
b1 = np.array([0.0, 1.0, 2.0])

print(a1[:, np.newaxis] + b1)
Output-
[3. 6. 9.]
[3. 6. 9.]
[[ 0. 1. 2.]
[10. 11. 12.]
[20. 21. 22.]
[30. 31. 32.]]
Process finished with exit code 0

In the above example, a*b is a much better process as its faster because numpy doesn’t create copies of the single ineteger value 3. Hence, saving memory. (np.newaxis converts array into a row/column vector)

Date/Time

Numpy can input array data in the date time form. The data type is called ‘datetime64’. We can use arange function which does the exact same sorting with date entries. Even arithmetic operations are possible. Code snippet —

import numpy as np

# creating a date
today = np.datetime64('2019-08-03')
print("Date is:", today)
print("Year is:", np.datetime64(today, 'Y'))

# creating array of dates in a month
dates = np.arange('2019-08', '2019-09', dtype='datetime64[D]')
print("\nDates of August, 2019:\n", dates)
print("Today is August:", today in dates)

# arithmetic operation on dates
dur = np.datetime64('2017-05-22') - np.datetime64('2016-05-22')
print("\nNo. of days:", dur)
print("No. of weeks:", np.timedelta64(dur, 'W'))
Output-
['2019-08-01' '2019-08-02' '2019-08-03' '2019-08-04' '2019-08-05'
'2019-08-06' '2019-08-07' '2019-08-08' '2019-08-09' '2019-08-10'
'2019-08-11' '2019-08-12' '2019-08-13' '2019-08-14' '2019-08-15'
'2019-08-16' '2019-08-17' '2019-08-18' '2019-08-19' '2019-08-20'
'2019-08-21' '2019-08-22' '2019-08-23' '2019-08-24' '2019-08-25'
'2019-08-26' '2019-08-27' '2019-08-28' '2019-08-29' '2019-08-30'
'2019-08-31']
Today is August: True
No. of days: 365 days
No. of weeks: 52 weeks
Dates in sorted order: ['2016-10-13' '2017-02-12' '2019-05-22']Process finished with exit code 0

Linear Algebra

Numpy provides several functions of linear algebra. We can find rank, determinant, trace etc of an array; eigenvalues of matrices; matrix product and exponentation and solve linear/tensor equation, etc…Code snippet-

import numpy as np

A = np.array([[6, 1, 1],
[4, -2, 5],
[2, 8, 7]])

print("Rank of A:", np.linalg.matrix_rank(A))

print("\nTrace of A:", np.trace(A))

print("\nDeterminant of A:", np.linalg.det(A))

print("\nInverse of A:\n", np.linalg.inv(A))

print("\nMatrix A raised to power 2:\n", np.linalg.matrix_power(A, 2))
Output-
Rank of A: 3
Trace of A: 11Determinant of A: -306.0Inverse of A:
[[ 0.17647059 -0.00326797 -0.02287582]
[ 0.05882353 -0.13071895 0.08496732]
[-0.11764706 0.1503268 0.05228758]]
Matrix A raised to power 2:
[[42 12 18]
[26 48 29]
[58 42 91]]
Process finished with exit code 0

Equation solving-

import numpy as np

# coefficients
a = np.array([[1, 2], [3, 4]])
# constants
b = np.array([8, 18])

print("Solution of linear equations:", np.linalg.solve(a, b))
Output-
Solution of linear equations: [2. 3.]
Process finished with exit code 0