[go: up one dir, main page]

0% found this document useful (0 votes)
4 views34 pages

unit-3

The document provides comprehensive lecture notes on Unit III, focusing on the NumPy library in Python, covering its basics, computations, aggregations, comparisons, and advanced features like masks, Boolean arrays, fancy indexing, and sorting. It includes practical examples and explanations of key functions and operations within NumPy, emphasizing its importance in numerical computing and data analysis. The notes are structured with 2-mark and 5-mark questions to facilitate understanding and application of the concepts discussed.

Uploaded by

amalamargret.cse
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)
4 views34 pages

unit-3

The document provides comprehensive lecture notes on Unit III, focusing on the NumPy library in Python, covering its basics, computations, aggregations, comparisons, and advanced features like masks, Boolean arrays, fancy indexing, and sorting. It includes practical examples and explanations of key functions and operations within NumPy, emphasizing its importance in numerical computing and data analysis. The notes are structured with 2-mark and 5-mark questions to facilitate understanding and application of the concepts discussed.

Uploaded by

amalamargret.cse
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/ 34

Lecture Notes

Unit III

Prepared By:
Dr. M. Shanmugam
Mr. D. Rajesh
Mrs. M. Hemalatha
Mrs. S. Deeba
Ms. V. Swathilakshmi

Verified By Approved By
--------------------------------------------------------------------------------------------------------------
UNIT – 3 - Using Numpy
Basics of NumPy – Computation on NumPy – Aggregations – Computation on
Arrays – Comparisons – Masks and Boolean Arrays – Fancy Indexing – Sorting
Arrays – Structured Data: NumPy’s Structured Array.
2-MARKS:
1. What is NumPy, and why is it used?
NumPy (Numerical Python) is a Python library for numerical computing. It
provides a powerful N-dimensional array object, efficient mathematical
operations, and functions for linear algebra, random number generation,
and more. It is widely used in data science, machine learning, and
scientific computing.
2. How do you install NumPy?
NumPy can be installed using pip install numpy in the terminal or
command prompt. If using Anaconda, install it with conda install numpy.
3. How do you create a NumPy array?
A NumPy array can be created using numpy.array([elements])
Example:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
4. What are the key differences between a Python list and a NumPy
array?
NumPy arrays are faster, consume less memory, and support vectorized
operations, while Python lists require loops for element-wise operations.
5. How do you check the shape and size of a NumPy array?
Use .shape for dimensions and .size for total elements.
Example:
arr = np.array([[1, 2], [3, 4]])
print(arr.shape, arr.size) # Output: (2,2) 4
6. What are universal functions (ufuncs) in NumPy?
Universal functions perform element-wise operations on arrays, like
np.add(), np.subtract(), np.multiply(), and np.divide().
7. How do you perform element-wise addition of two arrays?
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = arr1 + arr2
print(result) # Output: [5 7 9]
8. What is broadcasting in NumPy?

Broadcasting allows operations between arrays of different shapes by


expanding smaller arrays. Example:
arr = np.array([1, 2, 3])
print(arr + 5) # Output: [6 7 8]
9. How do you compute the exponential and logarithm of an array?
arr = np.array([1, 2, 3])
print(np.exp(arr), np.log(arr))
10. How do you compute the dot product of two arrays?
Use np.dot() or @ operator:
a = np.array([1, 2])
b = np.array([3, 4])
print(np.dot(a, b)) # Output: 11
11. How do you find the sum of a NumPy array?
Use np.sum().
Example:
arr = np.array([1, 2, 3, 4])
print(np.sum(arr)) # Output: 10
12. How do you compute the mean of an array?
Use np.mean().
Example:
arr = np.array([10, 20, 30])
print(np.mean(arr)) # Output: 20
13. How do you compute the standard deviation of an array?
Use np.std().
Example:
arr = np.array([1, 2, 3])
print(np.std(arr))
14. How do you find the maximum and minimum of an array?
Use np.max() and np.min().
Example:
arr = np.array([4, 7, 1])
print(np.max(arr), np.min(arr))
15. What is cumulative sum and cumulative product?
Use np.cumsum() for sum and np.cumprod() for product.
16. How do you reshape a NumPy array?
Use .reshape().
Example:
arr = np.array([1, 2, 3, 4, 5, 6])
print(arr.reshape(2, 3))
17. What is the difference between ravel() and flatten()?
Both convert arrays to 1D, but flatten() returns a copy, while ravel()
returns a view.
18. How do you stack NumPy arrays?
Use np.hstack() for horizontal and np.vstack() for vertical stacking.
19. How do you split an array into multiple parts?
Use np.split().
Example:
arr = np.array([1, 2, 3, 4])
print(np.split(arr, 2))
20. How do you transpose a matrix in NumPy?

