0% found this document useful (0 votes)
2 views39 pages

Notes Class 10 Python06 Numpy

Uploaded by

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

Notes Class 10 Python06 Numpy

Uploaded by

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

WORKING WITH NUMPY

NUMPY ARRAY
• NumPy stands for Numerical Python. It is the core
library for scientific computing in Python. It
consist of multidimensional array objects, and
tools for working with these arrays.

• Numpy Array is a grid of values with same type,


and is indexed by a tuple of nonnegative
integers. The number of dimensions of it ,is the
rank of the array; the shape of an array depends
upon a tuple of integers giving the size of the
array along each dimension.
NUMPY ARRAY

Note:
Before numpy based programming, it must
be installed. It can be installed using >pip
install numpy command at command prompt

1D Array:
Any arrays can be single or
multidimensional. The number of
subscript/index determines dimensions of
the array. An array of one dimension is
known as a one-dimensional array or 1-D
array
NUMPY ARRAY

In above diagram num 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. At 0 index position value is 2
and at 1 index position value is 5.
NUMPY ARRAY
Attributes of NumPy array
Some important attributes of a NumPy ndarray object are:

1)ndarray.ndim: gives the number of dimensions of the array as


an integer value. Arrays can be 1-D, 2-D or n-D. NumPy calls the
dimensions as axes (plural of axis). Thus, a 2-D array has two axes.
The row-axis is called axis-0 and the column-axis is called axis-1.
The number of axes is also called the array’s rank.

2)ndarray.shape: It gives the sequence of integers indicating the


size of the array for each dimension.
The output (3, 2) means the array has 3 rows and 2 columns.
NUMPY ARRAY

3)ndarray.size: It gives the total number of elements of the array. This is equal
to the product of the elements of shape.

4)ndarray.dtype: is the data type of the elements of the array. All the elements
of an array are of same data type. Common data types are int32, int64, float32,
float64, U32, etc.

5) ndarray.itemsize: It specifies the size in bytes of each element of the array.


Data type int32 and float32 means each element of the array occupies 32 bits in
memory. 8 bits form a byte. Thus, an array of elements of type int32 has itemsize
32/8=4 bytes. Likewise, int64/float64 means each item has itemsize 64/8=8
bytes.
NUMPY ARRAY
array,type, ndim,shape,size, dtype, itemsize functions on 1D array
Program:
import numpy as np
arr1=np.array([1,2,3,1])
print("Type of arr1:-",type(arr1))
print("Dimension of arr1:-", arr1.ndim)
print("Shape of arr1:-",arr1.shape)
print("Size of arr1:-",arr1.size)
print("dtype of arr1:-",arr1.dtype)
print("Itemsize of arr1:-",arr1.itemsize)

Output:
Type of arr1:- <class 'numpy.ndarray’>
Dimension of arr1:- 1
Shape of arr1:- (4,)
Size of arr1:- 4
dtype of arr1:- int64
Itemsize of arr1:- 8
NUMPY ARRAY
array,type, ndim,shape,size, dtype, itemsize functions on 2D array
Program:
import numpy as np
arr2=np.array([[1,2,3,1],[4,5,6,1],[7,8,9,1]])
print("Type of arr2:-",type(arr2))
print("Dimension of arr2:-", arr2.ndim)
print("Shape of arr2:-",arr2.shape)
print("Size of arr2:-",arr2.size)
print("dtype of arr2:-",arr2.dtype)
print("Itemsize of arr2:-",arr2.itemsize)

Output:
Type of arr2:- <class 'numpy.ndarray’>
Dimension of arr2:- 2
Shape of arr2:- (3, 4)
Size of arr2:- 12
dtype of arr2:- int64
Itemsize of arr2:- 8
NUMPY ARRAY
array,type, ndim,shape,size, dtype, itemsize functions on 3D array
Program:
import numpy as np
arr3=np.array([[[1,2,3],[4,5,6],[7,8,9]],
[[1,2,3],[4,5,6],[7,8,9]],
[[1,2,3],[4,5,6],[7,8,9]]])
print("Type of arr3:-",type(arr3))
print("Dimension of arr3:-", arr3.ndim)
print("Shape of arr3:-",arr3.shape)
print("Size of arr3:-",arr3.size)
print("dtype of arr3:-",arr3.dtype)
print("Itemsize of arr3:-",arr3.itemsize)

Output:
Type of arr3:- <class 'numpy.ndarray’>
Dimension of arr3:- 3
Shape of arr3:- (3, 3, 3)
Size of arr3:- 27
dtype of arr3:- int64
Itemsize of arr3:- 8
NUMPY ARRAY
Data types: Some of the data types supported by Numpy
NUMPY ARRAY
Data types: Some of the data types supported by Numpy
NUMPY ARRAY
Creation of 1D array:
One dimension array can be created using array method with list object
with one dimensional elements

Example program:-
import numpy as np
a = np.array([500, 200, 300]) # Create a 1D Array
print(type(a)) # Prints "<class
'numpy.ndarray'>" print(a.shape) # Prints "(3,)"
means dimension of array print(a[0], a[1], a[2]) #
Prints "500 200 300"
a[0] = 150 # Change an element of the array
print(a)
NUMPY ARRAY
Creation of 1D array Using functions

import numpy as np
p = np.empty(5) # Create an array of 5 elements with random values
print(p)
a1 = np.zeros(5) # Create an array of all zeros float values
print(a1) # Prints "[0. 0. 0. 0. 0.]”
a2 = np.zeros(5, dtype = np.int) # Create an array of all zeros int values
print(a2) # Prints "[0. 0. 0. 0. 0.]"
a3= np.ones(5) # Create an array of all ones
print(a3) # Prints "[1. 1. 1. 1. 1.]”
a4= np.full(5, 7) # Create a constant array
print(a4) # Prints "[7 7 7 7 7]”
a5=np.random.random(5) # Create an array
filled with random values
NUMPY ARRAY
Difference between Numpy array and list

NUMPY ARRAY LIST


Numpy Array works on Python list are made for
homogeneous types heterogeneous types
Python list support Numpy Array does not
adding and removing of support adding and
elements removing of elements
Can’t contain elements Can contain elements of
of different types different types
Smaller memory More memory consumption
consumption
Better runtime Runtime not speedy
NUMPY ARRAY
Create 1D from string:-
import numpy as np
data =np.fromstring('1 2', dtype=int, sep=‘ ’)
print(data)
•Note:- in fromstring dtype and sep argument
can be changed.

Create 1D from buffer:- numpy array from


range
numpy.arange(start, stop, step, dtype)
•Program 1
import numpy as np
x=np.arange(5) #for
float value specify dtype
NUMPY ARRAY
Program 1:
import numpy as np
x=np.arange(5) #for float value
specify dtype = float as argument

print(x) #print [0 1 2 3
4]

Program 2
import numpy as np
x = np.arange(10,20,2)
print(x) #print [10 12 1416
18]
NUMPY ARRAY
Create 1D from array
Copy function is used to create the copy
of the existing array.
Program:
import numpy as np
x = np.array([1, 2, 3])
y= x
z= np.copy(x)
x[0] = 10
print(x)
print(y)
print(z)
•Note that, when we modify
x, y changes, but not z:
numpy.fromiter ()
NUMPY ARRAY
This function builds an ndarray object from any iterable object.
A new one dimensional array is returned by this function.

OUTPUT:
NUMPY ARRAY
numpy.linspace():

This function is similar to arange() function. In this function,


instead of step size, the number of evenly spaced values between
the interval is specified. The usage of this function is as follows:

1 D ARRAY

OUTPUT: Ndarray with 6


values falling
between 2.5 and 5
NUMPY ARRAY
Slicing:
Slicing of numpy array elements is just similar to
slicing of list elements.

