Introduction to NumPy

NumPy stands for Numerical Python. It is a Python library that is used in linear algebra and matrices. It was created by Travis Oliphant. It is open source and is free to use.

Installing NumPy
pip install numpy

Importing NumPy
import numpy

An array is an ordered collection of elements of the same type, stored contiguously in memory. The array object in NumPy is called ndarray.

Creating a 1D Array
arr = numpy.array([12, 5, 7, 18])

Creating a 2D Array
arr = numpy.array([[1, 2, 3], [2, 4, 5]])

Attributes of NumPy Array
arr.ndim gives the number of dimensions.
arr.shape gives the size of each dimension as a tuple.
arr.size gives the total number of elements in the array.
arr.dtype gives the data type of the array elements.
arr.itemsize gives the size in bytes of each element in the array.

Mentioning Data Type while Creating Arrays
arr = numpy.array([[1, 2, 3], [4, 5, 6]], dtype = float)

Creating Arrays with All Elements as Zeros
arr = numpy.zeros((3, 2))
Here, by default, the data type of all the elements is float.

Creating Arrays with All Elements as Ones
arr = numpy.ones((2, 3))
Here, by default, the data type of all the elements is float.

Creating Arrays with A Range
arr = numpy.arange(6)
Stores elements from 0 to 5 with an increment of 1.
arr = numpy.arange(5, 20, 2)
Stores elements from 5 to less than 20 with step value 2.

Slicing 1D Arrays
x = numpy.array([4, 7, 8, 2, 9, 5])
y = x[2, 4] #y becomes [8, 2]

Slicing 2D Arrays
x = numpy.array([[1, 2, 3], [4, 5, 6]])
a = x[0:2, 1] #accesses all elements of the 2nd column

b = x[0:2, 1:3] #accesses all elements of the 2nd and 3rd column

Reversing an Array
x = [1, 7, 2, 8, 5]
y = x[::-1]

Arithmetic Operations on Arrays
Corresponding elements in both the arrays can be added, subtracted, multiplied, divided, etc.
a1 = numpy.array([[1, 2], [2, 5]])
a2 = numpy.array([[5, 8], [7, 4]])
arr = a1 + a2

arr = a1 - a2
arr = a1 * a2
arr = a1 / a2
arr = a1 % a2 #remainder
arr = a1 ** 2 #exponentiation

Transpose
Transpose means converting rows into columns and columns into rows.
a1 = numpy.array([[1, 2, 5], [7, 4, 3]])
a2 = a1.transpose()

Sorting
arr = numpy.array([7, 2, 8, 1, 5])
arr.sort() #ascending order

arr = arr[::-1] #reversing leads to descending order
Sorting in 2D arrays can be done row-wise and column-wise. By default, it is done row-wise.
arr = numpy.array([[7, 2, 5], [8, 9, 3]])
arr.sort() #row-wise
arr.sort(axis = 0) #column-wise

Concatenating 1D Arrays
a1 = numpy.array([1, 2, 3])
a2 = numpy.array([3, 4, 5])
c = numpy.concatenate((a1, a2))

Concatenating 2D Arrays
a = numpy.array([[1, 2], [3, 4]])
b = numpy.array([[5, 6]])
c = numpy.concatenate((a, b)) #row-wise

d = numpy.array([7], [8])
c = numpy.concatenate((a, d), axis = 1) #column-wise

Reshaping Arrays
Reshaping arrays cannot be used to change the total number of elements in the array.
arr = numpy.arange(11, 21)
arr.reshape(5, 2)

Splitting 1D Array into Equal Parts
arr = numpy.array([1, 2, 3, 4, 5, 6])
a, b, c = numpy.split(arr, 3) #splits into 3 equal parts

Splitting 1D Array at Specific Indices
arr = numpy.array([1, 2, 3, 4, 5, 6])
a, b, c = numpy.split(arr, [2, 3]) #splits at indexes 2 and 3

Splitting 2D Array Row-Wise into 3 Equal Parts
x = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a, b, c = numpy.split(x, 3, axis = 0)

Splitting 2D Array Column-Wise into 3 Equal Parts
x = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a, b, c = numpy.split(x, 3, axis = 1)

Finding Maximum Element in the Array
x = numpy.array([7, 2, 8, 9, 5])
y = x.max()

a = numpy.array([[1, 5, 2], [7, 4, 6]])
b = a.max() #returns 7
b = a.max(axis = 1) #returns [5, 7]
b = a.max(axis = 0) #returns [7, 5, 6]

Finding Minimum Element in the Array