U4 NumPy&MatplotLib Presentation
U4 NumPy&MatplotLib Presentation
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 2 11/18/24 4
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
Creating Arrays
NumPy Array
• Arrays are the core data structure and are used
extensively for numerical computing.
Array Attributes
Creating Arrays
#1D Array: • Shape: [Link] #returns tuple
import numpy as np
arr = [Link]([1, 2, 3, 4]) • Size: [Link] #total elements
• Dimensions: [Link]
#2D Array (Matrix):
arr2 = [Link]([[1, 2], [3, 4]]) • Data Type: [Link]
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 6 11/18/24 8
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
Dimensions: [Link] & Data Type: [Link]
- Shape: [Link]
Output:
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 9 11/18/24 11
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
Size: [Link]
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 10 11/18/24 12
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
dtype
ü dtype stands for "data type” Array Indexing
ü represents the type of elements that are stored in a
NumPy array. Access elements using indices:
ü Every NumPy array has a dtype associated with it, arr = [Link]([10, 20, 30])
which tells NumPy how to interpret the data in the print(arr[0])
array's memory.
Multi-dimensional indexing:
arr2 = [Link]([[1, 2], [3, 4]])
print(arr2[1, 0])
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 13 11/18/24 15
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
Array Slicing
Output:
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 14 11/18/24 16
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
# Accessing elements in a 1D array
Slicing a 2D Array arr = [Link]([1, 2, 3, 4, 5])
• Access the first element (index 0) print(arr[0])
1
• Access the last element (index -1) print(arr[-1])
5
• Access a slice of the array (elements print(arr[1:4])
[2 3 4]
from index 1 to 3)
# Element-wise addition
add_result = arr1 + arr2
Addition: [5 7 9]
print("Addition:", add_result)
# Element-wise subtraction
sub_result = arr1 - arr2
print("Subtraction:", sub_result) Subtraction: [-3 -3 -3]
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 21 11/18/24 23
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 22 11/18/24 24
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
Array Math Operations # Square root of each element
Assignment
sqrt_result = [Link](arr1)
PCU/Python/UnitIV/NumPyMatPlot/Namita
# Minimum element in anPCU/Python/UnitIV/NumPyMatPlot/Namita
array
11/18/24 25 11/18/24 27
Chawla/Batch24/MCA/SemI min_result = [Link](arr1) Chawla/Batch24/MCA/SemI
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 26 11/18/24 28
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
arr1 = [Link]([[1, 2, 3], [4, 5, 6]]) #Shape (2,3)
arr2 = [Link]([10, 20, 30]) #Shape (3,)
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 29 11/18/24 31
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 33 11/18/24 35
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 37 11/18/24 39
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 38 11/18/24 40
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
Image…
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 41 11/18/24 43
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
Customizing Plots
- Change line styles and colors:
[Link](x, y, linestyle='--', color='g')
- Add legends:
[Link](['Data Line'])
- Add gridlines:
[Link](True)
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 12, 15, 20, 25]
# Adding a legend
#[Link](['Data Line', 'Meta'])
[Link]()
# Adding gridlines
Thank You… !!
[Link](True)
Reshaping Arrays
• Changing array shape with reshape():
• arr = [Link]([1, 2, 3, 4])
• reshaped = [Link](2, 2)
• Resulting shape: (2, 2) matrix.
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 46 11/18/24 48
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI