0% found this document useful (0 votes)
439 views14 pages

IP Class-XI Chapter-9 NOTES

1. NumPy is a Python library used for working with multi-dimensional arrays and matrices for numerical computing. It contains functions for scientific computing including linear algebra, Fourier transforms, and random number generation. 2. NumPy arrays can have multiple dimensions. 1D arrays represent vectors, 2D arrays represent matrices, and higher dimensional arrays exist. Arrays can be created, sliced, indexed, and have mathematical operations applied element-wise. 3. NumPy provides multiple ways to create arrays including from lists, using functions like zeros, ones, and linspace. Arrays support slicing, indexing, copying, reshaping, and mathematical operations like addition and multiplication.

Uploaded by

RituNagpal Ipis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
439 views14 pages

IP Class-XI Chapter-9 NOTES

1. NumPy is a Python library used for working with multi-dimensional arrays and matrices for numerical computing. It contains functions for scientific computing including linear algebra, Fourier transforms, and random number generation. 2. NumPy arrays can have multiple dimensions. 1D arrays represent vectors, 2D arrays represent matrices, and higher dimensional arrays exist. Arrays can be created, sliced, indexed, and have mathematical operations applied element-wise. 3. NumPy provides multiple ways to create arrays including from lists, using functions like zeros, ones, and linspace. Arrays support slicing, indexing, copying, reshaping, and mathematical operations like addition and multiplication.

Uploaded by

RituNagpal Ipis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CHAPTER-9

Data Handling using NUMPY


DATA AND ITS PURPOSE
Data can be defined as a systematic record of a particular quantity. Data is a
collection of raw facts and figures to be used for a specific purpose such as a survey
or analysis. When data is arranged in an organized form, can be called information.

NUMPY- Numpy stands for “Numerical python”.


NumPy is a package that contains several classes, functions, variables etc. to deal
with scientific calculations in Python. NumPy is useful to create and process single
and multi-dimensional arrays. In addition, NumPy contains a large library of
mathematics like linear algebra functions and Fourier transformations.
NumPy array can accept only one type of elements. We cannot store different data
types into same arrays.

ARRAY STRUCTURE IN NUMPY

PYTHON AND NUMPY


For working with NumPy, we should first import NumPy module into our Python
program.
Following line of code is use to import NumPy in the python program.
import numpy
or
import numpy as <<name>>
TYPES OF ARRAY IN NUMPY
An array in NumPy is of the following types-
1. 1D Array
2. 2D Array
3. N-Dimension Array

1D Array- One dimensional array contains elements only in one dimension. In


other words, the shape of the NumPy array should contain only one value in the
tuple.
.

5 6 9 4 3 1

A simple program to implement one dimensional array using NumPy.


Example:
import numpy
a = numpy.array([10,20,30,40,50])
print(a)
Output: [10,20,30,40,50]

Example:
import numpy as np
a = np.array([10,20,30,40,50])
print(a)
Output: [10, 20, 30, 40, 50]

Note: If we use the from statement then there is no need to add anything in front of array
function.
Example:
from numpy import *
a = array([10, 20,30,40,50])
print(a)
Output: [10, 20,30,40,50]

IMPLEMENTATION OF 1D ARRAY IN NUMPY


Creating array in NumPy can be done in several ways. Some of the important ways are:
i. Using array( ) function
ii. Using linspace( ) function
iii. Using arange( ) function
iv. Using zeros( ) and ones( ) functions

1. Using array( ) function


Using this function, we can create array of any data type, but if no data type is
mentioned, then default data type will be the "int"
Example:
from numpy import *
Arr=array([10,20,30,40,50], int)
OR
arr = array([10,20,30,40,50])

While creating array if one of the values in the specified list belongs to float, then all
the values will be converted to float by default.
Example:
from numpy import *
a = array([10,30,40.5, 50,100])
print(a)
Output: [10.0,30.0,40.5,50.0,100.0]

2. linspace( ) Function
The linspace() function is used to create an array with evenly spaced points between a
starting and ending point.
Syntax- linspace(start, stop, N) Output:
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
Example:
import numpy as np
a = np.linspace(1,10,10)
print(a)
 Start represents the starting  N represents the number of parts, into
number. which the elements should be divided.
 Stop represents the ending element.
