Professional Documents
Culture Documents
CLASS XII
INFORMATICS PRACTICES
AISMV_XII_IP_POOJA THAKUR
NUMPY ARRAYS
NumPy stands for Numerical Python.It is the core library for scientific computing in Python.
Numpy array (ndarray) is a grid of values with same data type
It describes the collection of items of the same type.
Items in the collection can be accessed using a zero-based index.
Every item in an ndarray takes the same size of block in the memory.
Each element in ndarray is an object of data-type object (called dtype).
Numpy Array is indexed by a tuple of nonnegative integers
AISMV_XII_IP_POOJA THAKUR
NUMPY ARRAYS
Numpy Array does not support addition and Python list support adding and removing elements
removal of elements.
Arrays support element wise operations- vector Lists do not support element wise operations as the
operations elements are not of same type
AISMV_XII_IP_POOJA THAKUR
WAYS OF CREATING NUMPY ARRAYS
AISMV_XII_IP_POOJA THAKUR
CREATING 1-DIMENSIONAL ARRAY USING ARRAY()
One dimension array can be created using array method with list object with one
dimensional elements.
In above diagram a is an array ,it’s first element is at 0 index position ,next element is at 1 and so on
till last element at n-1 index position.
import numpy as np
a = np.array([100, 200, 300,400,500])
print(type(a))
print(a.shape)
AISMV_XII_IP_POOJA THAKUR
CREATING 2-DIMENSIONAL ARRAY USING ARRAY()
Axis 1
0 1 2 3 4
0 1 2 3 4 5
Axis 0 10 20 30 40 50
1
In above diagram a is a 2D array, where axis 0, refers to rows and axis 1 refers to columns
import numpy as np
a = np.array([[1,2,3,4,5],[10,20,30,40,50]])
print(type(a)) #data type
print(a.shape) #dimensions 2 rows and 5 columns
AISMV_XII_IP_POOJA THAKUR
CREATING ARRAY FROM A LIST USING ARRAY()
import numpy as np
L=[1,2,3,4,5]
arr1=np.array(L) output
print(arr1)
import numpy as np
L=[[1,2,3,4,5],[10,11,12,13,14]]
arr1=np.array(L)
output
print(arr1)
AISMV_XII_IP_POOJA THAKUR
CREATING ARRAY FROM A STRING
import numpy as np
data =np.fromstring(‘1 2 3 4 12', dtype=int, sep=' ‘)
print(data)
print(type(data)) # type of array
output
Note: the dtype and sep argument can be changed as per the requirement
AISMV_XII_IP_POOJA THAKUR
EMPTY()
To create empty arrays, empty() can be used. It creates an uninitialized array of specified shape and type.
Syntax:
np.empty(shape,dtype=<datatype>)
Example 1:
import numpy as np output
arr=np.empty([3,2],dtype=int)
print(arr)
If we don’t
specify the data
Example 2:
type, by default
output it is float
Note: The elements in the array show random/garbage values as they are not initialized. The values are displayed according to the data
type mentioned
AISMV_XII_IP_POOJA THAKUR while creating arrays. Default data type of arrays is float.
ZEROS()
zeros() creates a new array with all zeros as its elements as per the specified data type and size.
Syntax:
np.zeros(shape,dtype)
Note: the default
# creating 1-D array using zeros() data type float
arr1=np.zeros(5) output has been taken
print(arr1)
np.ones(shape,dtype)
Example 1: Creating 1D array
import numpy as np output
arr1=np.ones(5)
print(arr1)
Example 1:
import numpy as np
arr1=np.arange(2,7) output
print(arr1)
Example 2:
import numpy as np
arr2=np.arange(1,7,2, dtype=float) output
print(arr2) AISMV_XII_IP_POOJA THAKUR
LINSPACE()
linspace() creates an array of linearly spaced array elements based on the range and number of elements to be
generated.
Syntax:
<array>=np.linspace(start,stop,number of elements,dtype)
Example 1:
import numpy as np
output
a1=np.linspace(1,10,5)
print(a1)
Example 2:
import numpy as np output
a2=np.linspace(1,10,5,dtype=int)
AISMV_XII_IP_POOJA THAKUR
print(a2)
COPY()
An array can be created from another array.
If we assign a portion of the array to a new array, the new array refers to the parent array in memory.
If any changes are made in the new array, the changes will be reflected in the parent array as well.
To avoid disturbing the parent array, we need to make a copy of it using copy().
Example:
x=np.array([1,2,3,4,5])
y=x
z=np.copy(x)
x[0]=10 Note here, that the changes
made in x were reflected
print("x=",x) in y but not in z as z was
output created using copy()
print("y=",y)
print("z=",z)
RESHAPE()
x=np.arange(6)
print(x)
y=np.reshape(x,(2,3)) output
print(y)
AISMV_XII_IP_POOJA THAKUR
NUMPY- BASIC OPERATIONS
AISMV_XII_IP_POOJA THAKUR
LEARNING OBJECTIVE :
0 1 2 3 4 5 6 7 8 9
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
AISMV_XII_IP_POOJA THAKUR
BACKWARD INDEXING
SLICING IN 1D ARRAY USING FORWARD INDEXING
Consider the array x created below
x = np.arange(10)
print(x) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
output
x[4:7] output
AISMV_XII_IP_POOJA THAKUR
SLICING IN 1D ARRAY USING BACKWARD INDEXING
Consider the array ‘x’ created below:-
x = np.arange(10)
print(x) FORWARD INDEXING
<ndarray>[<start>:<stop>:<step> , : :
<start> <stop> <step> ]
COLUMN SLICING
ROW SLICING parameters
parameters
import numpy as np
a=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) Consider the array ‘a’
print(a)
Slicing syntax: <arrayname>[start row:end row,start col:end col]
Example:
a[1:2,0:4] # displays 2nd row and all columns output
output
1. hstack( ) :
Horizontal joining
2. vstack( ) :
Vertical joining
3. concatenate ():
Axis wise joining
AISMV_XII_IP_POOJA THAKUR
hstack( ) for 1-D
import numpy as np
A=np.array([1,2,3])
B=np.array([40,50,60])
C=np.hstack((A,B))
print(C) output
D=np.hstack((B,A))
print(D) output
AISMV_XII_IP_POOJA THAKUR
vstack( ) for 1-D
import numpy as np
A=np.array([1,2,3])
B=np.array([40,50,60])
C=np.vstack((A,B))
print(C) output
D=np.vstack((B,A))
print(D) output
AISMV_XII_IP_POOJA THAKUR
Concatenate( ) for 1-D
import numpy as np
x=np.array([1,2,3])
y=np.array([10,20,30])
z=np.concatenate((x,y))
print(z)
z=np.concatenate((y,x),axis=0)
print(z)
AISMV_XII_IP_POOJA THAKUR
hstack( ) for 2-D
AISMV_XII_IP_POOJA THAKUR
vstack for 2-D
AISMV_XII_IP_POOJA THAKUR
concatenate( ) for 2-D
1 2 3
4 5 6
7 8 9
10 11 12
1 4 7 10
2 5 8 11
3 6 9 12
OBTAINING SUBSETS OF ARRAYS
AISMV_XII_IP_POOJA THAKUR
HSPLIT()
AISMV_XII_IP_POOJA THAKUR
HSPLIT()
AISMV_XII_IP_POOJA THAKUR
VSPLIT()
AISMV_XII_IP_POOJA THAKUR
VSPLIT()
AISMV_XII_IP_POOJA THAKUR
SPLIT()
Split(Arr,[2,6])
AISMV_XII_IP_POOJA THAKUR
LEARNING OBJECTIVES
ARITHMETIC STATISTICAL
OPERATIONS ON OPERATIONS ON
ARRAYS ARRAYS
AISMV_XII_IP_POOJA THAKUR
ARITHMETIC OPERATIONS
When standard mathematical operations are used with arrays, they are applied on an element-by-element basis.
This means that the arrays should be the same size.
Arithmetic operations on Numpy arrays are fast and simple
When basic arithmetic operations are performed on two arrays, the operation is done on each corresponding pair
of elements
For instance, adding two arrays will result in the first element of first array to be added to first element of second
array, and so on.
Addition :- + or add()
Subtraction:- - or subtract()
Multiplication:- * or multiply()
Division:- / or divide()
AISMV_XII_IP_POOJA THAKUR
ARITHMETIC OPERATIONS ON 1 D ARRAY
import numpy as np
x=np.array([1,2,3,4])
X= [1 2 3 4]
print(x)
y=np.array([10,20,30,40]) Y=[10 20 30 40 ]
print(y)
z=x+y
print(z) output Z=[ 11 22 33 44 ]
AISMV_XII_IP_POOJA THAKUR
ARITHMETIC OPERATIONS (+,-,*,/,%,**) ON 1D ARRAY
import numpy as np
X= [ [1 2 3 4]
x=np.array([[1,2,3,4],[5,6,7,8]]) [5 6 7 8]]
print(x)
y=np.array([[10,20,30,40],[100,200,300,400]]) Y=[ [10 20 30 40 ]
100 200 300 400] ]
print(y)
z=x+y
print(z)
output
AISMV_XII_IP_POOJA THAKUR
To perform arithmetic operation on an array, a value can be added
CONSIDER THE using add() or + and for subtracting a value, subtract() or – can be
ARRAY (AR) used.
AISMV_XII_IP_POOJA THAKUR
To perform arithmetic operation on an array, a value can be
CONSIDER THE multiplied using multiply() or * and for dividing a value, divide() or /
ARRAY (AR) can be used.
AISMV_XII_IP_POOJA THAKUR
STATISTICAL OPERATIONS ON ARRAYS
Standard
median() count_nonzero() variance var())
deviation std()
AISMV_XII_IP_POOJA THAKUR
MAX()
import numpy as np
ar1=np.array([1,2,0,-5,6,3,-7,4,8])
ar2=np.array([[1,10,15],[4,7,20]])
print(ar1.max())
print(ar2.max())
print(ar2.max(axis=1)) output
print(ar2.max(axis=0))
Note: If axis=1, it returns the maximum value row wise i.e. Max of row 1(1,10,15=15) and max of row2 (4,7,20=20)
If axis=0, it returns maximum value column wise i.e. Column1(1,4=4), column2=(10,7=10), column3(15,20=20)
MIN()
import numpy as np
ar1=np.array([1,2,0,-5,6,3,-7,4,8])
ar2=np.array([[1,10,15],[4,7,20]])
print(ar1.min())
print(ar2.min())
print(ar2.min(axis=1)) output
print(ar2.min(axis=0))
Note: If axis=1, it returns the minimum value row wise i.e. min of row 1(1,10,15=1) and min of row2 (4,7,20=4)
If axis=0, it returns minimum value column wise i.e. Column1(1,4=1), column2=(10,7=7), column3(15,20=15)
SUM()
The sum() calculates and displays the sum of all elements of an array
import numpy as np
ar1=np.array([1,2,0,-5,6,3,-7,4,8])
ar2=np.array([[1,10,15],[4,7,20]])
print(ar1.sum())
print(ar2.sum()) output
print(ar2.sum(axis=1)) # sum of each row
print(ar2.sum(axis=0)) # sum of each column
AISMV_XII_IP_POOJA THAKUR
COUNT_NONZERO()
The count_nonzero() counts the number of non zero elements in a Numpy array.
Syntax: np.count_nonzero(<array>)
import numpy as np
ar1=np.array([1,2,0,-5,6,3,-7,4,8])
ar2=np.array([[1,0,15],[0,7,20]]) Returns count of non zero values in ar1
print(np.count_nonzero(ar1)) output Returns count of non zero values in ar2
print(np.count_nonzero(ar2)) Returns count of non zero values on each row
print(np.count_nonzero(ar2, axis=1)) Returns count of non zero values in each column
print(np.count_nonzero(ar2,axis=0))
AISMV_XII_IP_POOJA THAKUR
MEAN()
import numpy as np
ar1=np.array([1,2,0,-5,6,3,-7,4,8])
ar2=np.array([[1,0,15],[0,7,20]])
print(ar1.mean()) # mean of all elements of array ar1
print(ar2.mean()) # mean of all elements of array ar2
print(ar2.mean(axis=1)) # row wise mean of elements output
print(ar2.mean(axis=0)) # column wise mean of elements
AISMV_XII_IP_POOJA THAKUR
MEDIAN()
Median is defined as the value separating the higher half of a data sample from the lower half
Median is the middle value in a sorted array.
To find the median of elements of an array, the elements need to be sorted. The middle term, if number of terms is
odd is the median. In case the number of elements are even then the average of middle terms is the median.
Example:
age=np.array([12,23,14,15,6,19,21,18,19])
np.median(age) output
Since the number of terms are 9, and when we sort the elements, then we have 18 at the middle element.
AISMV_XII_IP_POOJA THAKUR
STANDARD DEVIATION
Standard Deviation (SD) is measured as the spread of data distribution in the given dataset. It is calculated using the
formula:
Standard deviation=√mean(abs(x-x.mean())2)
Numpy provides a built in function std() to calculate standard deviation for a given set of array elements
Example:
import numpy as np
ar1=np.array([1,2,0,-5,6,3,-7,4,8])
ar2=np.array([[1,0,15],[0,7,20]])
print(ar1.std())
output
print(ar2.std())
print(ar2.std(axis=1))
print(ar2.std(axis=0))
AISMV_XII_IP_POOJA THAKUR
VARIANCE
Variance measures the variation of a single random variable (like the height of a person in a population
Covariance is a measure of how much two random variables vary together (like the height of a person and weight of a
person in a population)
Variance=(xn-x)2/n where xn= value point, x=mean and n=number of observations
Syntax:
import numpy as np
ar1=np.array([1,2,0,-5,6,3,-7,4,8])
ar2=np.array([[1,0,15],[0,7,20]])
print(np.var(ar1))
print(np.var(ar2)) output
print(np.var(ar2,axis=0))
print(np.var(ar2,axis=1))
AISMV_XII_IP_POOJA THAKUR
AISMV_XII_IP_POOJA THAKUR