NUMPY
NUMPY
What is NumPy?
NumPy (Numerical Python) is a powerful Python library used for numerical computing. It
provides multi-dimensional arrays (ndarray) and a collection of mathematical functions to
operate on these arrays efficiently.
N-dimensional array (ndarray) – Faster and more memory-efficient than Python
lists.
Vectorized operations – Perform operations on entire arrays without loops.
Mathematical functions – Support for linear algebra, statistics, and random
number generation.
Broadcasting – Enables operations on arrays of different shapes without explicit
loops.
import numpy
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
#or like this arr = np.array((1, 2, 3, 4, 5))
#arr = np.array(5) one element added
# arr = np.array([[1, 2, 3], [4, 5, 6]]) 2D array
print(arr) # [1 2 3 4 5]
print(arr[0]) # accessing elements
print(type(arr)) # <class 'numpy.ndarray'>
import numpy as np
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(d.ndim) # checking the dimensions : 3
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr) #[[[[[1 2 3 4]]]]]
print('number of dimensions :', arr.ndim) #5
Slicing arrays
Slicing in python means taking elements from one given index to another given index. We
can also define the step, like this: [start:end:step].
If we don't pass start its considered 0 If we don't pass end its considered length of array in
that dimension If we don't pass step its considered 1
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5]) # [2 3 4 5]
print(arr[-3:-1]) # [5 6]
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
Shape of an array
NumPy arrays have an attribute called shape that returns a tuple with each index having the
number of corresponding elements.
import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape) # (2 4) 2 dimensions where first one having 2 elements (blue) and second
one having 4 (green)
Reshaping arrays
Reshaping means changing the shape of an array. The shape of an array is the number of
elements in each dimension. By reshaping we can add or remove dimensions or change
number of elements in each dimension.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3) #number of rows and numbers of cols we want , dimensions like
5*2 will result in errors
print(newarr)
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
print(arr.reshape(2, 4).base) # return original array, so it’s a view
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(2, 3, 2) #2 arrays that contains 3 arrays, each with 2 elements
print(newarr)
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
You are allowed to have one "unknown" dimension. Meaning that you do not have to specify
an exact number for one of the dimensions in the reshape method. Pass -1 as the value, and
NumPy will calculate this number for you. (We can not pass -1 to more than one dimension.)
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
newarr = arr.reshape(2, 2, -1)
print(newarr)
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
Iterating Arrays
for x in arr:
print(x)
for x in np.nditer(arr):
print(x)
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr) # [[1 2 5 6] [3 4 7 8]]
arr = np.concatenate((arr1, arr2), axis=0)
print(arr) # [[1 2] [3 4] [5 6] [7 8]]
arr = np.concatenate((arr1, arr2), axis=None)
print(arr) # [1 2 3 4 5 6 7 8]
array1, array2, ... → Arrays to concatenate (must have the same shape except along the
axis of concatenation).
axis → The dimension along which concatenation happens:
axis=0 → Concatenates along rows (vertically)
axis=1 → Concatenates along columns (horizontally)
axis=None → Flattens the arrays before concatenation
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=1)
print(arr) # [[1 4] [2 5] [3 6]]
numpy.stack() is used to join multiple arrays along a new axis. Unlike concatenate(), which
joins along existing axes, stack() creates a new dimension.
array1, array2, ... → Arrays to be stacked (must have the same shape).
axis → The dimension where a new axis is inserted:
o axis=0 → New dimension added as the first axis.
o axis=1 → New dimension added as the second axis.
o axis=2, axis=-1, etc., can be used for higher dimensions.
np.vstack((a, b)) → Equivalent to np.stack((a, b), axis=0), but only for
vertical stacking.
np.hstack((a, b)) → Equivalent to np.concatenate((a, b), axis=1), but only for
horizontal stacking.
np.dstack((a, b)) → Equivalent to np.stack((a, b), axis=2) (stacks along
height, which is same as depth
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.hstack((arr1, arr2))
print(arr) #[1 2 3 4 5 6]
arr = np.vstack((arr1, arr2))
print(arr)#[[1 2 3][4 5 6]]
arr = np.dstack((arr1, arr2))
print(arr) #[[[1 4][2 5][3 6]]]
Splitting array
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr) #[array([1, 2]), array([3, 4]), array([5, 6])]
If the array has less elements than required, it will adjust from the end accordingly.
newarr = np.array_split(arr, 3)
print(newarr[0])
print(newarr[1])
print(newarr[2])
# [1 2]
[3 4]
[5 6]
Arr1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr1 = np.hsplit(arr, 3)
print(newarr1)
#[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
Searching array
You can search an array for a certain value, and return the indexes that get a match.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 4, 4])
x = np.where(arr == 4)
print(x) #(array([3, 5, 6]),)
x = np.where(arr%2 == 0)
print(x) #(array([3, 5, 4]),)
Search Sorted
searchsorted() which performs a binary search in the array, and returns the index where the
specified value would be inserted to maintain the search order.
import numpy as np
arr = np.array([6, 7, 8, 9])
x = np.searchsorted(arr, 7)
print(x)
By default the left most index is returned, but we can give side='right' to return the right most
index instead.
To search for more than one value, use an array with the specified values.
Sort Arrays
This method returns a copy of the array, leaving the original array unchanged.
If you use the sort() method on a 2-D array, both arrays will be sorted.
import numpy as np
arr = np.array([3, 2, 0, 1])
print(np.sort(arr))
Filtering Arrays
Getting some elements out of an existing array and creating a new array out of them is
called filtering.
In NumPy, you filter an array using a boolean index list.
If the value at an index is True that element is contained in the filtered array, if the value at
that index is False that element is excluded from the filtered array.
import numpy as np
arr = np.array([41, 42, 43, 44])
x = [True, False, True, False]
newarr = arr[x]
print(newarr) #[41 43]
RANDOM
NumPy offers the random module to work with random numbers.
from numpy import random
x = random.randint(100)# random number from 0 to 100
print(x)
x = random.rand()# random float number from 0 to 1
print(x)
x=random.randint(100, size=(5)) #takes a size parameter- creates an arr of random numbers,
2 parameters- 2D array
print(x)
x = random.rand(5) # float array with 5 (size) floats
print(x)
x = random.choice([3, 5, 7, 9]) #randomly returns one element of the array
print(x)
x = random.choice([3, 5, 7, 9], size=(3, 5))# an array of 3 elements containing 5 elements
each (repeatative)
print(x)
Data Distribution
Data Distribution is a list of all possible values, and how often each value occurs.
Random