0% found this document useful (0 votes)
9 views

U4 NumPy&MatplotLib Presentation

Uploaded by

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

U4 NumPy&MatplotLib Presentation

Uploaded by

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

What is NumPy?

- NumPy stands for Numerical Python.


- It is a core library for numerical computing in
Unit- IV Python.
Introduction to NumPy - Provides support for arrays, matrices, and
high-level mathematical functions.
Powerful Library for Numerical - Linear Algebra Operations: Matrix multiplication,
Computing in Python determinant calculation
- Statistical Functions: Mean, Median, Mode
- Mathematical Operations: Exponential, logarithmic,
trigonometric (e.g. np.exp(), np.log())
- Random Sampling Functions
11/18/24
PCU/Python/UnitIV/NumPyMatPlot/Namita
Chawla/Batch24/MCA/SemI
1 11/18/24 - Fourier Transforms Functions… etc
PCU/Python/UnitIV/NumPyMatPlot/Namita
Chawla/Batch24/MCA/SemI
3

Topics will be covered:


Installation
• What is NumPy • Install using pip:
• NumPy installation pip install numpy
• Arrays
• Array indexing
• Verify the Installation:
• Array vs Listing Data types
• Array Math
• Array Broadcasting

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.

• known as ndarrays Output:


(short for N-dimensional arrays)

• While they can store non- mathematical


information such as strings, they exist mainly to
manage and facilitate operations with data that is
numeric in nature.
Output:
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 5 11/18/24 7
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI

Array Attributes
Creating Arrays
#1D Array: • Shape: arr.shape #returns tuple
import numpy as np
arr = np.array([1, 2, 3, 4]) • Size: arr.size #total elements

• Dimensions: arr.ndim
#2D Array (Matrix):
arr2 = np.array([[1, 2], [3, 4]]) • Data Type: arr.dtype

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: arr.ndim & Data Type: arr.dtype
- Shape: arr.shape

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: arr.size

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 = np.array([10, 20, 30])
which tells NumPy how to interpret the data in the print(arr[0])
array's memory.

