Lab Session 02: The Shape and Reshaping of Numpy Array:: / / 10:20am To 01:00Pm
Lab Session 02: The Shape and Reshaping of Numpy Array:: / / 10:20am To 01:00Pm
NO:238W1A5446 23AI&DS4354 DATA SCIENCE USING PYTHON LABORATORY ACADEMIC YEAR: 2024-2025
.size: Returns the total number of elements in the array (product of the dimensions).
.ndim: Returns the number of dimensions (axes) of the array.
Example:
Given the array np.array([[1, 2, 3], [4, 5, 6]]):
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
print(arr.size)
print(arr.ndim)
Output:
(2, 3)
6
2
2. Why does reshaping a NumPy array require the total number of elements to remain constant?
A: Example: If you have a 1D array with 12 elements, can you reshape it into a (3, 5) array? Why or
why not?
Reshaping a NumPy array requires the total number of elements to remain constant because the
underlying data is not changed, only its layout.
For example, a 1D array with 12 elements cannot be reshaped into a (3, 5) array because 3 * 5 = 15,
which doesn't match the original 12 elements.
3. What are the potential issues when reshaping or flattening a NumPy array?
A: Example: Given the array arr = np.array([[1, 2], [3, 4], [5, 6]]), what would happen if you attempt
arr.reshape(4, 2)?
The potential issue when reshaping a NumPy array is that the total number of elements must remain the
same. If you try to reshape an array into a shape that requires a different number of elements, NumPy
will raise a ValueError.
For example, reshaping a 6-element array into shape (4, 2) would cause an error because it requires
8 elements.
4. What is the purpose of flattening an array, and when might it be necessary in real-world
applications?
A: Example: Consider a 2D image represented as a 2D array. How would flattening help in image
processing tasks?
Flattening an array is useful for transforming multi-dimensional data into a single line of values,
making it easier to process or input into algorithms that require 1D data.
In Image Processing:
For Machine Learning: Models like neural networks require image data to be flattened into 1D
arrays before processing, because they typically work with vectorized data.
Data Transformation: Flattening makes it easier to manipulate or transform the image, e.g., by
applying mathematical operations or reshaping it for certain tasks.
This flattened array can then be used in models, stored, or transformed more easily.
Output:
2
Output:
(2, 3)
Output:
6
Output:
[[1 2 3]
[4 5 6]]
Output:
[1 2 3 4 5 6]
print(transposed_arr)
Output:
[[1 4]
[2 5]
[3 6]]
Example: Create a 2D array of shape (2, 3) and flatten it. Verify if the results match a 1D array
created using np.array([elements]).
A 1D array is a single-dimensional array with one axis, while a flattened 2D array is a 1D array
obtained by converting a 2D array into a single sequence of elements.
Example:
import numpy as np
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
flattened_arr = arr_2d.flatten()
arr_1d = np.array([1, 2, 3, 4, 5, 6])
print(np.array_equal(flattened_arr, arr_1d))
Both the flattened array and the manually created 1D array contain the same elements and are equal.
Example: Given arr = np.arange(12), reshape it into a (2, 2, 3) array and explain each step.
Output:
[[[ 0 1 2]
[ 3 4 5]]
[[ 6 7 8]
[ 9 10 11]]]
3. Transpose a 2D array and verify its result by comparing rows and columns.
Example: Given arr = np.array([[1, 2, 3], [4, 5, 6]]), calculate the transpose and explain how the
rows become columns.
Output:
[[1 4]
[2 5]
[3 6]]
Explanation:
The first row [1, 2, 3] becomes the first column [1, 4].
The second row [4, 5, 6] becomes the second column [2, 5].
4. Why might flattening and reshaping operations be important in machine learning pipelines?
Example: Consider a dataset where each sample is a 2D array. Why would you need to flatten the
samples before inputting them into a linear model?
Flattening and reshaping are important in machine learning because models like linear regression
expect input data in a 1D format. Flattening a 2D array (e.g., an image) converts it into a 1D vector,
making it compatible with such models.
Example:
For an image of shape (28, 28), flattening it to a 1D vector of 784 values (28 * 28) is necessary
before feeding it into a linear model.
Students Signature
(For Evaluator’s use only)
Comment of the Evaluator (if Any) Evaluator’s Observation
Marks Secured:_______ out of ________