Use .T or np.transpose().
21. How do you compare two arrays element-wise?
Use comparison operators (>, <, ==, !=, etc.).
Example:
arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 2, 3])
print(arr1 == arr2) # Output: [False True True]
22. How do you find elements greater than a value in an array?
Use Boolean indexing.
Example:
arr = np.array([1, 2, 3, 4])
print(arr[arr > 2]) # Output: [3 4]
23. What is a Boolean mask in NumPy?
A Boolean mask is a condition applied to an array to filter
elements.
Example:
arr = np.array([1, 2, 3, 4])
mask = arr > 2
print(arr[mask]) # Output: [3 4]
24. How do you count the number of True values in a Boolean array?
Use np.count_nonzero() or np.sum().
Example:
bool_arr = np.array([True, False, True])
print(np.count_nonzero(bool_arr)) # Output: 2
25. How do you find unique values in a NumPy array?

Use np.unique().
Example:
arr = np.array([1, 2, 2, 3, 3])
print(np.unique(arr)) # Output: [1 2 3]
26. What is fancy indexing in NumPy?
Fancy indexing allows selecting multiple elements using an
index array.
Example:
arr = np.array([10, 20, 30, 40])
indices = [0, 2]
print(arr[indices]) # Output: [10 30]
27. How do you sort an array in NumPy?
Use np.sort().
Example:
arr = np.array([3, 1, 2])
print(np.sort(arr)) # Output: [1 2 3]
28. How do you get the indices of sorted elements?
Use np.argsort().
Example:
arr = np.array([3, 1, 2])
print(np.argsort(arr)) # Output: [1 2 0]
29. How do you sort a structured array by a specific field?
Use np.sort(arr, order='field_name') in a structured array.
Example:
dtype = [('name', 'U10'), ('age', int)]
data = np.array([('Alice', 25), ('Bob', 20)], dtype=dtype)
print(np.sort(data, order='age'))
30. How do you randomly shuffle an array?
Use np.random.shuffle(arr).
Example:
arr = np.array([1, 2, 3, 4])
np.random.shuffle(arr)
print(arr) # Output varies (e.g., [3 1 4 2])

5-MARKS:
1. What is NumPy, and why is it important in Python?
NumPy, short for Numerical Python, is a fundamental library for numerical and
scientific computing in Python. It provides support for large, multidimensional
arrays and matrices, along with a collection of high-level mathematical functions
to operate on these arrays efficiently.
Why is NumPy Important?
• Performance & Speed: NumPy arrays are much faster than Python lists
because they are implemented in C and use contiguous memory
allocation. This leads to faster computation.
• Memory Efficiency: NumPy uses less memory compared to traditional
lists. Since it stores homogeneous data in fixed-size memory blocks, it
reduces overhead.
• Mathematical Computations: It provides a wide range of functions for
linear algebra, statistics, and random number generation, making
mathematical operations easy.
• Interoperability: NumPy integrates seamlessly with other libraries such as
Pandas, Matplotlib, and SciPy, making it an essential part of data science
and machine learning workflows.
• Broadcasting: NumPy allows element-wise operations between arrays of
different shapes, which simplifies computations without explicit loops.
• Multidimensional Array Support: It provides a powerful ndarray object
that supports multi-dimensional data, unlike Python lists, which are one-
dimensional.
Example of NumPy Array Creation
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
(This code creates a NumPy array containing five elements.)

2. Explain Computation on NumPy with examples.


Computations in NumPy involve mathematical and logical operations performed
on arrays. NumPy is optimized for fast execution due to vectorization, which
eliminates the need for explicit loops.
Element-wise Operations
NumPy allows element-wise arithmetic operations on arrays, meaning
operations are performed directly on corresponding elements.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Element-wise operations
sum_array = a + b # [5, 7, 9]
diff_array = a - b # [-3, -3, -3]
prod_array = a * b # [4, 10, 18]
div_array = a / b # [0.25, 0.4, 0.5]
This approach is much faster than using traditional loops in Python.
Broadcasting in Computation
NumPy allows operations on arrays of different shapes through broadcasting.
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([10, 20, 30])

