Numpy Tutorial In Python

0 Comments

NumPy is a Python package. It stands for ‘Numerical Python’. A library used for working, processing with arrays and consisting of multidimensional array objects. It also perform operations related to linear algebra, fourier transform, and matrices.

NumPy aims to provide an array object that is up to 50x faster that traditional Python lists.

The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy.

import numpy as npnumpy.__version__
li=[[2,3],[65,78],[90,57]]
arr2=np.array(li)
print(arr2)
type(arr2)
list1 = [10,20,30,40,50,60]
list1
print(type(list1))

#Convert list to Numpy Array
arr1 = np.array(list1)
arr1

# Display type of an object
print(type(arr1))

#Datatype of array
print(type(arr1))
#1D array
a = np.array([[1, 2], [3, 4]]) 
#2D array
a = np.array([1, 2, 3,4,5], ndmin = 2) 
#3D array
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

Multidimensional Array

multi_arr=np.array([[2,3],[4,6],[9,4],[7,8]]) print(multi_arr) 
print("Dimension: ",multi_arr.ndim) print("Shape: ",multi_arr.shape)

The arange(start,stop,step) function returns an array with sequence of numbers between the mentioned intervals.

# Generate evenly spaced numbers (space =1) between 0 to 10
np.arange(0,10)

# Generate numbers between 0 to 100 with a space of 10
np.arange(0,100,10)

#Shape of Array
arr3 = np.arange(0,10)
print(arr3.shape)
print(arr3)

# Size of array
print(arr3.size)

# Dimension 
print(arr3.ndim)

# Length of array
print(len(arr3))

# Generate an array of zeros
print(np.zeros(10))

# Generate an array of ones with given shape
print(np.ones(10))

linspace() will give us linearly spaced elements by figuring out the spacing on it’s own.

np.linspace(10,20,11)

Array Operations

arr2 = np.arange(1,20)
print(arr2)

# Sum of all elements in an array
arr2.sum()

# Cumulative Sum
np.cumsum(arr2)

# Find Minimum number in an array
arr2.min()

# Find MAX number in an array
arr2.max()

# Find INDEX of Minimum number in an array
arr2.argmin()

# Find INDEX of MAX number in an array
arr2.argmax()

# Find mean of all numbers in an array
arr2.mean()

# Find median of all numbers present in arr2 
np.median(arr2)

# Variance
np.var(arr2)

# Standard deviation
np.std(arr2)

# Calculating percentiles
np.percentile(arr2,70)

# 10th & 70th percentile
np.percentile(arr2,[10,70])

Operations on a 2D Array

a = np.array([[1,2,3,0] , [5,6,7,22] , [10 , 11 , 1 ,13] , [14,15,16,3]])

# SUM of all numbers in a 2D array
a.sum()

a.max()

a.min()
# Column wise mimimum value 
np.amin(a, axis=0)

# Row wise mimimum value 
np.amin(a, axis=1)

# Mean of all numbers in a 2D array
a.mean()

# Mean
np.mean(a)

# Median
np.median(a)

#variance
np.var(a)

#standard deviation
np.std(a)

# Enumerate for Numpy 2D Arrays
for index, value in np.ndenumerate(A):
    print(index, value)

Reading Array Elements

ar = np.arange(1,20)
ar

# Replace EVEN numbers with ZERO
rep1 = np.where(ar % 2 == 0, 0 , ar)
print(rep1)

ar2 = np.array([10, 20 , 30 , 10 ,10 ,20, 20])
ar2

p2 = np.arange(0,100,10)
p2

# Replace values at INDEX loc 0,3,5 with 33,55,99
np.put(p2, [0, 3 , 5], [33, 55, 99])
p2

Missing Values in an array

a = np.array([10 ,np.nan,20,30,60,np.nan,90,np.inf])

# Search for missing values and return as a boolean array
np.isnan(a)

# Replace all missing values with 99
a[np.isnan(a)] = 99
a

# Check if array has any NULL value
np.isnan(a).any()

Stacking

The reshape() function is used to give a new shape to an array without changing its data.

a = np.zeros(20).reshape(2,-1)
b = np.repeat(1, 20).reshape(2,-1)
np.vstack([a,b])
np.hstack([a,b])

Common items between two Arrays

c1 = np.array([10,20,30,40,50,60])
c2 = np.array([12,20,33,40,55,60])
np.intersect1d(c1,c2)
np.setdiff1d(c1,c2) # Remove common elements of C1 & C2 array from C1

a = np.array([1,2,3,6,8])
b = np.array([10,2,30,60,8])

np.where(a == b) # returns the indices of elements in an input array where the given #condition is satisfied.

#ISIN()
a = np.array([10,20,30,40,50,60,70])
np.isin(a, [11,20])

Sort & Reverse

#Reverse Array

a4 = np.arange(10,30)

np.flip(a4)
a = np.array([10,5,2,22,12,92,17,33])

# Sort array in ascending order
np.sort(a)
# Sort along rows
np.sort(a3,axis =1)
# Sort along columns
np.sort(a3,axis =0)

# unique numbers in an array
b = np.array([10,10,10,20,30,20,30,30,20,10,10,30,10])
np.unique(b)

Flatten() vs Ravel()

A multi-dimensional array can be flattened using flatten() and ravel() functions of the numpy module. Both these functions work in the same manner, the only difference is that the ravel function is actually a reference to the parent array, that is , any changes done to the array created using ravel function is reflected back in the orginal/parent array. Whereas the flatten function on the other hand creates a copy of the original array.
But ravel() function is memory efficient since it does not create a copy of the original array

ar=np.random.randint(1,10,size=(2,6))

ar.flatten()
ar.ravel()

That’s all for today. Thank you for reading!


Leave a Reply

Your email address will not be published.