Program:
import numpy as np
data = np.array([5,2,7,3,9])
print (data[:]) #print [5 2 7
3 9]
print(data[1:3]) #print [2 7]
print(data[:2]) #print [5 2]
print(data[-2:]) #print [3 9]
NUMPY ARRAY
Joining of 1D array-Concatenate:
Joining of two or more one dimensional array is possible
with the help of concatenate() function of numpy object.

Program
import numpy as np
a = np.array([1, 2, 3])
b = np.array([5, 6])
c=np.concatenate([a,b,a])
print(c) #print [1 2 3 5 6 1 2 3]
NUMPY ARRAY
Basic arithmetic operation on 1D Array:

Program:
import numpy as np
x = np.array([1, 2, 3,4])
y = np.array([1, 2, 3,4])
z=x+y
print(z) #print [2 4 6 8]
z=x-y
print(z) #print [0 0 0 0]
z=x*y
print(z) #print [ 1 4 9 16]
z=x/y
print(z) #print [1. 1. 1. 1.]
z=x+1
print(z) #print [2 3 4 5]
NUMPY ARRAY
Aggregate operation on 1D Array:

Program:
import numpy as np
x = np.array([1, 2, 3,4])
print(x.sum()) #print 10
print(x.min()) #print 1
print(x.max()) #print 4
print(x.mean()) #print 2.5
print(np.median(x)) #print 2.5
NUMPY ARRAY
2 D Array
An array of one dimension/index/subscript is known as a one
dimensional array or 1-D array. In below diagram num is an array
of two dimension with 3 rows and 4 columns. Subscript of rows is
0 to 2 and columns is 0 to 3.
NUMPY ARRAY
Storage of 2D array in Memory
Elements of arrays are stored in contiguous memory locations.
Therefore 2D arrays are linearized for storage in two ways:

Row-Major implementation of arrays (row-wise) :


This technique stores firstly the first row of the array, then second
row and so forth.

Row-Major
NUMPY ARRAY
Column-Major implementation of arrays(column-wise)
This technique stores firstly the first column of the array, then
second column and so forth
Column-
Major
NUMPY ARRAY

Creation of 2D array
Two dimension array can be created using array method with list
object with two dimensional elements.

Program
import numpy as np
a = np.array([[3, 2, 1],[1, 2, 3]]) # Create a 2D Array
print(type(a)) # Prints "<class 'numpy.ndarray'>"
print(a.shape) # Prints (2, 3)
print(a[0][1]) # Prints 2
a[0][1] = 150 # Change an element of the
array
print(a) # prints [[ 3 150 1] [1 2
3]]
NUMPY ARRAY
Creation of 2D array Using functions

import numpy as np
p = np.empty([2,2]) # Create an array of 4 elements with random
values print(p)
a1 = np.zeros([2,2]) # Create 2d array of all zeros float values
print(a1) # Prints [[0. 0.][0. 0.]]
a2 = np.zeros([2,2], dtype = np.int) # Create an array of all
zeros int values print(a2) # Prints [[0 0] [0 0]]
b= np.ones([2,2]) # Create an array of all ones
print(b) # Prints [[1. 1.] [1. 1.]]
c= np.full([2,2], 7) # Create a constant array
print(c) # Prints [[7 7] [7 7]]
e = np.random.random([2,2]) # Create 2d array filled with random
values
NUMPY ARRAY
Creation of 2D array from 1D array:
We can create 2D array from 1d array using reshape() function.

Program:

import numpy as np
A = np.array([1,2,3,4,5,6])
B = np.reshape(A, (2, 3))
print(B)

Output:
[[1 2 3]
[4 5 6]]
NUMPY ARRAY
Slicing-2 D Array:
2d array elements is just similar to slicing of list elements with 2
dimension.

Program
import numpy as np
A = np.array([[7, 5, 9, 4],[ 7, 6, 8, 8],[ 1, 6, 7, 7]])
print(A[:2, :3]) #print elements of 0,1 rows and 0,1,2 columns
print(A[:3, ::2]) #print elements of 0,1,2 rows and alternate
column position print(A[::-1, ::-1]) #print elements in reverse
order
print(A[:, 0]) #print all elements of 0 column
print(A[0, :]) #print all elements of 0 rows
NUMPY ARRAY
Joining of 2D Array using Concatenation

Program:
import numpy as np
A = np.array([[7, 5],[1, 6]])
print(np.concatenate([A, A])) # concatenate along the first axis

Output:
[[7 5] [1 6] # concatenate along the second axis (zero-
indexed)
[7 5] [1 6]]

print(np.concatenate([A, A], axis=1))


[[7 5 7 5]
[1 6 1 6]]
NUMPY ARRAY
Joining of 2D Array using Concatenation

# vertically stack the arrays

• print(np.vstack([x, A])) [[1 2] # horizontally stack the arrays [7 5] y


= np.array([[99], [1 6]]

• [99]])

• print(np.hstack([A, y])) [[ 7 5 99]

• [ 1 6 99]]
NUMPY ARRAY
Sorting:
Sorting is to arrange the elements of an array in
hierarchical order either ascending or descending. By
default, numpy does sorting in ascending order.
NUMPY ARRAY

In 2-D array, sorting can be


done along either of the
axes i.e., row-wise or
column-wise. By default,
sorting is done row-wise
(i.e., on axis = 1). It means
to arrange elements in
each row in ascending
order. When axis=0, sorting
is done column-wise, which
means each column is
sorted in ascending order
NUMPY ARRAY
2D Array – Arithmetic Operation: Output:
Arithmetic operation over 2d array is possible [[7 5 9]
with add, substract, multiply, divide functions. [2 6 8]]

Program: [[17 15 19]


import numpy as np a = np.array([[7, 5, 9],[ 2, 6, [12 16 18]]
8]])
print(a) [[-3 -5 -1]
b = np.array([10,10,10]) [-8 -4 -2]]
c=np.add(a,b) # c=a+b,
print(c) [[70 50 90]
c=np.subtract(a,b) # c=a-b, [20 60 80]]
print(c)
c=np.multiply(a,b) # c=a*b,
print(c) [[0.7 0.5
c=np.divide(a,b) # c=a/b 0.9]
print(c) [0.2 0.6
0.8]]
NUMPY ARRAY
Note:

1) if both 2d arrays are with same dimension[matrix form] then


one to one arithmetic operation will be performed.

2) No of elements of a dimension must match otherwise error


message thrown 2D Array – Arithmetic operation
NUMPY ARRAY
Arithmetic operation over 2d array can be
done with single value also.
Output:
Program: [[7 5 9]
import numpy as np a = np.array([[7, 5, 9], [2 6 8]]
[ 2, 6, 8]])
print(a) [[ 9 7 11]
c=np.add(a,2) [ 4 8 10]]
print(c)
c=np.subtract(a,2) [[5 3 7]
print(c) [0 4 6]]
c=np.multiply(a,2)
print(c) [[14 10 18]
c=np.divide(a,2) [ 4 12 16]]
print(c)
[[3.5 2.5
4.5]
[1. 3. 4. ]]
NUMPY ARRAY
2D Array – Mathematical Functions

Maths functions like power, abs, ceil, floor, around and trigonometric
functions like sin, cos, tan, asin etc are supported by numpy
Output:
Program: [[53.772889
import numpy as np 27.279729]
a = np.array([[7.333, 5.223],[ 2.572, 6.119]]) [ 6.615184
print(np.power(a,2)) 37.442161]]
print(np.ceil(a)) [[8. 6.]
[3. 7.]]
print(np.floor(a))
[[7. 5.]
print(np.around(a,1)) [2. 6.]]

[[7.3 5.2]
[2.6 6.1]]
Keep
Learning…

You might also like