result = c + d
print(result)
Here, d is broadcasted to match the dimensions of c, and element-wise
addition is performed.
3. What are Aggregations in NumPy? Explain with examples.
Aggregation functions summarize the values in an array using operations like
sum, mean, min, and max.
Common Aggregation Functions
Function Description
np.sum() Computes sum of all elements
np.mean() Computes average (mean) value
np.min(), np.max() Returns minimum and maximum values
np.std() Computes standard deviation
np.var() Computes variance
EXAMPLE :
import numpy as np
arr = np.array([5, 10, 15, 20])
print(np.sum(arr)) # Output: 50
print(np.mean(arr)) # Output: 12.5
print(np.min(arr)) # Output: 5
print(np.max(arr)) # Output: 20
Aggregations are commonly used in data analysis.
4. How does NumPy perform comparisons on arrays?
NumPy provides element-wise comparison operations, returning Boolean
arrays.
Comparison Operators
• > Greater than
• < Less than
• >= Greater than or equal to
• <= Less than or equal to
• == Equal to
• != Not equal to
Example
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([2, 2, 3, 1])

print(a > b) # Output: [False, False, False, True]


print(a == b) # Output: [False, True, True, False]
This method is useful for filtering data
5. What are Masks and Boolean Arrays in NumPy? Explain with Examples.
Masks and Boolean arrays in NumPy are used to filter or manipulate data
efficiently. A mask is a Boolean array where each element is either True or False,
and it is used to extract, modify, or count specific elements in an array based on
a given condition.
NumPy makes use of Boolean indexing to work with masks, which is useful in
data analysis, machine learning, and scientific computing.
How Boolean Arrays Work
In NumPy, Boolean arrays are generated when a condition is applied to an array.
The result is an array of True and False values, where True represents elements
that satisfy the condition and False represents elements that do not.
Example 1: Creating a Boolean Mask
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
mask = arr > 25 # Condition-based mask
print(mask)
Output:
[False False True True True]
Here, NumPy creates a mask that marks values greater than 25 as True.
Filtering Elements Using Boolean Masks
Once we have a mask, we can use it to extract only the True values from the
original array.
Example 2: Applying a Mask to Extract Values
filtered_arr = arr[mask]
print(filtered_arr)
Output: [30 40 50] #Only the values that satisfy the condition (> 25)
Modifying Values Using Masks
We can also modify elements in an array using a mask.
Example 3: Modifying Values Based on a Condition
arr[arr > 25] = 100
print(arr)
Output:
[ 10 20 100 100 100]
All elements greater than 25 are replaced with 100.
6. Explain Fancy Indexing in NumPy with Examples.
Fancy indexing is a feature in NumPy that allows selecting multiple elements
from an array using index arrays. Unlike slicing, which selects contiguous
elements, fancy indexing allows arbitrary selections using an array of indices.
Fancy indexing is fast and eliminates the need for loops when extracting
multiple elements.
Basic Example of Fancy Indexing
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
indices = [0, 2, 4] # Selecting elements at positions 0, 2, and 4
result = arr[indices]
print(result)
Output:
[10 30 50]
Here, we extract elements at positions 0, 2, and 4 using an index list.
Fancy Indexing on Multi-Dimensional Arrays
Fancy indexing also works on 2D and 3D arrays.
Example: Fancy Indexing in a 2D Array
matrix = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
row_indices = [0, 2] # Selecting rows 0 and 2
column_indices = [1, 2] # Selecting columns 1 and 2
result = matrix[row_indices, column_indices]
print(result)
Output:
[20 90] #Here, matrix[0,1] (20) and matrix[2,2] (90) are extracted.
Using Fancy Indexing to Modify Arrays
We can modify specific elements in an array using fancy indexing.
Example: Changing Values Using Fancy Indexing
arr = np.array([10, 20, 30, 40, 50])
indices = [1, 3] # Modifying elements at positions 1 and 3
arr[indices] = [100, 200]
print(arr)
Output: [ 10 100 30 200 50]
Elements at indices 1 and 3 are replaced with 100 and 200.
7. Explain Sorting in NumPy with Different Sorting Techniques.
Sorting is the process of arranging elements in a particular order (ascending or
descending). NumPy provides efficient functions for sorting arrays quickly, which
is useful in data analysis and scientific computing.
Basic Sorting with np.sort()
The np.sort() function returns a sorted copy of an array while keeping the
original array unchanged.
import numpy as np
arr = np.array([5, 2, 9, 1, 7])
sorted_arr = np.sort(arr)
print(sorted_arr)
Output:
[1 2 5 7 9]
This sorts the array in ascending order.
Sorting in Descending Order
To sort in descending order, use [::-1] after sorting:
sorted_desc = np.sort(arr)[::-1]
print(sorted_desc)
Output:
[9 7 5 2 1]
In-Place Sorting with sort()
If you want to sort an array without creating a new copy, use:
arr.sort()
print(arr)
This modifies the original array.
Sorting Multi-Dimensional Arrays
For 2D arrays, sorting happens row-wise by default:
matrix = np.array([[3, 1, 2], [9, 7, 5]])
print(np.sort(matrix))
Output:
[[1 2 3]
[5 7 9]]
8. What is Structured Data in NumPy? Explain NumPy’s Structured Arrays.
Structured arrays in NumPy allow handling heterogeneous data types (e.g.,
names as strings, ages as integers). They are useful when working with datasets
similar to tables or databases.
Creating a Structured Array
A structured array is created using dtype:
import numpy as np
data = np.array([('Alice', 25, 55.5), ('Bob', 30, 70.2)],
dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
print(data)
Output:
[('Alice', 25, 55.5) ('Bob', 30, 70.2)]
Accessing Columns by Name
print(data['name']) # ['Alice' 'Bob']
print(data['age']) # [25 30]
Structured arrays work like a mini-database.
Modifying Elements
data['age'][0] = 26
print(data)
Output: [('Alice', 26, 55.5) ('Bob', 30, 70.2)]
This makes structured arrays useful for data processing and scientific
computing.
9. Explain NumPy’s Aggregation Functions with Examples.
Aggregation functions in NumPy are used to perform summary operations like
sum, mean, max, and min on arrays.
Common Aggregation Functions

Function Description

np.sum() Sum of elements

np.mean() Mean (average)

np.min() / np.max() Minimum and maximum values

np.median() Median value

np.std() Standard deviation

Example of Aggregation in 1D Array


arr = np.array([1, 2, 3, 4, 5])
print(np.sum(arr)) # 15
print(np.mean(arr)) # 3.0
print(np.min(arr)) # 1
print(np.max(arr)) # 5
Aggregation on 2D Arrays
You can aggregate along rows (axis=1) or columns (axis=0).
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.sum(matrix, axis=0)) # Column-wise sum: [5 7 9]
print(np.sum(matrix, axis=1)) # Row-wise sum: [6 15]
Aggregation functions are used in data analysis, machine learning, and
statistics.

10. Explain Computation on NumPy Arrays with Vectorized Operations.


NumPy allows vectorized operations, meaning operations apply directly on
arrays without loops. This makes computations faster and more efficient.
Basic Arithmetic Operations
Instead of using loops, NumPy allows operations like addition and
multiplication directly:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr * 2) # [2 4 6 8]
print(arr + 5) # [6 7 8 9]
This is much faster than using a loop.
Element-Wise Operations on Arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 + arr2) # [5 7 9]
print(arr1 * arr2) # [4 10 18]
These operations work element-wise.
Using NumPy’s Mathematical Functions
NumPy has built-in mathematical functions that operate on arrays efficiently.
arr = np.array([0, np.pi/2, np.pi])
print(np.sin(arr)) # [0. 1. 0.]
print(np.exp(arr)) # [1. 1.64872 23.1407]
print(np.log(arr + 1)) # [0. 0.6931 1.0986]
Matrix Computations Using NumPy
For matrix operations, NumPy provides:
A = np.array([[1, 2], [3, 4]])
B = np.array([[2, 0], [1, 2]])
print(np.dot(A, B)) # Matrix multiplication
Vectorized computations make NumPy powerful for scientific computing and
AI applications.

10-MARKS:
1. Explain NumPy and its advantages over Python lists.
NumPy (Numerical Python) is a fundamental library for numerical computing in
Python. It provides support for multi-dimensional arrays and mathematical
functions to perform fast operations on large datasets.
Python lists are widely used for storing data, but they have performance and
memory limitations when dealing with large amounts of numerical data. NumPy
overcomes these limitations by providing efficient array operations and
vectorized computation.
Key Features of NumPy
• Efficient Storage – NumPy arrays require less memory compared to
Python lists.
• Fast Computation – Uses C-based implementation for high-speed
calculations.
• Vectorized Operations – Operations are applied to entire arrays at once,
avoiding explicit loops.
• Support for Mathematical Functions – Includes functions like
trigonometric, statistical, linear algebra, and Fourier transformation
operations.
• Multi-Dimensional Arrays – Supports 1D, 2D, and n-dimensional arrays
for complex data processing.
• Broadcasting – Allows operations on arrays of different shapes without
looping.

Differences Between NumPy and Python Lists

Feature Python List NumPy Array

Faster due to vectorized


Performance Slower due to loops
operations

Memory Usage Takes more memory Optimized memory usage

Can store different Stores only one data type (int,


Data Type
data types float, etc.)

Mathematical
Requires loops Direct operations possible
Operations

Multi-Dimensional Built-in support for multi-


Needs nested lists
Support dimensional arrays

Advanced functions for data


Functionality Basic list operations
processing

Example: Python List vs NumPy Array


1. Memory Efficiency
import numpy as np
import sys
list1 = [1, 2, 3, 4, 5]
arr1 = np.array([1, 2, 3, 4, 5])
print(sys.getsizeof(list1)) # 120 bytes (Python list)
print(sys.getsizeof(arr1)) # 80 bytes (NumPy array)
NumPy arrays take less memory than Python lists.

2. Speed Comparison
import time
size = 1000000
list1 = list(range(size))
arr1 = np.array(list1)
# Time taken for list multiplication
start = time.time()
list_result = [x * 2 for x in list1]
end = time.time()
print("List Time:", end - start)
# Time taken for NumPy array multiplication
start = time.time()
arr_result = arr1 * 2
end = time.time()
print("NumPy Time:", end - start)
NumPy executes operations faster than Python lists.

3. Vectorized Operations in NumPy


Python lists require loops for mathematical operations, while NumPy performs
vectorized computations.
list1 = [1, 2, 3, 4]
arr1 = np.array([1, 2, 3, 4])
# List operation (loop required)
list_result = [x * 2 for x in list1]
# NumPy operation (direct multiplication)
arr_result = arr1 * 2

print(list_result) # [2, 4, 6, 8]
print(arr_result) # [2 4 6 8]
NumPy is a powerful tool for numerical computation, offering high-speed
operations, optimized memory usage, and rich mathematical functions. It is
widely used in data science, machine learning, and scientific computing.
2. Explain Computation on NumPy Arrays with Examples.
NumPy allows fast mathematical operations on arrays without loops.
Computations in NumPy are performed using vectorized operations, which
utilize optimized C-based functions.
Basic Arithmetic Operations
NumPy allows element-wise operations on arrays:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr + 5) # [6 7 8 9]
print(arr * 2) # [2 4 6 8]
print(arr - 1) # [0 1 2 3]
print(arr / 2) # [0.5 1. 1.5 2. ]
Each operation is applied to all elements simultaneously.
Mathematical Functions in NumPy
NumPy provides built-in mathematical functions such as trigonometric,
logarithmic, and exponential functions.
Trigonometric Functions
arr = np.array([0, np.pi/2, np.pi])
print(np.sin(arr)) # [0. 1. 0.]
print(np.cos(arr)) # [1. 0. -1.]
print(np.tan(arr)) # [0. 1.633... 0.]
Exponential and Logarithm Functions
arr = np.array([1, 2, 3])
print(np.exp(arr)) # [2.718 7.389 20.085]
print(np.log(arr)) # [0. 0.693 1.098]
These functions are used in scientific computations and machine learning.
Element-wise Computation
NumPy allows array-to-array computations without loops.
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 + arr2) # [5 7 9]
print(arr1 * arr2) # [4 10 18]
print(arr1 / arr2) # [0.25 0.4 0.5]
Aggregation Functions (Sum, Mean, Min, Max)
Aggregation functions summarize data quickly.
arr = np.array([1, 2, 3, 4, 5])
print(np.sum(arr)) # 15
print(np.mean(arr)) # 3.0
print(np.min(arr)) # 1
print(np.max(arr)) # 5
print(np.std(arr)) # 1.41 (Standard Deviation)
These functions help in data analysis.
Matrix Computations
NumPy supports matrix operations, such as dot products and transposition.
Matrix Multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
C = np.dot(A, B)
print(C) # [[19 22]
# [43 50]]
Transpose of a Matrix
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.T) # [[1 4]
# [2 5]
# [3 6]]
Broadcasting in NumPy
Broadcasting allows operations on arrays of different shapes.
arr = np.array([[1, 2, 3], [4, 5, 6]])
scalar = 2
print(arr + scalar)
# [[3 4 5]
# [6 7 8]]
NumPy automatically expands the scalar to match the array dimensions.
NumPy’s computation capabilities are powerful and efficient, allowing for fast
mathematical operations and optimized data processing. These operations are
widely used in machine learning, data science, and numerical simulations
3. Explain Aggregations in NumPy with examples.
Aggregation refers to the process of summarizing or computing statistical values
from an array, such as sum, mean, minimum, maximum, standard deviation,
and variance. These operations help in data analysis, scientific computations,
and machine learning.
NumPy provides built-in aggregation functions, which are optimized for fast
execution compared to Python loops.
Types of Aggregation Functions in NumPy

Function Description

np.sum() Returns the sum of array elements

np.mean() Computes the mean (average) of the elements

np.min() Finds the minimum value in an array

np.max() Finds the maximum value in an array

np.std() Computes the standard deviation

np.var() Computes the variance

np.median() Computes the median value

np.prod() Returns the product of array elements

