NumPy Basics: Arrays and Vectorized Computation

50 questions available

Summary unavailable.

Questions

Question 1

What is the primary function of the NumPy ndarray object as described in the chapter?

View answer and explanation
Question 2

What does the `dtype` attribute of a NumPy ndarray represent?

View answer and explanation
Question 3

Consider the following code: `import numpy as np; arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])`. What will be the output of `arr.ndim`?

View answer and explanation
Question 4

How does slicing a NumPy array differ from slicing a standard Python list regarding data copying?

View answer and explanation
Question 5

Given the code: `import numpy as np; arr = np.arange(10); arr_slice = arr[5:8]; arr_slice[1] = 12345`. What is the value of `arr[6]`?

View answer and explanation
Question 6

Which operator should be used to perform a logical AND operation when combining multiple Boolean conditions on NumPy arrays?

View answer and explanation
Question 7

What is the purpose of the `seed` argument when creating a random number generator with `np.random.default_rng(seed=12345)`?

View answer and explanation
Question 8

What is a universal function (or ufunc) in NumPy?

View answer and explanation
Question 9

What does the `numpy.modf` ufunc return when applied to a floating-point array?

View answer and explanation
Question 10

What is the primary function of `numpy.where`?

View answer and explanation
Question 11

When using a statistical method like `arr.mean(axis=1)` on a 2D NumPy array, what is being computed?

View answer and explanation
Question 12

What is the key difference between the `arr.sort()` method and the `numpy.sort(arr)` function?

View answer and explanation
Question 13

Which pair of NumPy functions is used for saving and loading a single array in NumPy's uncompressed raw binary format?

View answer and explanation
Question 14

For two 2D NumPy arrays, `mat1` and `mat2`, which expression correctly performs matrix multiplication?

View answer and explanation
Question 15

In the context of the random walk simulation, what does calling `.argmax()` on a Boolean array like `(np.abs(walk) >= 10).argmax()` compute?

View answer and explanation
Question 16

What is the result of casting a float array to an integer dtype using `astype(np.int32)`?

View answer and explanation
Question 17

What is the primary characteristic of 'fancy indexing' in NumPy?

View answer and explanation
Question 18

If `arr` is a NumPy array, what is the effect of the `.T` attribute?

View answer and explanation
Question 19

When comparing the performance of NumPy array operations to pure Python operations on lists for large sequences, what is the typical observation?

View answer and explanation
Question 20

What is the output of the following code? `import numpy as np; arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1]); print(arr.astype(np.int32))`

View answer and explanation
Question 21

In a 2D NumPy array `arr2d`, what does the indexing expression `arr2d[0, 2]` select?

View answer and explanation
Question 22

What is the result of selecting data from an array using Boolean indexing, such as `data[names == 'Bob']`, in terms of data copying?

View answer and explanation
Question 23

What is the function of `numpy.unique` on a 1D ndarray?

View answer and explanation
Question 24

What happens when you pass multiple index arrays to a NumPy array, like `arr[[1, 5, 7, 2], [0, 3, 1, 2]]`?

View answer and explanation
Question 25

Which `numpy.linalg` function is used to compute the inverse of a square matrix?

View answer and explanation
Question 26

What does the `any()` method check for in a Boolean array?

View answer and explanation
Question 27

What is 'vectorization' as the term is used in the context of NumPy arrays?

View answer and explanation
Question 28

Given `arr = np.arange(32).reshape((8, 4))`, what is the result of `arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]]`?

View answer and explanation
Question 29

What does the `swapaxes` method, for example `arr.swapaxes(0, 1)`, do on an ndarray?

View answer and explanation
Question 30

How can you create a 3x6 NumPy array filled with zeros?

View answer and explanation
Question 31

What is the primary risk of using the `numpy.empty` function to create an array?

View answer and explanation
Question 32

What does the NumPy function `np.arange(15)` produce?

View answer and explanation
Question 33

What is the result of the expression `arr.cumsum(axis=1)` on a 2D array `arr = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])`?

View answer and explanation
Question 34

If `numeric_strings = np.array(['1.25', '-9.6', '42'], dtype=np.string_)`, which method converts this array to a numeric floating-point form?

View answer and explanation
Question 35

According to the chapter, why is it advisable to use `import numpy as np` instead of `from numpy import *`?

View answer and explanation
Question 36

In a comparison between arrays of the same size, such as `arr2 > arr`, what is the resulting object?

View answer and explanation
Question 37

When simulating many random walks at once, if `nwalks = 5000` and `nsteps = 1000`, what is the shape of the `walks` array created by `steps.cumsum(axis=1)`?

View answer and explanation
Question 38

What is the purpose of the `out` argument in some ufuncs, such as `np.add(arr, 1, out=out_array)`?

View answer and explanation
Question 39

What would `np.in1d(values, [2, 3, 6])` return if `values` is `np.array([6, 0, 0, 3, 2, 5, 6])`?

View answer and explanation
Question 40

When saving multiple arrays to a single file archive, which function should be used?

View answer and explanation
Question 41

If `arr = np.array([[1., 2., 3.], [4., 5., 6.]])`, what is the result of `1 / arr`?

View answer and explanation
Question 42

In the `numpy.random` module, how does `shuffle` differ from `permutation`?

View answer and explanation
Question 43

What is the function `np.meshgrid` typically used for, as shown in the example in Section 4.4?

View answer and explanation
Question 44

If `arr` is a NumPy array, what is the key difference between using fancy indexing (`arr[[4, 3, 0]]`) and slicing (`arr[0:4]`) regarding data copying?

View answer and explanation
Question 45

What does the `logical_not` ufunc compute?

View answer and explanation
Question 46

Consider the code `arr = np.array([False, False, True, False])`. What will `arr.all()` and `arr.any()` return, respectively?

View answer and explanation
Question 47

When sorting a multidimensional array in-place along an axis, for example `arr.sort(axis=0)`, what is the behavior?

View answer and explanation
Question 48

What is the key advantage of NumPy's array-oriented computing approach, exemplified by its C-based algorithms?

View answer and explanation
Question 49

If `arr` is a 5x4 array, what is the shape of the result of `arr.mean(axis=0)`?

View answer and explanation
Question 50

What is the purpose of the `svd` function in the `numpy.linalg` module?

View answer and explanation