You are on page 1of 58

DATA HANDLING USING NUMPY

CREATING NUMPY ARRAYS

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

 An array consists of:


 Element- Each item stored in an array is called an
element
 Index- Each element in an array has a
location/position, known as index which is used to
identify the element. The indexing starts with 0.
 Axes: An array can be one dimensional (1D), two
dimensional (2D) and multidimensional (3D or 4D …).
The dimensions indicate the axes of an array. An array
with axis=0, defines a 1D array where the elements are
across the columns. In 2D array there are two axes,
axis 0 (rows)and axis 1(columns).
 Rank: Rank of an array is defined by the number of
axes it possesses.
 Shape: Shape of an array is defined as the number of
elements it contains. AISMV_XII_IP_POOJA THAKUR
NUMPY ARRAY VS PYTHON LISTS
NUMPY ARRAY LIST

Numpy Array works on homogeneous Python list are made up for


(same) types. heterogeneous (different) types.

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

Less memory consumption More memory consumption

Faster runtime execution. Runtime execution is comparatively slower than


Arrays.

AISMV_XII_IP_POOJA THAKUR
WAYS OF CREATING NUMPY ARRAYS

array() fromstring() empty() zeros() ones()

arange() linspace() copy() reshape()

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.

VALUES 100 200 300 400 500

INDEX a[0] a[1] a[2] a[3] a[4]

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()

 A 1-D array can be created from a python list using array():

import numpy as np
L=[1,2,3,4,5]
arr1=np.array(L) output
print(arr1)

 A 2-D array can be created from a python list using array():

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

To Create an array from a string, we can use fromstring()

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)

#creating 2-D array using zeros()


arr1=np.zeros([3,4], dtype=int) output
print(arr1
AISMV_XII_IP_POOJA THAKUR
ONES()
 ones() creates and returns a new array with all ones as array elements as per the specified type and size.
 Syntax:

np.ones(shape,dtype)
Example 1: Creating 1D array
import numpy as np output

arr1=np.ones(5)
print(arr1)

Example 2: creating 2D array


import numpy as np
arr1=np.ones([2,3], dtype=int) output

print(arr1) AISMV_XII_IP_POOJA THAKUR


FULL()
 To create an array with constant values, full() can be used
 Syntax:
 np.full(shape,constant value)
 Example 1: creating an array with constant integer values
import numpy as np
An array having 10 as
arr1=np.full([3,4],10) output constant value in all
print(arr1) elements is created

Example 2: creating an array with constant float values


import numpy as np
arr1=np.full([3,4],1.23) output
print(arr1) AISMV_XII_IP_POOJA THAKUR
ARANGE()

 An array can be created from a range using arange()


 Syntax:
 <arrayname>=np.arange(start,stop,step, dtype)

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()

 A 2D array can be created from a 1D array using reshape()


 Reshaping is changing the arrangement of items so that the shape of the array changes while maintaining the
dimensions .
 Reshaping cannot change the number of elements in an array

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 :

Numpy allows various operations to be performed on arrays and retrieving its


elements.
 Array Slices
 1-D Array Slices
 2-D Array Slices

 Joining or Concatenating Numpy Arrays


 hstack( )
 Vstack( )
 Concatenate( )

 Splitting Numpy Arrays- Subsets of Arrays


 Hsplit( )
 Vsplit( )
 Split( )
AISMV_XII_IP_POOJA THAKUR
UNDERSTANDING ARRAY INDEXES
The values stored in an array are indexed. So to access the values, we need to specify the index numbers.
The array elements are indexed from left to right (forward indexing), the first element has an index 0, in backward
indexing, the indexing starts from -1 (right to left)

Consider the array ‘x’ created below:-


x = np.arange(10)
print(x)
FORWARD INDEXING
0 1 2 3 4 5 6 7 8 9

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

#from start to 4th position

print(x[:5]) array([0, 1, 2, 3, 4])


output

#from 4th position to end


array([4, 5, 6, 7, 8, 9])
x[4:] output

#from 4th to 6th position

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

#access elements from 0 1 2 3 4 5 6 7 8 9


-8 to -2 index position
0 1 2 3 4 5 6 7 8 9
x[-8:-1]
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
X[-8:-1] X[-2:]

# access the second last element BACKWARD INDEXING


till last index
x[-2:]
AISMV_XII_IP_POOJA THAKUR
ARRAY SLICING FOR 2D ARRAYS

It is combination of row slicing and column slicing.

<ndarray>[<start>:<stop>:<step> , : :
<start> <stop> <step> ]
COLUMN SLICING
ROW SLICING parameters
parameters

First extract the rows as per row parameters specified


Now, on the extracted rows ,apply column slice to get the output
AISMV_XII_IP_POOJA THAKUR
SLICING IN 2D ARRAY

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

a[1:2,1:2] # displays 2nd row and 2nd column output

a[:2,0:2] # displays first 2 rows and columns output


AISMV_XII_IP_POOJA THAKUR
SLICING IN 2D ARRAY
Based on the array ‘a’
Consider the array ‘a’
predict the output of following slices

a[:3,::2] # displays elements of 0,1,2 rows and alternate column positions

output

a[::-1,::-1] # displays all elements in reverse order output

a[-2:-1,-2:] # displays elements of -2 row and -2&-1 column


output
AISMV_XII_IP_POOJA THAKUR
ARRAY JOINING:

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

Consider the Arrays Arr1 and Arr2 given :-

AISMV_XII_IP_POOJA THAKUR
vstack for 2-D

Consider the Arrays Arr1 and Arr2 given :-

AISMV_XII_IP_POOJA THAKUR
concatenate( ) for 2-D

• If axis = 1, column wise joining and


number of rows in both arrays must be
same
• If axis = 0, row wise joining and number
of columns in both array must be same
TRANSPOSE of a 2-D Array

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

 hsplit( )- splitting the array horizontally


 vsplit() - splitting the array vertically
 split( )- splitting of a Numpy array can be done both wise vertically or
horizontally by providing axis argument

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])

[0:2] [2:6] [6:]

The argument divides the array into 3 slice ranges


AISMV_XII_IP_POOJA THAKUR
SPLIT()
default is axis=0 (vsplit) Consider the
array AR
ARITHMETIC & STATISTICAL
OPERATIONS ON ARRAYS

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

>>> a = np.array([1,2,3], float)


>>> b = np.array([5,2,6], float)
>>> a + b
array([6., 4., 9.])
>>> a – b
array([-4., 0., -3.])
>>> a * b
array([5., 4., 18.])
>>> b / a
array([5., 1., 2.])
>>> a % b
array([1., 0., 3.])
>>> b**a
array([5., 4., 216.]) AISMV_XII_IP_POOJA THAKUR
ARITHMETIC OPERATIONS ON 2 D 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

Numpy provides functions to perform many useful statistical operations on arrays.

max() min() sum() mean()

Standard
median() count_nonzero() variance var())
deviation std()
AISMV_XII_IP_POOJA THAKUR
MAX()

 The max() finds the maximum element from 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.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()

 The min() finds the minimum element from 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.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()

 The mean() finds the average of elements of the 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]])
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

You might also like