np.cumsum() Returns the cumulative sum of elements

np.cumprod() Returns the cumulative product of elements

Basic Aggregation Examples


import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print("Sum:", np.sum(arr)) # 15
print("Mean:", np.mean(arr)) # 3.0
print("Min:", np.min(arr)) # 1
print("Max:", np.max(arr)) # 5
print("Standard Deviation:", np.std(arr)) # 1.41
print("Variance:", np.var(arr)) # 2.0
NumPy executes these operations efficiently without requiring loops.
Aggregation Across Different Axes
For multi-dimensional arrays, aggregations can be performed along rows
(axis=1) or columns (axis=0).
arr2D = np.array([[1, 2, 3],
[4, 5, 6]])
print("Sum (All Elements):", np.sum(arr2D)) # 21
print("Sum (Column-wise):", np.sum(arr2D, axis=0)) # [5 7 9]
print("Sum (Row-wise):", np.sum(arr2D, axis=1)) # [6 15]
• axis=0 → Sum along columns
• axis=1 → Sum along rows
Cumulative Aggregation (Cumulative Sum and Product)
Cumulative operations are useful for time-series analysis.
arr = np.array([1, 2, 3, 4])
print("Cumulative Sum:", np.cumsum(arr)) # [1 3 6 10]
print("Cumulative Product:", np.cumprod(arr)) # [1 2 6 24]
Each element in the result is the sum/product of the current and all previous
elements.
Median and Percentile Calculation
The median is the middle value of a dataset, while percentiles divide data into
specific proportions.
arr = np.array([1, 3, 5, 7, 9])
print("Median:", np.median(arr)) # 5
print("25th Percentile:", np.percentile(arr, 25)) # 3
print("75th Percentile:", np.percentile(arr, 75)) # 7
Percentiles help in statistical analysis and machine learning models.
NumPy provides optimized aggregation functions that allow fast computation of
summarized statistics from arrays. These functions are widely used in data
analysis, image processing, and machine learning.
4. Explain Computation on Arrays in NumPy with examples.
NumPy supports fast mathematical operations on arrays, including:
• Element-wise arithmetic operations
• Universal functions (ufuncs)
• Trigonometric, exponential, and logarithmic operations
• Linear algebra functions
NumPy eliminates loops by performing operations in a vectorized manner,
making it faster than traditional Python lists.
Element-wise Arithmetic Operations
Operations like addition, subtraction, multiplication, and division are
performed directly on arrays.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print("Addition:", arr1 + arr2) # [5 7 9]
print("Subtraction:", arr1 - arr2) # [-3 -3 -3]
print("Multiplication:", arr1 * arr2) # [4 10 18]
print("Division:", arr1 / arr2) # [0.25 0.4 0.5]
Each operation is applied element-wise.
Universal Functions (ufuncs)
NumPy Universal Functions (ufuncs) allow for fast element-wise operations.
arr = np.array([1, 2, 3, 4])
print("Square Root:", np.sqrt(arr)) # [1. 1.414 1.732 2.]
print("Power (2):", np.power(arr, 2)) # [1 4 9 16]
print("Absolute Value:", np.abs([-1, -2, 3])) # [1 2 3]
These functions operate faster than traditional loops.
Trigonometric Operations
NumPy provides functions to compute sin, cos, tan, arcsin, arccos, arctan.
arr = np.array([0, np.pi/2, np.pi])
print("Sin:", np.sin(arr)) # [0. 1. 0.]
print("Cos:", np.cos(arr)) # [1. 0. -1.]
print("Tan:", np.tan(arr)) # [0. 1.633 0.]
These are useful in engineering and physics calculations.
Exponential and Logarithmic Functions
NumPy includes exponential (exp) and logarithm (log) functions.
arr = np.array([1, 2, 3])
print("Exponential:", np.exp(arr)) # [2.718 7.389 20.085]
print("Natural Log:", np.log(arr)) # [0. 0.693 1.098]
print("Log Base 10:", np.log10(arr)) # [0. 0.301 0.477]
These functions are useful in financial and scientific applications.
Linear Algebra Operations
NumPy supports matrix operations like dot product, determinant, inverse.
Matrix Multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
C = np.dot(A, B)
print("Matrix Multiplication:\n", C)
Matrix Transpose
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print("Transpose:\n", matrix.T)
Inverse of a Matrix
from numpy.linalg import inv
A = np.array([[1, 2], [3, 4]])
print("Inverse:\n", inv(A))

5. Explain Comparisons in NumPy with examples.


NumPy allows element-wise comparisons between arrays using relational
operators like:
• > (greater than)
• < (less than)
• >= (greater than or equal to)
• <= (less than or equal to)
• == (equal to)
• != (not equal)
These comparisons return Boolean arrays, where True represents a condition
that is met, and False represents a condition that is not met.
Comparisons are useful in data filtering, decision-making, and machine
learning preprocessing.
Element-wise Comparisons
Comparing two arrays of the same shape results in a Boolean array.
import numpy as np
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([3, 2, 1, 4, 5])
print(arr1 > arr2) # [False False True False False]
print(arr1 < arr2) # [ True False False False False]
print(arr1 == arr2) # [False True False True True]
Each element is compared individually, producing a Boolean result.
Comparisons with Scalars
A scalar value can be compared with an entire NumPy array.
arr = np.array([10, 20, 30, 40, 50])
print(arr > 25) # [False False True True True]
print(arr == 10) # [ True False False False False]
The result is a Boolean array, where each element is checked against the scalar.
Combining Multiple Conditions
Logical operations such as & (AND), | (OR), and ~ (NOT) can be applied.
arr = np.array([10, 20, 30, 40, 50])
print((arr > 20) & (arr < 50)) # [False False True True False]
print((arr == 10) | (arr == 50)) # [ True False False False True]
print(~(arr < 30)) # [False False True True True]
• & (AND) returns True if both conditions are True.
• | (OR) returns True if at least one condition is True.
• ~ (NOT) inverts the Boolean values.
np.where() for Conditional Selection
np.where(condition, true_value, false_value) returns values based on a
condition.
arr = np.array([10, 20, 30, 40, 50])
result = np.where(arr > 25, "High", "Low")
print(result) # ['Low' 'Low' 'High' 'High' 'High']
• If arr[i] > 25, it is labeled "High".
• Otherwise, it is labeled "Low".
Finding Indices of Matching Elements
np.where() can also be used to find indices of elements that match a condition.
arr = np.array([5, 10, 15, 20, 25])
indices = np.where(arr > 10)
print(indices) # (array([2, 3, 4]),)
The result is an array of indices where the condition holds.

6. Explain Masks and Boolean Arrays in NumPy with examples.


A mask in NumPy is a Boolean array that determines which elements should
be selected or modified.
Boolean arrays (True/False) are used to filter or manipulate data dynamically.
Creating Boolean Masks
A mask is simply a Boolean array that represents conditions applied to a
NumPy array.
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
mask = arr > 25
print(mask) # [False False True True True]
This Boolean mask can be used to filter values.
Using Boolean Masks for Filtering
Masks can select specific elements from an array.
arr = np.array([10, 20, 30, 40, 50])
mask = arr > 25
filtered_arr = arr[mask]
print(filtered_arr) # [30 40 50]
Only elements where mask == True are selected.
Modifying Elements Using Masks
Masks can be used to update values.
arr = np.array([10, 20, 30, 40, 50])
arr[arr > 25] = 100
print(arr) # [ 10 20 100 100 100]
All values greater than 25 are replaced with 100.
Using np.where() with Masks
We can use np.where() to apply conditional replacements.
arr = np.array([10, 20, 30, 40, 50])
new_arr = np.where(arr > 25, 999, arr)
print(new_arr) # [ 10 20 999 999 999]
Elements greater than 25 are replaced with 999, while others remain
unchanged.
Combining Multiple Conditions with Masks
Multiple conditions can be combined using & (AND) and | (OR).
arr = np.array([10, 20, 30, 40, 50])
mask = (arr > 10) & (arr < 50)
print(arr[mask]) # [20 30 40]
Only values greater than 10 and less than 50 are selected.
Using np.nonzero() to Find Matching Indices
np.nonzero() returns the indices where a condition is True.
arr = np.array([5, 10, 15, 20, 25])
indices = np.nonzero(arr > 10)
print(indices) # (array([2, 3, 4]),)
The indices [2, 3, 4] contain values greater than 10.
print(count) # 3
There are 3 elements greater than 25.
Masks and Boolean arrays allow for powerful filtering, modifications, and
logical operations on NumPy arrays. They are widely used in data science,
machine learning, and numerical computing.
7. Explain Fancy Indexing in NumPy with examples.
Fancy indexing in NumPy allows you to access multiple elements of an array
simultaneously using integer arrays or Boolean masks. It enables advanced
indexing operations without using loops, making data selection and
manipulation more efficient.
Fancy Indexing Using Integer Arrays
Instead of selecting elements one by one, we can pass an array of indices to
extract multiple elements at once.
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
indices = [0, 2, 4] # Select elements at positions 0, 2, and 4
result = arr[indices]
print(result) # Output: [10 30 50]
Here, the elements at indices 0, 2, and 4 are selected in a single step.
Fancy Indexing in 2D Arrays
We can use fancy indexing in multi-dimensional arrays to extract specific rows
or columns.
arr2D = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
row_indices = [0, 2] # Selecting rows 0 and 2
print(arr2D[row_indices])
# Output:
# [[10 20 30]
# [70 80 90]]
Here, rows at index 0 and 2 are extracted.
Similarly, we can extract specific columns:
col_indices = [0, 2] # Selecting columns 0 and 2
print(arr2D[:, col_indices])

