Unit 7: Operations on NumPy arrays - Subjective Questions
ECAP776 • Practice Questions with Detailed Answers
20 questions
Define array manipulation in NumPy. Explain any four commonly used array-manipulation operations with examples.
Array manipulation refers to changing an array's shape, dimensions, organization, or arrangement without necessarily changing its underlying data.
Common operations include:
- Reshaping:
a.reshape(2, 3)changes a six-element array into two rows and three columns. - Flattening:
a.flatten()returns an independent one-dimensional copy of an array. - Transposing:
a.Tinterchanges the axes of a two-dimensional array, converting rows into columns. - Joining:
np.concatenate((a, b), axis=0)combines compatible arrays along an existing axis. - Splitting:
np.split(a, 2)divides an array into two equal subarrays.
These operations make data suitable for numerical calculations, machine-learning models, and other processing tasks.
Explain the NumPy reshape() operation. What condition must be satisfied when an array is reshaped?
The reshape() method changes the dimensions of an array while preserving its elements and their logical order.
For example, if a = np.arange(12), then a.reshape(3, 4) produces an array with three rows and four columns.
The total number of elements must remain unchanged. Therefore, the new shape must satisfy:
For an array containing 12 elements, shapes such as , , , and are valid, whereas is invalid.
NumPy also permits one dimension to be specified as -1, as in a.reshape(3, -1). NumPy calculates that dimension automatically.
Distinguish between NumPy's flatten() and ravel() methods.
Both methods convert a multidimensional array into a one-dimensional array, but they differ in memory behavior.
flatten(): Always returns a new independent copy of the data. Modifying the flattened result does not modify the original array.ravel(): Returns a flattened view whenever possible. If a view is returned, modifying it may also modify the original array.- Memory usage:
ravel()is generally more memory-efficient because it avoids copying when possible. - Safety:
flatten()is preferable when an independent result is required.
Example:
f = a.flatten() creates a copy, whereas r = a.ravel() usually creates a view. The exact behavior of ravel() can depend on the array's memory layout.
Describe transposition and axis permutation in NumPy. Compare arr.T, np.transpose(), and np.swapaxes().
Transposition and axis permutation rearrange the axes of an array.
arr.T: Reverses the order of all axes. For a matrix of shape , the result has shape .np.transpose(arr, axes): Permutes axes according to a specified order. For example, an array of shape transformed with axes(1, 2, 0)has shape .np.swapaxes(arr, axis1, axis2): Interchanges exactly two selected axes while leaving the other axes in place.
For a two-dimensional array, arr.T and np.transpose(arr) have the same effect. For higher-dimensional arrays, explicitly specifying the axis order provides more control. These operations commonly return views rather than copies.
Explain how arrays are joined using np.concatenate(), np.stack(), np.vstack(), and np.hstack().
NumPy provides several ways to join arrays:
np.concatenate(): Joins arrays along an existing axis. The dimensions other than the joining axis must be compatible.np.stack(): Joins arrays along a newly created axis. Consequently, the result has one more dimension than each input array.np.vstack(): Stacks arrays vertically. For two-dimensional arrays, this is equivalent to concatenation along axis0.np.hstack(): Stacks arrays horizontally. For two-dimensional arrays, this is generally equivalent to concatenation along axis1.
For two arrays of shape :
- Concatenating on axis
0gives shape . - Concatenating on axis
1gives shape . - Stacking on a new axis gives shape when the new axis is
0.
Thus, the key distinction is whether data is joined along an existing axis or a new axis.
Describe array splitting in NumPy using np.split(), np.array_split(), np.hsplit(), and np.vsplit().
Splitting divides one array into multiple subarrays.
np.split(arr, n, axis): Divides an array intonequal parts along the specified axis. It raises an error if equal division is impossible.np.array_split(arr, n, axis): Permits unequal divisions when the axis length is not exactly divisible byn.np.hsplit(): Splits an array horizontally, normally along axis1for a two-dimensional array.np.vsplit(): Splits an array vertically, along axis0.
For example, np.split(np.arange(8), 4) returns four subarrays containing two elements each. In contrast, np.array_split(np.arange(8), 3) succeeds by producing subarrays whose sizes differ by at most one.
What is broadcasting in NumPy? State and explain its compatibility rules.
Broadcasting is NumPy's mechanism for performing element-wise operations on arrays with different but compatible shapes without explicitly copying the smaller array.
NumPy compares shapes from their trailing, or rightmost, dimensions. Two dimensions are compatible when:
- They are equal, or
- One of them is
1.
A missing leading dimension is treated as if it were 1. The output size along each axis is the larger compatible size.
For example, shapes and are compatible because the trailing dimensions are both 3. The second array is conceptually applied to every row. However, shapes and are incompatible because their trailing dimensions 3 and 4 are neither equal nor 1.
Broadcasting enables concise and efficient vectorized calculations.
Derive the broadcasted output shape for arrays with shapes and . Explain the comparison axis by axis.
Shapes are aligned from the right:
- First array:
- Second array after adding a missing leading dimension:
The dimensions are compared as follows:
- Rightmost axis:
1and5are compatible, producing5. - Next axis:
6and1are compatible, producing6. - Next axis:
1and7are compatible, producing7. - Leftmost axis:
8and1are compatible, producing8.
Therefore, the broadcasted output shape is:
No dimension conflicts occur because every pair of corresponding dimensions is either equal or contains a 1.
Explain row-wise and column-wise broadcasting for a matrix of shape . Give one compatible vector shape for each case.
Consider a matrix A with shape .
- Row-wise application: A vector with shape aligns with the matrix's last dimension. NumPy applies the vector to each of the three rows. The result has shape .
- Column-wise application: A vector containing three values must be reshaped to $(3,1)
. Its size3matches the row axis, while its size1` expands across the four columns. The result again has shape $(3,4)$.
A vector with shape cannot directly be broadcast with a matrix because the trailing dimensions 3 and 4 conflict. It can be converted using v[:, np.newaxis] or v.reshape(3, 1).
Describe how np.newaxis and np.expand_dims() help control broadcasting.
np.newaxis and np.expand_dims() insert an axis of length 1 into an array. Such singleton dimensions can expand during broadcasting.
Suppose v has shape $(3,)`:
v[:, np.newaxis]changes its shape to .v[np.newaxis, :]changes its shape to .np.expand_dims(v, axis=1)also produces shape .
These operations do not add new data values; they only change how dimensions are interpreted. For example, converting a three-element vector to shape allows it to be broadcast column-wise against a matrix of shape .
Compare implicit broadcasting with np.broadcast_to() and np.tile().
The three techniques can make values behave as though they were repeated, but they differ substantially:
- Implicit broadcasting: Occurs automatically during a compatible operation. It generally avoids materializing repeated data.
np.broadcast_to(): Produces a broadcasted, typically read-only view with a requested compatible shape. It does not normally allocate storage for all apparent repetitions.np.tile(): Constructs an array by physically repeating data according to specified repetition counts, which may require much more memory.
Broadcasting and broadcast_to() are preferable when repeated values are only needed for computation. tile() is appropriate when an actual repeated array is specifically required. A broadcasted view should not be treated as an independently writable array.
Explain the advantages and possible limitations of broadcasting in numerical programs.
Advantages of broadcasting include:
- Concise code: It removes many explicit Python loops.
- High performance: Operations execute through NumPy's optimized compiled routines.
- Memory efficiency: The smaller operand is usually not physically copied.
- Clear vectorized expressions: Mathematical relationships can be written directly.
Possible limitations include:
- Shape errors: Incompatible trailing dimensions raise a broadcasting error.
- Logical mistakes: A technically valid broadcast may occur along an unintended axis.
- Large outputs: Although operands are not repeated physically, the resulting array may still be very large.
- Reduced readability: Complicated multidimensional broadcasting can be difficult to understand without documenting shapes.
Programmers should inspect array shapes and introduce singleton dimensions explicitly when the intended alignment is not obvious.
Explain NumPy's arithmetic binary operators and describe how they behave when applied to arrays.
Arithmetic binary operators combine two operands and generally act element by element on NumPy arrays.
Important operators include:
+for addition-for subtraction*for multiplication/for true division//for floor division%for remainder**for exponentiation
If two arrays have the same shape, corresponding elements are combined. If their shapes differ, NumPy first attempts broadcasting. For arrays a and b, a + b is equivalent to np.add(a, b), and a ** b is equivalent to np.power(a, b).
The output data type is determined through NumPy's type-promotion rules. Division or operations involving floating-point operands may therefore produce floating-point results.
Distinguish between element-wise multiplication, matrix multiplication, and the dot product in NumPy.
These operations have different meanings:
- Element-wise multiplication:
A * Bmultiplies corresponding elements after broadcasting. For equal shapes , the output also has shape . - Matrix multiplication:
A @ Bornp.matmul(A, B)contracts the last axis ofAwith the second-last axis ofB. If has shape and has shape , the result has shape . - Dot product:
np.dot(a, b)gives the scalar inner product for one-dimensional vectors. For two-dimensional arrays, it performs matrix multiplication, but its higher-dimensional behavior differs frommatmul().
For vectors and of length , the dot product is:
Thus, * must not be used when matrix multiplication is intended.
Describe comparison operators and boolean masks in NumPy. How can they be used to filter or modify an array?
Comparison operators such as >, <, >=, <=, ==, and != operate element-wise and return a boolean array.
For example, if a = np.array([2, 7, 4, 9]), then a > 5 produces a mask equivalent to [False, True, False, True].
The mask can be used in several ways:
- Filtering:
a[a > 5]returns the elements7and9. - Modification:
a[a > 5] = 0replaces matching elements with zero. - Conditional selection:
np.where(a > 5, a, -1)keeps matching values and substitutes-1elsewhere.
The mask must either match the indexed dimensions or be valid according to the particular indexing operation. Boolean masks provide a vectorized alternative to conditional loops.
Why should and, or, and not generally not be used to combine NumPy boolean arrays? Explain the correct alternatives and operator-precedence requirement.
Python's and, or, and not expect each operand to have one truth value. A NumPy array normally contains many truth values, so using these keywords can raise an error stating that the truth value of an array is ambiguous.
The element-wise alternatives are:
&ornp.logical_and()for logical AND|ornp.logical_or()for logical OR~ornp.logical_not()for logical NOT^ornp.logical_xor()for exclusive OR
Each comparison should be enclosed in parentheses because bitwise operators have different precedence from comparison operators. For example:
(a > 0) & (a < 10)
To test the entire boolean array, mask.all() checks whether every element is true, while mask.any() checks whether at least one element is true.
Explain bitwise binary operators in NumPy and distinguish them from logical operations.
Bitwise operators act on the individual bits of integer or boolean values:
&performs bitwise AND.|performs bitwise OR.^performs bitwise XOR.~performs bitwise inversion.<<shifts bits to the left.>>shifts bits to the right.
For integer arrays, these operators manipulate binary representations. For example, decimal 6 is binary 110 and decimal 3 is binary 011; therefore, because 110 & 011 gives 010.
For boolean arrays, &, |, ^, and ~ behave like element-wise logical operations. Functions such as np.logical_and() first interpret operands by their truth values, whereas bitwise functions such as np.bitwise_and() operate on the actual bits of integer operands.
What are in-place binary operations in NumPy? Discuss their benefits and the data-type issue that may occur.
An in-place operation stores the result back into an existing array. Examples include a += b, a *= 2, and a **= 2.
Benefits include:
- Reduced need for an additional result array
- Potentially lower memory consumption
- Convenient updating of an existing array
However, the result generally must be cast back to the data type of the left-hand array under NumPy's casting rules. For example, applying a += 0.5 to an integer array may raise a casting error because the floating-point result cannot be safely stored as integers.
An out-of-place expression such as a = a + 0.5 can create a new floating-point array instead. In-place modification can also affect other variables if they are views of the same underlying data.
Explain NumPy universal functions and show how binary operators relate to binary ufuncs. Discuss the out and where parameters.
A universal function, or ufunc, performs fast element-wise operations while supporting broadcasting, type handling, and optional output control.
Binary operators commonly correspond to ufuncs:
a + bcorresponds tonp.add(a, b).a - bcorresponds tonp.subtract(a, b).a * bcorresponds tonp.multiply(a, b).a / bcorresponds tonp.divide(a, b).
The out parameter stores results in a supplied compatible array, which can reduce temporary allocation. The where parameter applies the operation only where a boolean condition is true.
For example, np.divide(a, b, out=result, where=b != 0) performs division only for nonzero divisors. Positions where the condition is false retain the existing values in result, so that output should be initialized appropriately.
Given an array of shape , determine whether operands with shapes , , , and can be broadcast with it. Justify each answer.
Each shape is aligned with from the right.
- Shape : Treated as . Every axis is compatible, so the output shape is .
- Shape : Treated as . The dimensions are compatible with , so the output shape is .
- Shape : Compared directly with . The middle dimensions
1and3are compatible, so the output shape is . - Shape : Treated as . The middle dimension
2conflicts with3; neither is1. Therefore, broadcasting fails.
This example demonstrates why dimensions must be compared from right to left rather than by matching the first dimensions.
Define array manipulation in NumPy. Explain any four commonly used array-manipulation operations with examples.
Array manipulation refers to changing an array's shape, dimensions, organization, or arrangement without necessarily changing its underlying data.
Common operations include:
- Reshaping:
a.reshape(2, 3)changes a six-element array into two rows and three columns. - Flattening:
a.flatten()returns an independent one-dimensional copy of an array. - Transposing:
a.Tinterchanges the axes of a two-dimensional array, converting rows into columns. - Joining:
np.concatenate((a, b), axis=0)combines compatible arrays along an existing axis. - Splitting:
np.split(a, 2)divides an array into two equal subarrays.
These operations make data suitable for numerical calculations, machine-learning models, and other processing tasks.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →