SlideShare a Scribd company logo
Enhancing Data
Insights and
Image Analysis-
NumPy
DR.PUSHPA MOHAN
PROFESSOR
Title: Introduction to NumPy and
NumPy Operations
What is NumPy?
NumPy (Numerical Python) is a fundamental package for scientific
computing in Python.
•Purpose: Provides support for arrays, matrices, and a variety of
mathematical functions to operate on these data structures.
•Key Feature: Provides an efficient multidimensional array object
(ndarray).
Why Use NumPy?
•Performance: Provides a performance boost over native Python
lists for numerical computations.
•Functionality: Includes mathematical functions, linear algebra
operations, and random number generation.
•Integration: Works seamlessly with other libraries like SciPy,
Pandas, and Matplotlib.
Installing NumPy
•Command:
pip install numpy or ! pip install numpy –google colab
•Verification: Importing NumPy in Python
import numpy as np
Basic Concepts
Arrays
NumPy’s core feature is the ndarray (n-dimensional array).
Unlike Python lists, NumPy arrays are homogeneous, meaning they can only contain
elements of the same type, and they offer better performance for numerical
operations.
Creating NumPy Arrays
•Creating Arrays:
1. From lists: a=np.array([1, 2, 3]) b = np.array([[1, 2], [3, 4]])
2. zeros_array = np.zeros((2, 3)) # Array of zeros
3. ones_array = np.ones((3, 2)) # Array of ones
4. random_array = np.random.rand(2, 2) # Array with random values
5. range_array = np.arange(0, 10, 2) # Array with values from 0 to 10 with a step
of 2
6. linspace_array = np.linspace(0, 1, 5) # Array with 5 values evenly spaced
between 0 and 1
7. Using functions: np.zeros((2, 3)), np.ones((2, 3)), np.arange(10)
•Array Properties: Shape, Size, Dtype
import numpy as np
a = np.array([1, 2, 3])
b = np.array([[1, 2], [3, 4]])
print(a)
print(b)
a1 = np.ones((3,2))
a2 = np.random.rand(3,3)
print(a1)
print(a2)
range_array = np.arange(0, 10, 2)
print(range_array)
linspace_array = np.linspace(0, 1, 5)
print(linspace_array)
print(a1.shape)
print(a2.dtype)
print(b.size)
Arithmatic with NumPy Arrays
Any arithmetic operations between equal-size arrays applies the operation element-wise:
arr = np.array([[1., 2., 3.], [4., 5., 6.]])
print(arr)
[[1. 2. 3.]
[4. 5. 6.]]
print(arr * arr)
[[ 1. 4. 9.]
[16. 25. 36.]]
print(arr - arr)
[[0. 0. 0.]
[0. 0. 0.]]
Arithmetic with NumPy Arrays
Arithmetic operations
with scalars propagate
the scalar argument to
each element in the
array:
Comparisons between arrays of the same
size yield boolean arrays:
arr2 = np.array([[0., 4., 1.], [7., 2., 12.]])
print(arr2)
[[ 0. 4. 1.]
[ 7. 2. 12.]]
print(arr2 > arr)
[[False True False]
[ True False True]]
arr = np.array([[1., 2., 3.], [4., 5., 6.]])
print(arr)
[[1. 2. 3.]
[4. 5. 6.]]
print(arr **2)
[[ 1. 4. 9.]
[16. 25. 36.]]
import numpy as np
arr = np.array([[1., 2., 3.], [4., 5., 6.]])
print(arr)
print(arr * arr)
print(arr - arr)
print(arr **2)
arr2 = np.array([[0., 4., 1.], [7., 2., 12.]])
print(arr2)
print(arr2 > arr)
Indexing and Slicing
One-dimensional arrays are simple; on the surface they act similarly to Python
lists:
arr = np.arange(10)
print(arr) # [0 1 2 3 4 5 6 7 8 9]
print(arr[5]) #5
print(arr[5:8]) #[5 6 7]
arr[5:8] = 12
print(arr) #[ 0 1 2 3 4 12 12 12 8 9]
Indexing and Slicing
arr = np.arange(10)
print(arr) # [0 1 2 3 4 5 6 7 8 9]
arr_slice = arr[5:8]
print(arr_slice) # [5 6 7]
arr_slice[1] = 12345
print(arr) # [ 0 1 2 3 4 5 12345 7 8 9]
arr_slice[:] = 64
print(arr) # [ 0 1 2 3 4 64 64 64 8 9]
Basic Array Operations
Indexing and Slicing:
Example :
arr = np.array([1, 2, 3, 4, 5])
print(arr[1:4])
# Output: [2 3 4]
Reshaping:
Example :
arr = np.arange(6).reshape((2, 3))
Indexing :
a = np.array([1, 2, 3, 4, 5])
# Accessing elements
element = a[2] # 3
# Slicing
slice_a = a[1:4] # array([2, 3, 4])
import numpy as np
ar = np.arange(6).reshape((2, 3))
print(ar)
print(ar[1,2])
arr = np.arange(10)
print(arr)
print(arr[5])
print(arr[5:8])
arr[5:8] = 12
print(arr)
arr1 = np.arange(10)
print(arr1)
arr_slice = arr1[5:8]
print(arr_slice)
arr_slice[1] = 12345
print(arr1)
arr_slice[:]=75
print(arr1)
Mathematical Operations
Element-wise Operations: Aggregate Functions:
Example
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b)
# Output: [5 7 9]
Example
1. sum_a = np.sum(a) # Sum of elements
2. mean_a = np.mean(a) # Mean of elements
3. std_a = np.std(a) # Standard deviation of
elements
4. max_a = np.max(a) # Maximum value
5. min_a = np.min(a) # Minimum value
np.mean(), np.sum(), np.max(), np.min()
Element-wise Functions:
Example
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
sqrt_a = np.sqrt(a) # Square root of each element
exp_a = np.exp(a) # Exponential of each element
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b)
sum_a = np.sum(a)
print(sum_a)
mean_a = np.mean(a)
print(mean_a)
std_a = np.std(a)
print(std_a)
max_a = np.max(a)
print(max_a)
min_a = np.min(a)
print(min_a)
sqrt_a = np.sqrt(a)
print(sqrt_a)
For multi-dimensional arrays, you
can use tuple indexing:
Multi-dimensional array:
b = np.array([[1, 2, 3], [4, 5, 6]])
# Accessing a specific element
element = b[1, 2] # 6
# Slicing a sub-array
sub_array = b[0:2, 1:3]
# array([[2, 3], [5, 6]])
•0:2: This selects rows from index 0 up to (but not including) index
2. So, it selects the first and second rows.
•1:3: This selects columns from index 1 up to (but not including)
index 3. So, it selects the second and third columns.
import numpy as np
b = np.array([[1, 2, 3], [4, 5,
6]])
print(b)
element = b[1, 2]
print(element)
sub_array = b[0:2, 1:3]
print(sub_array)
Linear Algebra
# Matrix multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
product = np.dot(A, B) #
array([[19, 22], [43, 50]])
# Determinant
determinant = np.linalg.det(A)
# -2.0000000000000004
# Eigenvalues
eigenvalues, eigenvectors =
np.linalg.eig(A)
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
product = np.dot(A, B)
print(product)
determinant = np.linalg.det(A)
print(determinant )
# Eigenvalues
eigenvalues, eigenvectors = np.linalg.eig(A)
print(eigenvalues)
print(eigenvectors)
Iterating NumPy arrays.
1. One dimensional array
2. Two dimensional array
3. Multi dimensional array
Iterating over a one-dimensional
NumPy array:
import numpy as np
# Create a 1D NumPy array
arr_1d = np.array([1, 2, 3, 4, 5])
# Iterate over the elements
for element in arr_1d:
print(element)
Iterating over a two-dimensional
NumPy array:
# Create a 2D NumPy array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
# Iterate over rows
for row in arr_2d:
print("Row:", row)
# You can also iterate over each element in the row
for element in row:
print(element)
import numpy as np
# Create a 3D NumPy array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
# Use nditer to iterate over each element
for element in np.nditer(arr_3d)
: print(element)
Iterating over a multi-dimensional NumPy
array using nditer:
import numpy as np
# Create a 1D NumPy array
arr_1d = np.array([1, 2, 3, 4, 5])
print(arr_1d.shape)
# Iterate over the elements
for element in arr_1d:
print(element)
# Create a 2D NumPy array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d.shape)
# Iterate over rows
for row in arr_2d:
print("Row:", row)
# You can also iterate over each element in the row
for element in row:
print(element)
# Create a 3D NumPy array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr_3d.shape)
# Use nditer to iterate over each element
for element in np.nditer(arr_3d):
print(element)
Summary:
1.NumPy is essential for numerical and matrix
computations in Python.
2.It offers powerful tools for creating and manipulating
arrays.
3.Supports advanced mathematical and linear algebra
operations.
Exercises
Exercise
1. Creating and Inspecting Arrays
Problem:
1.Create a NumPy array with values from 10 to 50 (inclusive) with a step of 5.
2.Reshape the array into a 2D array with 3 rows and 3 columns.
3.Find the shape and data type of the array.
2.Create different types of NumPy arrays.
Problem:
1.Create a one-dimensional array containing the numbers 1 to 10.
2.Create a two-dimensional array of size 3x3 containing random integers between 1
and 20.
3.Create a 5x5 identity matrix.
Exercises
3.Perform basic operations on NumPy arrays.
Problem :
1.Add 10 to every element in an array.
2.Multiply two arrays element-wise.
3.Find the dot product of two arrays.
4..Perform array indexing and slicing on a given array.
Problem :
1.Create an array from 10 to 30 (inclusive).
2.Slice the array to get the first 5 elements.
3.Get all the even numbers from the array.
Exercises
5.Iterate over a two-dimensional array.
Problem :
1. Create a 2D array of shape 3x3 with values from 1 to 9.
2. Iterate over each row and print the row.
3. Iterate over each element of the array.

More Related Content

Similar to NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx (20)

PDF
Numpy python cheat_sheet
Nishant Upadhyay
 
PDF
Numpy python cheat_sheet
Zahid Hasan
 
PPTX
Usage of Python NumPy, 1Dim, 2Dim Arrays
NarendraDev11
 
PPTX
Lecture 2 _Foundions foundions NumPyI.pptx
disserdekabrcha
 
PPTX
NumPy.pptx
DrJasmineBeulahG
 
PDF
Python_cheatsheet_numpy.pdf
AnonymousUser67
 
PPT
Introduction to Numpy Foundation Study GuideStudyGuide
elharriettm
 
PDF
Introduction to NumPy (PyData SV 2013)
PyData
 
PDF
Introduction to NumPy
Huy Nguyen
 
PPTX
Introduction-to-NumPy-in-Python (1).pptx
disserdekabrcha
 
PDF
Concept of Data science and Numpy concept
Deena38
 
PPTX
THE NUMPY LIBRARY of python with slides.pptx
fareedullah211398
 
PDF
Numpy cheat-sheet
Arief Kurniawan
 
PPT
CAP776Numpy (2).ppt
ChhaviCoachingCenter
 
PPT
CAP776Numpy.ppt
kdr52121
 
PPTX
NUMPY-2.pptx
MahendraVusa
 
PPTX
NumPy.pptx
EN1036VivekSingh
 
PPTX
Data Preprocessing Introduction for Machine Learning
sonali sonavane
 
PPTX
Introduction to numpy Session 1
Jatin Miglani
 
PPTX
Numpy
Jyoti shukla
 
Numpy python cheat_sheet
Nishant Upadhyay
 
Numpy python cheat_sheet
Zahid Hasan
 
Usage of Python NumPy, 1Dim, 2Dim Arrays
NarendraDev11
 
Lecture 2 _Foundions foundions NumPyI.pptx
disserdekabrcha
 
NumPy.pptx
DrJasmineBeulahG
 
Python_cheatsheet_numpy.pdf
AnonymousUser67
 
Introduction to Numpy Foundation Study GuideStudyGuide
elharriettm
 
Introduction to NumPy (PyData SV 2013)
PyData
 
Introduction to NumPy
Huy Nguyen
 
Introduction-to-NumPy-in-Python (1).pptx
disserdekabrcha
 
Concept of Data science and Numpy concept
Deena38
 
THE NUMPY LIBRARY of python with slides.pptx
fareedullah211398
 
Numpy cheat-sheet
Arief Kurniawan
 
CAP776Numpy (2).ppt
ChhaviCoachingCenter
 
CAP776Numpy.ppt
kdr52121
 
NUMPY-2.pptx
MahendraVusa
 
NumPy.pptx
EN1036VivekSingh
 
Data Preprocessing Introduction for Machine Learning
sonali sonavane
 
Introduction to numpy Session 1
Jatin Miglani
 

More from tahirnaquash2 (7)

PDF
Academia Maven Silicon.pdfAcademia Maven Silicon.pdf
tahirnaquash2
 
PDF
ATAL_Online_6_Days_Faculty_Development_Programme_Selected_List_for_A.Y_2024-2...
tahirnaquash2
 
PPT
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt
tahirnaquash2
 
PPT
ch10_EffiBinSearchTrees ch10_EffiBinSearchTrees
tahirnaquash2
 
PPT
ch10_Key_Management.ppt ch10_Key_Management.ppt ch10_Key_Management.ppt
tahirnaquash2
 
PPT
ch08 modified.pptmodified.pptmodified.ppt
tahirnaquash2
 
PPT
Intro-2013.pptIntro-2013.pptIntro-2013.ppt
tahirnaquash2
 
Academia Maven Silicon.pdfAcademia Maven Silicon.pdf
tahirnaquash2
 
ATAL_Online_6_Days_Faculty_Development_Programme_Selected_List_for_A.Y_2024-2...
tahirnaquash2
 
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt
tahirnaquash2
 
ch10_EffiBinSearchTrees ch10_EffiBinSearchTrees
tahirnaquash2
 
ch10_Key_Management.ppt ch10_Key_Management.ppt ch10_Key_Management.ppt
tahirnaquash2
 
ch08 modified.pptmodified.pptmodified.ppt
tahirnaquash2
 
Intro-2013.pptIntro-2013.pptIntro-2013.ppt
tahirnaquash2
 
Ad

Recently uploaded (20)

PPTX
Benign Paroxysmal Positional Vertigo (Bppv)
Tejalvarpe
 
PDF
NNF NEONATOLOGY GUIDE LINES. Includes the very basic about important helping ...
Tariq Mir
 
PDF
BUCAS supporting DOH 8 Health Priorities
pedrofamorca
 
DOCX
Why Inflammation Markers Are Reshaping Heart Disease Risk Assessment
Ram Gopal Varma
 
PDF
ESC guidelines on heart failure 2025.pdf
KamruzzamanShawon4
 
PPTX
Fundamentals of computer aided drug design.pptx
Onkar589550
 
PPTX
Decoding the Woodward-Fieser Rules A Comprehensive Guide to UV-Visible Spectr...
Dr. Smita Kumbhar
 
PPTX
Regulatory Aspects of Herbal and Biologics in INDIA.pptx
Aaditi Kamble
 
PDF
SULCI, GYRI & FUNCTIONAL AREAS OF CEREBRUM-Prof.Dr.N.Mugunthan KMMC.pdf
Kanyakumari Medical Mission Research Center, Muttom
 
PPTX
UPDATE on NEWER MALARIA VACCINE.pptx
AshwaniSood12
 
PPTX
COPD (Chronic Obstructive Pulmonary Disease) .pptx
Dr. Sukriti Silwal
 
PDF
Miraculous Clinico-Radiological Complete Remission to Low-Dose Nivolumab and ...
Kanhu Charan
 
PPTX
Decoding the Optic Disc: A Beginner’s Guide to OCT Imaging & Analysis
KafrELShiekh University
 
PPTX
Cancer - Treatment Modalities, Principles of cancer chemotherapy.pptx
Ayesha Fatima
 
PPTX
questionnaires in and how to map as per CT and aCRF
JampaniGangadhar
 
PPTX
Epidemiology for Nursing by Dr.Ayan Ghosh.pptx
Ayan Ghosh
 
PDF
Process validation of liquid orals and aerosals
PavanKumar245656
 
PDF
DEVELOPMENT OF GIT. Prof. Dr.N.MUGUNTHAN KMMC.pdf
Kanyakumari Medical Mission Research Center, Muttom
 
PDF
Balance and Equilibrium - The Vestibular System
MedicoseAcademics
 