# Output:
# [[10 30]
# [40 60]
# [70 90]]
The : selects all rows, while [0,2] extracts only the first and third columns.
Fancy Indexing with Negative Indices
We can also use negative indices to select elements.
arr = np.array([10, 20, 30, 40, 50])
indices = [-1, -3, -5] # Select the last, third-last, and first element
print(arr[indices]) # Output: [50 30 10]
Negative indices allow us to select elements from the end of the array.
Fancy Indexing with Repeated Indices
We can repeat indices to get multiple copies of the same element.
arr = np.array([100, 200, 300, 400, 500])
indices = [1, 3, 3, 0] # Selecting elements at 1st, 3rd, 3rd, and 0th index
print(arr[indices]) # Output: [200 400 400 100]
The third element (400) is repeated twice because index 3 appears twice in
indices.
Fancy Indexing with Boolean Masks
Fancy indexing can be combined with Boolean arrays to filter elements based
on conditions.
arr = np.array([10, 20, 30, 40, 50])
mask = arr > 25 # Condition to select elements greater than 25
print(arr[mask]) # Output: [30 40 50]
Here, elements greater than 25 are automatically selected.
Fancy indexing allows fast and flexible data selection in NumPy. It is widely used
in data preprocessing, deep learning, and machine learning for extracting
specific elements, filtering datasets, and reshaping arrays.

8. Explain Sorting Arrays in NumPy with examples.


Sorting is an important operation in data processing. NumPy provides efficient
sorting algorithms to arrange elements in ascending or descending order.
Using np.sort() for Sorting
The np.sort() function sorts an array in ascending order by default.
import numpy as np
arr = np.array([5, 2, 8, 1, 9])
sorted_arr = np.sort(arr)
print(sorted_arr) # Output: [1 2 5 8 9]
This function returns a sorted copy while keeping the original array unchanged.
Sorting in Descending Order
To sort in descending order, we can use [::-1] after sorting.
arr = np.array([5, 2, 8, 1, 9])
sorted_desc = np.sort(arr)[::-1]
print(sorted_desc) # Output: [9 8 5 2 1]
Reversing the sorted array gives the elements in descending order.
Sorting 2D Arrays (Row-wise and Column-wise)
We can sort individual rows or columns using axis=0 (column-wise) or axis=1
(row-wise).
arr2D = np.array([[3, 2, 1],
[9, 8, 7]])
sorted_rows = np.sort(arr2D, axis=1) # Sort each row
print(sorted_rows)
# Output:
# [[1 2 3]
# [7 8 9]]
sorted_cols = np.sort(arr2D, axis=0) # Sort each column
print(sorted_cols)
# Output:
# [[3 2 1]
# [9 8 7]]
Sorting along axis=1 sorts each row, while axis=0 sorts each column.
Using np.argsort() to Get Sorted Indices
Instead of sorting values, np.argsort() returns the indices of sorted elements.
arr = np.array([50, 20, 40, 10, 30])
indices = np.argsort(arr)
print(indices) # Output: [3 1 4 2 0]
The smallest element (10) is at index 3, and the largest element (50) is at index
0.
We can reorder elements using these indices:
sorted_arr = arr[indices]
print(sorted_arr) # Output: [10 20 30 40 50]
Sorting Structured Data (Sorting by Multiple Columns)
NumPy can sort structured arrays based on multiple fields.
data = np.array([(25, 'Alice'), (30, 'Bob'), (22, 'Charlie')],
dtype=[('age', int), ('name', 'U10')])
sorted_data = np.sort(data, order='age') # Sort by age
print(sorted_data)
This sorts the records based on the 'age' field.
Using np.partition() for Partial Sorting
If we need only the smallest k elements, we can use np.partition().
arr = np.array([50, 20, 40, 10, 30])
partitioned = np.partition(arr, 2) # First 2 smallest elements
print(partitioned) # Output: [10 20 30 50 40]
The first two elements are the smallest, but the rest are not necessarily
sorted.
Sorting is essential in data analysis, searching, and ranking operations.
NumPy’s sorting functions like np.sort(), np.argsort(), and np.partition() provide
fast and efficient sorting capabilities.

You might also like