unit-3
unit-3
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?
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.)
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])
Function Description
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.
Mathematical
Requires loops Direct operations possible
Operations
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.
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
# 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.