L.1 (1) Which of the following statement is False?
We will generally manipulate arrays using concise functional-style programming techniques with internal iteration rather than using an explicit for loop.
Most array operations execute significantly faster than corresponding list operations.
Method reshape will return a deep copy of the original array with the new specified dimensions, and it does not modify the original array.
Transposing an array using .T attribute does not modify the original array.
Ans: Double click to answer the question
It does not modify the original array. Actually, Method reshape returns a view (shallow copy) of the original array with the new dimensions.
L.2 (2) Which of the following is the most convenient method to produce evenly spaced numbers over a specified interval by specifying the number of numbers desired?
arange
linspace
full
range
Ans: Double click to answer the question
L.3 (3) Which of the following statement is False?
Usually, the Numpyarrays are statically typed. The type of the elements is determined when the array is created
A 24-element one-dimensional array can be reshaped into a 2-by-12, 8-by-3 or 4-by-6 array, and vice versa.
Just like the built-in list, Numpyarrays can be used to store heterogeneous data with different data types.
Just like the built-in list, Numpyarrays can be sliced and indexed.
Ans: Double click to answer the question
This statement is false because Numpyarrays are statically typed, which means that all elements in the array must have the same data type. In contrast, list objects can store elements of different data types.
L.4 (4) Which of the following operation is invalid?
a = np.zeros((5, 1)); b = np.arange(3); a + b
a = 3; b = np.full((2, 4), 3); a + b
a = np.array([1, 2, 3]).reshape(3, 1); b = np.array([4, 5]); a * b
a = np.ones((4, 2)); b = np.arange(4); a + b
Ans: Double click to answer the question
L.5 (5) Which of the following function is not possible to produce an array that contains the numerical value 10 as its element?
np.linspace(1, 11, 5)
np.random.randint(11)
np.arange(11)
np.array(range(11))
Ans: Double click to answer the question
L.6 (6) Suppose we are developing a chess game and the chess game provides a special checkerboards as follows:
We decide to use 1 to represent the white square and 0 to represent the black square. Write a program to create two 2D arrays to represent the two checkerboards as follows:
Note you should not directly hardcode the above arrays. You should use Numpy methods to create the arrays. After you have finished the exercise, you can print out the checkerboard using the following code cell.
import matplotlib.pyplot as pltplt.imshow(checkerboard, cmap='gray');
L.7 (7) Write a program that computes the value of \(\pi\) from the following infinite series \(4 \times (1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}...-\frac{(-1)^n}{2n-1})\) without using the loop. What is the error when you use 500 terms to compute the value of \(\pi\)?
Hint: You can use Numpy methods to create the array and compute the sum. Use the constant np.pi as the ground truth value for \(\pi\) and use np.abs() to calculate the error.
n =500k = np.arange(1, n+1)error = np.abs(np.pi -4*np.sum(-(-1)**(k) / (2*k -1)))print(f"The error is {error} when using {n} terms in the series.")
The error is 0.0019999980000111606 when using 500 terms in the series.
The first column is the original grade, while the second column is all-ones. Find a 1D vector v such that S @ v outputs the adjusted score according to the following formula:
adjusted score = 40 + 0.6 * (original score)
S = np.array([[43., 1.], [75., 1.], [80., 1.], [56., 1.], [61., 1.]])v = [0.6, 40]S @ v
array([65.8, 85. , 88. , 73.6, 76.6])
The 1D vector is [0.6, 40].
L.9 (9) Compare the performance between for loop and Numpy vectorization for calculating the summation of all odd numbers smaller than 10000, i.e., \(1 + 3 + 5 \ldots + 9997 + 9999\). What is the speedup factor of the Numpy vectorization?
Hint: Use %%timeit to measure the performance of the code.
%%timeiti =0for item inrange(1, 10000, 2): i = i + item
175 µs ± 2.97 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%%timeitnp.sum(np.arange(1, 10000, 2))
7.87 µs ± 99.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Speed up is 175/7.87 = 22.2
L.10 (10) Find the maximum and minimum values of the function \(f(x) = x^2\) on the interval \([-3, 5]\) by substituting 1000 evenly spaced numbers between \(-3\) and \(5\) into the function. What is the corresponding \(x\) value for the maximum and minimum values and how do they compare with the actual values?
Hint: You may find np.linspace, np.max/np.min and np.argmax/np.argmin useful.
N =1000x = np.linspace(-3, 5, 1000)y = x**2y_max = np.max(y)y_min = np.min(y)x_max = x[np.argmax(y)]x_min = x[np.argmin(y)]print('Maximum value is y =', y_max,'at x =', x_max)print('Minimum value is y =', y_min,'at x =', x_min)
Maximum value is y = 25.0 at x = 5.0
Minimum value is y = 9.01802703604398e-06 at x = 0.0030030030030028243
They are quite close to the actual values. Where actual minimum is at \(x=0\) and maximum is at \(x=5\).