Multi-dimensional indexing:
arr2 = np.array([[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 = np.array([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)

# Accessing elements in a 2D array (matrix)


matrix = np.array([[1, 2, 3], [4, 5, 6]])
• Access the element at row 0, column 0 print(matrix[0,
1 0])
• Access the element at row 1, column 2 print(matrix[1,
6 2])
• Access the entire first row print(matrix[0,
[1 2 3] :])
• Access the entire second column print(matrix[:,
[2 5] 1])

# Accessing elements in a 3D array


tnsr_3d = np.array([[[1, 2], [3, 4]], [[5,
6], [7, 8]]])
• Access the element at depth 0, row 0, print(tnsr_3d[0, 1 0, 0])
column 0
• Access the element at depth 1, row 1, print(tnsr_3d[1, 8 1, 1])
column 1
• Access the entire first row across all print(tnsr_3d[:, [[1 2] 0, :])
depths [5 6]]
11/18/24 the entire second column at depth print(tnsr_3d[0, :,
19 1])
PCU/Python/UnitIV/NumPyMatPlot/Namita • Access PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 17
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
0 [2 4]

Array Math Operations


arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

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

Output è # Element-wise multiplication


multipli_res = arr1 * arr2
print("Multiplication:", multipli_res) Multiplication: [ 4 10 18]
# Element-wise division
div_result = arr1 / arr2
print("Division:", div_result) Division: [0.25 0.4 0.5 ]
# Dot product of two arrays
dot_product_res = np.dot(arr1, arr2)
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24
Chawla/Batch24/MCA/SemI
18 print("Dot
11/18/24 Product:", dot_product_res)
Chawla/Batch24/MCA/SemI Dot Product: 32 20
Array Math Operations - ndarray
arr1 = np.array([[1, 2, 3], [7, 8, 9]])
arr2 = np.array([[4, 5, 6], [5, 6, 7]])
# Element-wise addition Addition:
add_result = arr1 + arr2 [[ 5 7 9]
print("Addition:", add_result) [12 14 16]]

# Element-wise subtraction Subtraction:


sub_result = arr1 - arr2 [[-3 -3 -3]
print("Subtraction:", sub_result) [ 2 2 2]]

# Element-wise multiplication Multiplication:


multi_res = arr1 * arr2 [[ 4 10 18]
print("Multiplication:", multi_res) [35 48 63]]

# Element-wise division Division:


div_result = arr1 / arr2 [[0.25 0.4 0.5 ]
print("Division:", div_result) [1.4 1.33333333 1.28571429]]

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 = np.sqrt(arr1)

# Exponential of each element #(e**x, e= 2.718)


exp_result = np.exp(arr1)

# Sine of each element


sin_result = np.sin(arr1)

# Sum of all elements in an array


sum_result = np.sum(arr1)

# Mean of all elements in an array


mean_result = np.mean(arr1)

# Standard deviation of all elements in an array


std_result = np.std(arr1)

Output à # Maximum element in an array


max_result = np.max(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 = np.min(arr1) Chawla/Batch24/MCA/SemI

Mathematical Functions Broadcasting


• Examples: Allows operations on arrays of different shapes.
• - Sum: np.sum(arr) Example:
• - Mean: np.mean(arr)
• - Square Root: np.sqrt(arr)
• Vectorized operations for efficiency.

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 = np.array([[1, 2, 3], [4, 5, 6]]) #Shape (2,3)
arr2 = np.array([10, 20, 30]) #Shape (3,)

result = arr1 + arr2


print(result)

Before performing addition, NumPy "stretches"


arr2 to match the shape of arr1. It behaves as if
Output à arr2 becomes:

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

It "stretches" the dimensions of smaller arrays


to match the shape of larger arrays so that
element-wise operations

arr1 has shape (3, 1) and arr2 has shape (3,).


Here, the scalar 5 is "stretched" to match the
shape of arr (treated as [5, 5, 5]), allowing the
arr2 is "stretched" to shape (1, 3), then both arrays
element-wise addition.
11/18/24
PCU/Python/UnitIV/NumPyMatPlot/Namita
Chawla/Batch24/MCA/SemI
30
become compatible for element-wise multiplication.
11/18/24
PCU/Python/UnitIV/NumPyMatPlot/Namita
Chawla/Batch24/MCA/SemI
32
Matplotlib

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

What is Matplotlib? Creating a Basic Plot

• Matplotlib is a popular Python library used for


creating static, animated, and interactive
visualizations.

• used in data science, machine learning, and


scientific computing for its powerful plotting
capabilities and flexibility in generating a wide
variety of plots and charts.

• Matplotlib is designed to work seamlessly with


libraries like NumPy and Pandas, making it a go-to
choice for visualizing data.
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 34 11/18/24 36
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
subplot

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

plt.title("Simple Line Plot")


plt.xlabel("X-axis")
plt.ylabel("Y-axis")

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:
plt.plot(x, y, linestyle='--', color='g')

- Add legends:
plt.legend(['Data Line'])

- Add gridlines:
plt.grid(True)

- Add titles and labels: `plt.title('Title'),


plt.xlabel('X'), plt.ylabel('Y')`
PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 42 11/18/24 44
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI
import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 12, 15, 20, 25]

x1 = [15, 25, 35, 45, 55]


y1 = [10, 12, 15, 20, 25]

# Creating a basic plot


plt.plot(x, y, linestyle='--', color='g', label='Line1 Data')
plt.plot(x1, y1, linestyle='--', color='r', label='Line2 Data')

# Adding a legend
#plt.legend(['Data Line', 'Meta'])
plt.legend()

# Adding gridlines
Thank You… !!
plt.grid(True)

# Adding title and labels


plt.title('Simple Plot Example')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

# Displaying the plot


plt.show() PCU/Python/UnitIV/NumPyMatPlot/Namita PCU/Python/UnitIV/NumPyMatPlot/Namita
11/18/24 45 11/18/24 47
Chawla/Batch24/MCA/SemI Chawla/Batch24/MCA/SemI

Reshaping Arrays
• Changing array shape with reshape():
• arr = np.array([1, 2, 3, 4])
• reshaped = arr.reshape(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

You might also like