Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 8 MCQsNumPy functions
ECAP776 ·
60 questions. Pick an option to check it.
Which NumPy function calculates the square root of each element in an array?
Not quite. Try another option.
Answer: np.sqrt()
np.sqrt() returns the non-negative square root of each array element.
Question 2 of 60 · Mathematical functions · Easy
What is the result of np.square(np.array([2, 3]))?
Not quite. Try another option.
Answer: [4, 9]
np.square() raises every element to the power .
Question 3 of 60 · Mathematical functions · Easy
Which NumPy function returns the absolute value of every array element?
Not quite. Try another option.
Answer: np.abs()
np.abs() removes negative signs by returning each element's absolute value.
Question 4 of 60 · Mathematical functions · Easy
What does np.exp(x) calculate for each element ?
Not quite. Try another option.
Answer: The value
np.exp(x) calculates the exponential value element by element.
Question 5 of 60 · Statistical functions · Easy
Which NumPy function calculates the arithmetic mean of an array?
Not quite. Try another option.
Answer: np.mean()
np.mean() adds the values and divides the total by the number of values.
Question 6 of 60 · Statistical functions · Easy
What is the value of np.mean([2, 4, 6])?
Not quite. Try another option.
Answer: 4.0
The mean is .
Question 7 of 60 · Statistical functions · Easy
Which NumPy function finds the middle value of sorted numerical data?
Not quite. Try another option.
Answer: np.median()
np.median() returns the middle value after the data is placed in order.
Question 8 of 60 · Statistical functions · Easy
Which NumPy function calculates the standard deviation of array values?
Not quite. Try another option.
Answer: np.std()
np.std() measures how spread out array values are around their mean.
Question 9 of 60 · Sort functions · Easy
Which function returns a sorted copy of a NumPy array?
Not quite. Try another option.
Answer: np.sort()
np.sort() returns array elements arranged in ascending order by default.
Question 10 of 60 · Sort functions · Easy
What is the result of np.sort([3, 1, 2])?
Not quite. Try another option.
Answer: [1, 2, 3]
np.sort() arranges the values in ascending order by default.
Question 11 of 60 · Sort functions · Easy
What does np.argsort() return?
Not quite. Try another option.
Answer: Indices that sort the array
np.argsort() returns the indices that would arrange an array in sorted order.
Question 12 of 60 · Sort functions · Easy
By default, in which order does np.sort() arrange numeric values?
Not quite. Try another option.
Answer: Ascending order
For numeric arrays, np.sort() arranges values from smallest to largest by default.
Question 13 of 60 · Search functions · Easy
Which NumPy function returns indices where a condition is true?
Not quite. Try another option.
Answer: np.where()
np.where(condition) can return the indices of elements satisfying the condition.
Question 14 of 60 · Search functions · Easy
For a = np.array([4, 7, 2]), what does np.argmax(a) return?
Not quite. Try another option.
Answer: 1
The largest value is 7, and its zero-based index is 1.
Question 15 of 60 · Search functions · Easy
Which function returns the index of the smallest array element?
Not quite. Try another option.
Answer: np.argmin()
np.argmin() returns the zero-based index of the smallest element.
Question 16 of 60 · Search functions · Easy
What is np.searchsorted() mainly used for?
Not quite. Try another option.
Answer: Finding an insertion index
np.searchsorted() finds where a value should be inserted to preserve sorted order.
Question 17 of 60 · Counting functions · Easy
Which NumPy function counts the nonzero elements in an array?
Not quite. Try another option.
Answer: np.count_nonzero()
np.count_nonzero() returns the number of elements that are not zero.
Question 18 of 60 · Counting functions · Easy
What is the result of np.count_nonzero([0, 5, 0, 3])?
Not quite. Try another option.
Answer: 2
The two nonzero elements are 5 and 3.
Question 19 of 60 · Counting functions · Easy
Which call returns unique values and how many times each value appears?
Not quite. Try another option.
Answer: np.unique(a, return_counts=True)
np.unique() finds distinct values, while return_counts=True also returns their frequencies.
Question 20 of 60 · Counting functions · Easy
What does np.count_nonzero(a > 5) count?
Not quite. Try another option.
Answer: Elements greater than 5
The condition a > 5 is true only for elements greater than 5, and np.count_nonzero() counts those true values.
Question 21 of 60 · Mathematical functions · Medium
What is the result of np.sqrt(np.abs(np.array([-1, 4, 9])))?
Not quite. Try another option.
Answer: array([1., 2., 3.])
np.abs() first produces [1, 4, 9], and np.sqrt() then calculates the element-wise square roots.
Question 22 of 60 · Mathematical functions · Medium
Given a = np.array([[1], [2]]) and b = np.array([10, 20, 30]), what does np.add(a, b) return?
Not quite. Try another option.
Answer: array([[11, 21, 31], [12, 22, 32]])
Broadcasting expands a across columns and b across rows before performing element-wise addition.
Question 23 of 60 · Mathematical functions · Medium
What is returned by np.floor(np.array([-1.2, 2.8]))?
Not quite. Try another option.
Answer: array([-2., 2.])
np.floor() returns the greatest integer less than or equal to each value, so becomes .
Question 24 of 60 · Mathematical functions · Medium
Given x = np.array([2, 7, 4]) and y = np.array([3, 5, 4]), what is the result of np.maximum(x, y)?
Not quite. Try another option.
Answer: array([3, 7, 4])
np.maximum() compares corresponding elements and selects the larger value at each position.
Question 25 of 60 · Statistical functions · Medium
For a = np.array([[2, 4, 6], [8, 10, 12]]), what does np.mean(a, axis=0) return?
Not quite. Try another option.
Answer: array([5., 7., 9.])
Using axis=0 calculates the mean down each column: , , and .
Question 26 of 60 · Statistical functions · Medium
What is the value of np.median(np.array([1, 7, 3, 9]))?
Not quite. Try another option.
Answer: 5.0
After sorting to [1, 3, 7, 9], the median is the mean of the middle values: .
Question 27 of 60 · Statistical functions · Medium
Approximately what does np.std(np.array([2, 4, 6])) return when no ddof argument is specified?
Not quite. Try another option.
Answer: 1.633
NumPy uses the population standard deviation by default, dividing the squared deviations by , which gives .
Question 28 of 60 · Statistical functions · Medium
What is the result of np.percentile(np.array([0, 10, 20, 30, 40]), 25) using NumPy's standard linear calculation?
Not quite. Try another option.
Answer: 10.0
The 25th percentile lies at index in this five-element sorted array, so its value is 10.0.
Question 29 of 60 · Sort functions · Medium
Given a = np.array([[3, 1], [4, 2]]), what does np.sort(a, axis=1) return?
Not quite. Try another option.
Answer: array([[1, 3], [2, 4]])
With axis=1, NumPy sorts the values independently within each row.
Question 30 of 60 · Sort functions · Medium
What does np.argsort(np.array([30, 10, 20])) return?
Not quite. Try another option.
Answer: array([1, 2, 0])
np.argsort() returns the original indices that would arrange the values in ascending order.
Question 31 of 60 · Sort functions · Medium
For a = np.array([9, 1, 7, 3]), what value is guaranteed at a[np.argpartition(a, 2)[2]]?
Not quite. Try another option.
Answer: 7
np.argpartition(a, 2) places the element with sorted index in that position. The sorted values are [1, 3, 7, 9].
Question 32 of 60 · Sort functions · Medium
What is the shape and content of np.sort(np.array([[4, 1], [3, 2]]), axis=None)?
Not quite. Try another option.
Answer: A one-dimensional array [1, 2, 3, 4]
Setting axis=None flattens the array before sorting all elements in ascending order.
Question 33 of 60 · Sort functions · Medium
Given secondary = np.array([2, 1, 1]) and primary = np.array([1, 1, 0]), what does np.lexsort((secondary, primary)) return?
Not quite. Try another option.
Answer: array([2, 1, 0])
np.lexsort() uses the last key as primary. Index 2 comes first, while indices 1 and 0 are ordered by the secondary key.
Question 34 of 60 · Search functions · Medium
What does np.where(np.array([2, 7, 4, 9]) > 4) return?
Not quite. Try another option.
Answer: A tuple containing array([1, 3])
With only a condition, np.where() returns a tuple of arrays containing indices where the condition is true.
Question 35 of 60 · Search functions · Medium
For a = np.array([[1, 9, 3], [8, 2, 7]]), what does np.argmax(a, axis=1) return?
Not quite. Try another option.
Answer: array([1, 0])
np.argmax(..., axis=1) returns the column index of the maximum value in each row.
Question 36 of 60 · Search functions · Medium
What is returned by np.searchsorted(np.array([10, 20, 30, 40]), 25)?
Not quite. Try another option.
Answer: 2
The value 25 should be inserted at index 2 to preserve ascending order.
Question 37 of 60 · Search functions · Medium
What does np.flatnonzero(np.array([[0, 5], [2, 0]])) return?
Not quite. Try another option.
Answer: array([1, 2])
After flattening to [0, 5, 2, 0], the nonzero elements occur at indices 1 and 2.
Question 38 of 60 · Counting functions · Medium
For a = np.array([[0, 1, 2], [3, 0, 2]]), what does np.count_nonzero(a, axis=0) return?
Not quite. Try another option.
Answer: array([1, 1, 2])
Counting down the columns gives one nonzero value in the first column, one in the second, and two in the third.
Question 39 of 60 · Counting functions · Medium
Given a = np.array([2, 1, 2, 3, 1, 2]), which pair is returned by np.unique(a, return_counts=True)?
Not quite. Try another option.
Answer: (array([1, 2, 3]), array([2, 3, 1]))
np.unique() returns sorted unique values; 1, 2, and 3 occur two, three, and one time respectively.
Question 40 of 60 · Counting functions · Medium
What does np.bincount(np.array([0, 2, 2, 1]), minlength=4) return?
Not quite. Try another option.
Answer: array([1, 1, 2, 0])
Values 0, 1, 2, and 3 occur one, one, two, and zero times; minlength=4 preserves all four bins.
Question 41 of 60 · Mathematical functions · Hard
Given a = np.array([np.nan, -2.0, 3.0]) and b = np.array([2.0, -1.0, np.nan]), what does np.fmax(a, b) return?
Not quite. Try another option.
Answer: array([2., -1., 3.])
np.fmax ignores a NaN when the corresponding value in the other array is non-NaN and otherwise selects the elementwise maximum.
Question 42 of 60 · Mathematical functions · Hard
What are fractional and integral after fractional, integral = np.modf(np.array([-2.75, 3.5]))?
Not quite. Try another option.
Answer: fractional = [-0.75, 0.5], integral = [-2., 3.]
np.modf splits each value using truncation toward zero, so both returned parts preserve the original sign.
Question 43 of 60 · Mathematical functions · Hard
Consider x = np.array([1., 4., 9., 16.]), out = np.full(4, -1.), and np.sqrt(x, out=out, where=np.array([True, False, True, False])). What is out afterward?
Not quite. Try another option.
Answer: array([1., -1., 3., -1.])
The ufunc writes only where the mask is true. Masked-out positions retain their existing values in out.
Question 44 of 60 · Mathematical functions · Hard
Ignoring normal floating-point rounding, what is np.logaddexp(1000.0, 1000.0) designed to compute without overflowing?
Not quite. Try another option.
Answer: Approximately
np.logaddexp(x, y) computes stably. For equal inputs, this is .
Question 45 of 60 · Statistical functions · Hard
Given x = np.array([[1., 2., 9.], [4., 8., 12.]]), what is x - np.mean(x, axis=1, keepdims=True)?
Not quite. Try another option.
Answer: array([[-3., -2., 5.], [-4., 0., 4.]])
The row means are 4 and 8. With keepdims=True, their shape is (2, 1), so subtraction broadcasts across each row.
Question 46 of 60 · Statistical functions · Hard
For x = np.array([1., 2., 3.]), what is np.var(x, ddof=1) - np.var(x, ddof=0)?
Not quite. Try another option.
Answer:
The sum of squared deviations is . Dividing by gives , while dividing by gives ; their difference is .
Question 47 of 60 · Statistical functions · Hard
What does np.average(np.array([[1., 3.], [5., 7.]]), axis=1, weights=np.array([1., 3.]), returned=True) return?
Not quite. Try another option.
Answer: (array([2.5, 6.5]), array([4., 4.]))
Each row uses weighted mean (first + 3*second) / 4. With returned=True, the weight sum is broadcast to the result shape.
Question 48 of 60 · Statistical functions · Hard
Given x = np.array([[np.nan, np.nan], [1.0, np.nan]]), what is np.nanmean(x, axis=1)?
Not quite. Try another option.
Answer: array([nan, 1.]) with an empty-slice warning
np.nanmean ignores NaNs, but an all-NaN slice has no usable observations, so its result is NaN and a runtime warning is issued.
Question 49 of 60 · Sort functions · Hard
Let a = np.array([2, 1, 2, 1]) and b = np.array([1, 2, 0, 1]). What is np.lexsort((a, b))?
Not quite. Try another option.
Answer: array([2, 3, 0, 1])
In np.lexsort, the last key is primary. Indices are ordered first by b, then by a to break ties.
Question 50 of 60 · Sort functions · Hard
After y = np.partition(np.array([9, 1, 8, 2, 7]), 2), which property is guaranteed?
Not quite. Try another option.
Answer: y[2] is 7; earlier values are no larger and later values are no smaller
Index 2 receives the element that would occur there in sorted order. Elements on either side are partitioned around it, but not fully sorted.
Question 51 of 60 · Sort functions · Hard
What is np.argsort(np.array([3, 1, 3, 1]), kind="stable")?
Not quite. Try another option.
Answer: array([1, 3, 0, 2])
A stable sort preserves the original order of equal values: the 1s remain in index order 1, 3, followed by the 3s in index order 0, 2.
Question 52 of 60 · Sort functions · Hard
Given a = np.array([[3, 1], [2, 4], [3, 0]]), what is np.sort(a, axis=0)[::-1]?
Not quite. Try another option.
Answer: array([[3, 4], [3, 1], [2, 0]])
Sorting on axis 0 independently sorts each column to [[2, 0], [3, 1], [3, 4]]; [::-1] then reverses the row order.
Question 53 of 60 · Search functions · Hard
For a = np.array([1, 2, 2, 2, 5]), what is np.searchsorted(a, [2, 3], side="right")?
Not quite. Try another option.
Answer: array([4, 4])
With side="right", 2 is inserted after all existing 2s at index 4. The value 3 also belongs before 5, at index 4.
Question 54 of 60 · Search functions · Hard
Let a = np.array([30, 10, 20]) and sorter = np.argsort(a). What does np.searchsorted(a, 25, sorter=sorter) return?
Not quite. Try another option.
Answer: 2
sorter makes searchsorted search the logical order [10, 20, 30]. The insertion position of 25 in that order is index 2.
Question 55 of 60 · Search functions · Hard
Given a = np.array([[5, 9], [9, 1]]), what is np.unravel_index(np.argmax(a), a.shape)?
Not quite. Try another option.
Answer: (0, 1)
np.argmax returns the first maximum in flattened C order. The first 9 has flat index 1, corresponding to coordinate (0, 1).
Question 56 of 60 · Search functions · Hard
For a = np.array([[0.0, np.nan], [-2.0, 0.0]]), what does np.where(a) return?
Not quite. Try another option.
Answer: (array([0, 1]), array([1, 0]))
The one-argument form finds truthy elements. Both NaN and -2.0 are nonzero and therefore true, at coordinates (0, 1) and (1, 0).
Question 57 of 60 · Counting functions · Hard
What is np.count_nonzero(np.array([0.0, np.nan, -0.0, np.inf]))?
Not quite. Try another option.
Answer: 2
NaN and positive infinity are nonzero. Both positive and negative zero compare equal to zero and are not counted.
Question 58 of 60 · Counting functions · Hard
Given a = np.array([[[1, 0], [0, 2]], [[0, 3], [4, 0]]]), what is np.count_nonzero(a, axis=(0, 2), keepdims=True)?
Not quite. Try another option.
Answer: array([[[2], [2]]])
Axes 0 and 2 are reduced, leaving axis 1. Each of its two positions contains two nonzero values, and keepdims=True preserves reduced axes.
Question 59 of 60 · Counting functions · Hard
What is np.bincount([2, 0, 2, 1], weights=[0.5, 1.5, 2.0, 1.0], minlength=4)?
Not quite. Try another option.
Answer: array([1.5, 1.0, 2.5, 0.0])
np.bincount sums weights by integer value: bin 0 gets 1.5, bin 1 gets 1.0, bin 2 gets 0.5 + 2.0, and bin 3 is padded with zero.
Question 60 of 60 · Counting functions · Hard
Given a = np.array([[1, 2], [1, 2], [2, 1], [1, 2]]), what does np.unique(a, axis=0, return_counts=True) return?
Not quite. Try another option.
Answer: (array([[1, 2], [2, 1]]), array([3, 1]))
With axis=0, complete rows are treated as elements. Row [1, 2] occurs three times, while row [2, 1] occurs once.
That's unit 8
Answer a few questions and your score shows up here.
Nearly through. Did unit 8 of ECAP776 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 8 of ECAP776 save you a night before the exam?
Then you can help keep it free for the next batch. 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.