3. arange( ) Function
The arange( ) function in NumPy is same as range( ) function in Python. It also generates a
list of numbers.
Syntax- arange(start, stop, stepsize)

arange(10) – will create an array with values [0,1,2,3,4,5,6,7,8,9]


arange(5,10) – will create an array with values [5, 6,7,8,9]
arange(10,1,-1) – will create an array with values [10,9,8,7,6,5,4,3,2]

Example:
import numpy as np Output:
a = np.arange(10) [0,1,2,3,4,5,6,7,8,9]
b = np.arange(5,10) [5,6,7,8,9]
c = np.arange(10,1,-1) [10, 9, 8, 7, 6, 5, 4, 3, 2]
print(a)
print(b)
print(c)

4. Creating array using ones( ) and zeros( ) functions


We can use zeros( ) function to create an array with all zeros.
The ones( ) function will is useful to create an array with all ones.
Syntax -
zeros(n, datatype)
ones(n, datatype)

Note: If datatype is missing then the default value will be float.

zeros(5) – will create an array with five zero values.


ones(5) – will create an array with five 1 values.

Example:
import numpy as np
K = np.zeros(5) Output:
R = np.ones(5) [0. , 0. , 0. , 0. , 0.]
print(K) [1. , 1. , 1. , 1. , 1.]
print(R)
SLICING in 1-D ARRAY
Syntax-
 Arrayname[start : stop : step_size]
 The default value of step size is “1”

-5 -4 -3 -2 -1
6 7 8 9 23
0 1 2 3 4
Example:
A[:5] will give [6 7 8 9 23]
A[::2] will give [6 8 23]
A[-1:-5:-1] will give [23 9 8 7]
A[2:-2] will give [8]

MATHEMATICAL OPERATIONS ON ARRAYS


It is possible to perform various Mathematical operations like addition,
subtraction, division etc. on the elements of NumPy arrays. The functions of
math module can be applied to the elements of any array.

import numpy as np
K = np.array([10, 20, 30, 40,50])
K = K+5 Add 5 to the Array
print(k)
K = K-5 Subtract 5 from each value of Array
print(k)
K = K*5 Multiply array by 5 Output:
print(k) [15 25 35 45 55]
K = K/5 Divide Array by 5 [10 20 30 40 50]
[50 100 150 200 250]
print(k) [10. 20. 30. 40. 50.]
ALIASING THE ARRAYS
Aliasing in arrays does not make any new copy of the array defined earlier. It
means new reference created for the same array.
import numpy as np
k = np.array ([3, 5, 6,7,8])
h=k Give another name h to array k Output:
print(h) [3 5 6 7 8]
print(k) [3 5 6 7 8]
k[0] = 45 [45 5 6 7 8]
print(h) [45 5 6 7 8]
print(k)

copy( ) method
The copy( ) method is used to copy the contents of one array to another.
import numpy as np
k = np.array([3,5,6,7,8])
print(k)
h = k.copy( ) Create a copy of array k and call it h
print(h)
print(k)
k[0] = 45
print(h)
print(k)

Output:
[3 5 6 7 8]
[3 5 6 7 8]
[3 5 6 7 8]
[3 5 6 7 8]
[45 5 6 7 8]
2-DIMENSIONAL ARRAYS IN NUMPY
When an array contains more than one row and more than one column of
elements, it is called the 2-dimensional array or 2-D array.

Indexes in 2d Array
IMPLEMENTATION OF 2-D ARRAY IN NUMPY
import numpy as np
x =np.array([[2,4,6],[6,8,10]]
Print(x)

Output:
[ [2 4 6]
[6 8 10] ]

ndim Attribute - ndim attribute is used to represent the number of dimensions of axes of the
array. The number of dimensions is also known as 'rank'.

Example:
import numpy as np
A = np.array([5,6,7,8])
R = np.array([[4,5,6],[7,8,9]])
print(A.ndim) Number of rows in array A
print(R.ndim) Number of rows in array R
Output:
1
2
size Attribute - The size attributes gives the total number of elements in the array.
Example:
import numpy as np
a1 = np.array([1,2,3,4,5])
print(a1.size)
Output:
5

itemsize Attribute - The itemsize attributes gives the memory size of array elements in
bytes.
Example:
import numpy as np
a1 = np.array([1, 2,3,4,5])
print(a1.itemsize) gives memory size of array elements in bytes.
Output:
4

reshape( ) method - The reshape() method is useful to change the shape of an array. The
new array should have the same number of elements as in the original array.
Example:
import numpy as np
d =np.array([[4,5,6,7],[5,6,7,8],[7,8,9,6]])
print(d)
d = d.reshape(6,2) Reshape array d to 6 rows and 2 columns

Output:
[[4 5]
[6 7]
[5 6]
[7 8]
[7 8]
[9 6]]
INDEXING in 2-D Array
Index represents the location number of the row and the column of an element. The
individual elements of an array can be accessed by specifying the index of the array
element.
0 1 2
0

A[0][0] => represents 0th row and 0th column element in array A.
A[1][3] => represents 1st row and 3rd column element in the array A.

SLICING in 2-D Array


A slice represents a part or piece of the array.
Row Part Column Part

Array_Name[start:stop:step_size, start:stop:step_size]
0th row to 1st row,
11 2 3 56 14
0th column to 2nd column
A[0:2, 0:3]
Or 40 52 16 12 20
A[:2, :3]

2Nd row to 3RD row, 70 8 9 32 22

3RD column to 4THcolumn


A[2:4, 3:] 18 30 17 44 49

25 55 66 78 82
Top 2 rows and Right 3 columns
A[0:2, 2:] 11 2 3 56 14
Or
A[:2, 2:]
40 52 16 12 20

70 8 9 32 22
Lower 3 rows and right
2 columns
18 30 17 44 49
A[2:, 3:]

25 55 66 78 82

th th
0 row and 0 column
element 11 2 3 56 14
A[0:1, 0:1]
40 52 16 12 20

nd
2 row and 1st column
70 8 9 32 22
element
A[2:3, 1:2]
18 30 17 44 49

25 55 66 78 82
Programs related to NUMPY in Python

Q1. What will be the output of following code:

(a) import numpy as np


A=np.array([24,46,57,14,68,34,89,92])
print(A[7:3:-1])
print(A[2:6])
Ans:
[92 89 34 68]
[57 14 68 34]

(b) import numpy as np


A=np.array([1,2,3,4,5,6,7,8,9,10,11,12])
print(A[10:5:-2])
Ans:
[11 9 7]

(c) import numpy as np


A=np.ones(6)
print(A)
B=np.reshape(A,(2,3))
print(B)
Ans:
[1. 1. 1. 1. 1. 1.]
[[1. 1. 1.]
[1. 1. 1.]]

(d) import numpy as np


arr= np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(arr[::-2])
Ans:
[9 7 5 3 1]

(e) import numpy as np


arr= np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(arr[-2::-2])
Ans:
[8 6 4 2 0]
Q2. WAP to swap first two columns in a 2D numpy array?
Ans:
import numpy as np
arr = np.arange(9).reshape(3,3)
print(arr)
print(arr[:, [1,0,2]])

Or
import numpy as np
arr = np.arange(9).reshape(3,3)
print(arr)
arr[:, [0,1]]=arr[:,[1,0]]
print(arr)

Q3. WAP to swap first two rows in a 2D numpy array?


Ans:
import numpy as np
arr = np.arange(9).reshape(3,3)
print(arr)
print(arr[[1,0,2], :])

OR
import numpy as np
A = np.arange(9).reshape(3,3)
A[[0,1]] = A[[1,0]]
print(A)

Q4. WAP to reverse the rows in a 2D numpy array?


Ans:
import numpy as np
arr = np.arange(9).reshape(3,3)
print(arr)
print(arr[::-1])

Q5. WAP to reverse the columns in a 2D numpy array?


Ans:
import numpy as np
arr = np.arange(9).reshape(3,3)
print(arr)
print(arr[:, ::-1])
Q6. WAP to Create a 5X2 integer array from a range between 100 to 200 such that the
difference between each element is 10.
Ans:
import numpy
print("Creating 5X2 array using numpy.arange")
A= numpy.arange(100, 200, 10)
print(A.reshape(5,2))

Q7. WAP to create a 3*3 numpy array with all the elements as per the user choice and
print the sum of all elements of the array.
Ans:
import numpy as np
a=np.zeros(9,dtype=int).reshape(3,3)
sum=0
for i in range(3):
for j in range(3):
num=int(input('enter element of array'))
a[i][j]=num
sum=sum+a[i][j]
print(a)
print('sum of all elements of array is: ', sum)

You might also like