PPTX
tuberculosis of spine presebtation .pptx
sumitbhosale34
 
Benign Paroxysmal Positional Vertigo (Bppv)
Tejalvarpe
 
NNF NEONATOLOGY GUIDE LINES. Includes the very basic about important helping ...
Tariq Mir
 
BUCAS supporting DOH 8 Health Priorities
pedrofamorca
 
Why Inflammation Markers Are Reshaping Heart Disease Risk Assessment
Ram Gopal Varma
 
ESC guidelines on heart failure 2025.pdf
KamruzzamanShawon4
 
Fundamentals of computer aided drug design.pptx
Onkar589550
 
Decoding the Woodward-Fieser Rules A Comprehensive Guide to UV-Visible Spectr...
Dr. Smita Kumbhar
 
Regulatory Aspects of Herbal and Biologics in INDIA.pptx
Aaditi Kamble
 
SULCI, GYRI & FUNCTIONAL AREAS OF CEREBRUM-Prof.Dr.N.Mugunthan KMMC.pdf
Kanyakumari Medical Mission Research Center, Muttom
 
UPDATE on NEWER MALARIA VACCINE.pptx
AshwaniSood12
 
COPD (Chronic Obstructive Pulmonary Disease) .pptx
Dr. Sukriti Silwal
 
Miraculous Clinico-Radiological Complete Remission to Low-Dose Nivolumab and ...
Kanhu Charan
 
Decoding the Optic Disc: A Beginner’s Guide to OCT Imaging & Analysis
KafrELShiekh University
 
Cancer - Treatment Modalities, Principles of cancer chemotherapy.pptx
Ayesha Fatima
 
questionnaires in and how to map as per CT and aCRF
JampaniGangadhar
 
Epidemiology for Nursing by Dr.Ayan Ghosh.pptx
Ayan Ghosh
 
Process validation of liquid orals and aerosals
PavanKumar245656
 
DEVELOPMENT OF GIT. Prof. Dr.N.MUGUNTHAN KMMC.pdf
Kanyakumari Medical Mission Research Center, Muttom
 
Balance and Equilibrium - The Vestibular System
MedicoseAcademics
 
tuberculosis of spine presebtation .pptx
sumitbhosale34
 
Ad

NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx

  • 1. Enhancing Data Insights and Image Analysis- NumPy DR.PUSHPA MOHAN PROFESSOR
  • 2. Title: Introduction to NumPy and NumPy Operations
  • 3. What is NumPy? NumPy (Numerical Python) is a fundamental package for scientific computing in Python. •Purpose: Provides support for arrays, matrices, and a variety of mathematical functions to operate on these data structures. •Key Feature: Provides an efficient multidimensional array object (ndarray).
  • 4. Why Use NumPy? •Performance: Provides a performance boost over native Python lists for numerical computations. •Functionality: Includes mathematical functions, linear algebra operations, and random number generation. •Integration: Works seamlessly with other libraries like SciPy, Pandas, and Matplotlib.
  • 5. Installing NumPy •Command: pip install numpy or ! pip install numpy –google colab •Verification: Importing NumPy in Python import numpy as np
  • 6. Basic Concepts Arrays NumPy’s core feature is the ndarray (n-dimensional array). Unlike Python lists, NumPy arrays are homogeneous, meaning they can only contain elements of the same type, and they offer better performance for numerical operations.
  • 7. Creating NumPy Arrays •Creating Arrays: 1. From lists: a=np.array([1, 2, 3]) b = np.array([[1, 2], [3, 4]]) 2. zeros_array = np.zeros((2, 3)) # Array of zeros 3. ones_array = np.ones((3, 2)) # Array of ones 4. random_array = np.random.rand(2, 2) # Array with random values 5. range_array = np.arange(0, 10, 2) # Array with values from 0 to 10 with a step of 2 6. linspace_array = np.linspace(0, 1, 5) # Array with 5 values evenly spaced between 0 and 1 7. Using functions: np.zeros((2, 3)), np.ones((2, 3)), np.arange(10) •Array Properties: Shape, Size, Dtype
  • 8. import numpy as np a = np.array([1, 2, 3]) b = np.array([[1, 2], [3, 4]]) print(a) print(b) a1 = np.ones((3,2)) a2 = np.random.rand(3,3) print(a1) print(a2) range_array = np.arange(0, 10, 2) print(range_array) linspace_array = np.linspace(0, 1, 5) print(linspace_array) print(a1.shape) print(a2.dtype) print(b.size)
  • 9. Arithmatic with NumPy Arrays Any arithmetic operations between equal-size arrays applies the operation element-wise: arr = np.array([[1., 2., 3.], [4., 5., 6.]]) print(arr) [[1. 2. 3.] [4. 5. 6.]] print(arr * arr) [[ 1. 4. 9.] [16. 25. 36.]] print(arr - arr) [[0. 0. 0.] [0. 0. 0.]]
  • 10. Arithmetic with NumPy Arrays Arithmetic operations with scalars propagate the scalar argument to each element in the array: Comparisons between arrays of the same size yield boolean arrays: arr2 = np.array([[0., 4., 1.], [7., 2., 12.]]) print(arr2) [[ 0. 4. 1.] [ 7. 2. 12.]] print(arr2 > arr) [[False True False] [ True False True]] arr = np.array([[1., 2., 3.], [4., 5., 6.]]) print(arr) [[1. 2. 3.] [4. 5. 6.]] print(arr **2) [[ 1. 4. 9.] [16. 25. 36.]]
  • 11. import numpy as np arr = np.array([[1., 2., 3.], [4., 5., 6.]]) print(arr) print(arr * arr) print(arr - arr) print(arr **2) arr2 = np.array([[0., 4., 1.], [7., 2., 12.]]) print(arr2) print(arr2 > arr)
  • 12. Indexing and Slicing One-dimensional arrays are simple; on the surface they act similarly to Python lists: arr = np.arange(10) print(arr) # [0 1 2 3 4 5 6 7 8 9] print(arr[5]) #5 print(arr[5:8]) #[5 6 7] arr[5:8] = 12 print(arr) #[ 0 1 2 3 4 12 12 12 8 9]
  • 13. Indexing and Slicing arr = np.arange(10) print(arr) # [0 1 2 3 4 5 6 7 8 9] arr_slice = arr[5:8] print(arr_slice) # [5 6 7] arr_slice[1] = 12345 print(arr) # [ 0 1 2 3 4 5 12345 7 8 9] arr_slice[:] = 64 print(arr) # [ 0 1 2 3 4 64 64 64 8 9]
  • 14. Basic Array Operations Indexing and Slicing: Example : arr = np.array([1, 2, 3, 4, 5]) print(arr[1:4]) # Output: [2 3 4] Reshaping: Example : arr = np.arange(6).reshape((2, 3)) Indexing : a = np.array([1, 2, 3, 4, 5]) # Accessing elements element = a[2] # 3 # Slicing slice_a = a[1:4] # array([2, 3, 4])
  • 15. import numpy as np ar = np.arange(6).reshape((2, 3)) print(ar) print(ar[1,2]) arr = np.arange(10) print(arr) print(arr[5]) print(arr[5:8]) arr[5:8] = 12 print(arr) arr1 = np.arange(10) print(arr1) arr_slice = arr1[5:8] print(arr_slice) arr_slice[1] = 12345 print(arr1) arr_slice[:]=75 print(arr1)
  • 16. Mathematical Operations Element-wise Operations: Aggregate Functions: Example a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(a + b) # Output: [5 7 9] Example 1. sum_a = np.sum(a) # Sum of elements 2. mean_a = np.mean(a) # Mean of elements 3. std_a = np.std(a) # Standard deviation of elements 4. max_a = np.max(a) # Maximum value 5. min_a = np.min(a) # Minimum value np.mean(), np.sum(), np.max(), np.min()
  • 17. Element-wise Functions: Example a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) sqrt_a = np.sqrt(a) # Square root of each element exp_a = np.exp(a) # Exponential of each element
  • 18. import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(a + b) sum_a = np.sum(a) print(sum_a) mean_a = np.mean(a) print(mean_a) std_a = np.std(a) print(std_a) max_a = np.max(a) print(max_a) min_a = np.min(a) print(min_a) sqrt_a = np.sqrt(a) print(sqrt_a)
  • 19. For multi-dimensional arrays, you can use tuple indexing: Multi-dimensional array: b = np.array([[1, 2, 3], [4, 5, 6]]) # Accessing a specific element element = b[1, 2] # 6 # Slicing a sub-array sub_array = b[0:2, 1:3] # array([[2, 3], [5, 6]]) •0:2: This selects rows from index 0 up to (but not including) index 2. So, it selects the first and second rows. •1:3: This selects columns from index 1 up to (but not including) index 3. So, it selects the second and third columns.
  • 20. import numpy as np b = np.array([[1, 2, 3], [4, 5, 6]]) print(b) element = b[1, 2] print(element) sub_array = b[0:2, 1:3] print(sub_array)
  • 21. Linear Algebra # Matrix multiplication A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) product = np.dot(A, B) # array([[19, 22], [43, 50]]) # Determinant determinant = np.linalg.det(A) # -2.0000000000000004 # Eigenvalues eigenvalues, eigenvectors = np.linalg.eig(A)
  • 22. import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) product = np.dot(A, B) print(product) determinant = np.linalg.det(A) print(determinant ) # Eigenvalues eigenvalues, eigenvectors = np.linalg.eig(A) print(eigenvalues) print(eigenvectors)
  • 23. Iterating NumPy arrays. 1. One dimensional array 2. Two dimensional array 3. Multi dimensional array
  • 24. Iterating over a one-dimensional NumPy array: import numpy as np # Create a 1D NumPy array arr_1d = np.array([1, 2, 3, 4, 5]) # Iterate over the elements for element in arr_1d: print(element)
  • 25. Iterating over a two-dimensional NumPy array: # Create a 2D NumPy array arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Iterate over rows for row in arr_2d: print("Row:", row) # You can also iterate over each element in the row for element in row: print(element)
  • 26. import numpy as np # Create a 3D NumPy array arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Use nditer to iterate over each element for element in np.nditer(arr_3d) : print(element) Iterating over a multi-dimensional NumPy array using nditer:
  • 27. import numpy as np # Create a 1D NumPy array arr_1d = np.array([1, 2, 3, 4, 5]) print(arr_1d.shape) # Iterate over the elements for element in arr_1d: print(element) # Create a 2D NumPy array arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) print(arr_2d.shape) # Iterate over rows for row in arr_2d: print("Row:", row) # You can also iterate over each element in the row for element in row: print(element) # Create a 3D NumPy array arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print(arr_3d.shape) # Use nditer to iterate over each element for element in np.nditer(arr_3d): print(element)
  • 28. Summary: 1.NumPy is essential for numerical and matrix computations in Python. 2.It offers powerful tools for creating and manipulating arrays. 3.Supports advanced mathematical and linear algebra operations.
  • 29. Exercises Exercise 1. Creating and Inspecting Arrays Problem: 1.Create a NumPy array with values from 10 to 50 (inclusive) with a step of 5. 2.Reshape the array into a 2D array with 3 rows and 3 columns. 3.Find the shape and data type of the array. 2.Create different types of NumPy arrays. Problem: 1.Create a one-dimensional array containing the numbers 1 to 10. 2.Create a two-dimensional array of size 3x3 containing random integers between 1 and 20. 3.Create a 5x5 identity matrix.
  • 30. Exercises 3.Perform basic operations on NumPy arrays. Problem : 1.Add 10 to every element in an array. 2.Multiply two arrays element-wise. 3.Find the dot product of two arrays. 4..Perform array indexing and slicing on a given array. Problem : 1.Create an array from 10 to 30 (inclusive). 2.Slice the array to get the first 5 elements. 3.Get all the even numbers from the array.
  • 31. Exercises 5.Iterate over a two-dimensional array. Problem : 1. Create a 2D array of shape 3x3 with values from 1 to 9. 2. Iterate over each row and print the row. 3. Iterate over each element of the array.

Editor's Notes

  • #9: np.array(list)
  • #13: As NumPy has been designed to be able to work with very large arrays, you could imagine performance and memory problems if NumPy insisted on always copying data. If you want a copy of a slice of an ndarray instead of a view, you will need to explicitly copy the array—for example, arr[5:8].copy().