0% found this document useful (0 votes)
2 views4 pages

NumPy Arrays Practice Answers

This document provides the answer key for a NumPy Arrays practice assignment, including matching questions and fill-in-the-blank answers across various topics such as array creation, indexing, modification, and aggregation. It covers foundational concepts, operations, and common pitfalls associated with NumPy arrays. The answers are organized into sets and parts, detailing specific functions and behaviors of NumPy.

Uploaded by

S H
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

NumPy Arrays Practice Answers

This document provides the answer key for a NumPy Arrays practice assignment, including matching questions and fill-in-the-blank answers across various topics such as array creation, indexing, modification, and aggregation. It covers foundational concepts, operations, and common pitfalls associated with NumPy arrays. The answers are organized into sets and parts, detailing specific functions and behaviors of NumPy.

Uploaded by

S H
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

NumPy Arrays -- Practice Assignment (Answer Key)

NumPy Arrays -- Practice Assignment: Answer Key


Corresponds to: NumPy_Arrays_Practice_Questions.docx

Match the Following -- Answer Keys

Set 1 -- Foundations, Creation & Shape/Dimensionality


1-B, 2-H, 3-E, 4-J, 5-C, 6-L, 7-G, 8-A, 9-F, 10-I, 11-K, 12-D

Set 2 -- Indexing, Modification & Removal


1-G, 2-D, 3-K, 4-I, 5-A, 6-F, 7-B, 8-H, 9-J, 10-L, 11-E, 12-C

Set 3 -- Aggregation, Combining, Transformation, Copying & Pitfalls


1-D, 2-I, 3-B, 4-G, 5-L, 6-E, 7-J, 8-C, 9-H, 10-A, 11-F, 12-K

Fill in the Blanks -- Answers

Part 1 -- List vs. Array


1. arr ______ 2 # multiplies every element, no explicit loop written by
you -> *

2. A list comprehension like [i * 2 for i in L] still runs as a ______-


level loop (unlike a vectorized NumPy op) -> Python

Part 2 -- Getting Started with 1-D / 2-D Arrays


3. OneD.______ tells you the number of elements; for a 1-D array it
matches len(OneD) -> size
4. TwoDim[1]______ chains onto row 1 to reach a specific value within
that row -> [1]
5. OneD[OneD ______ 200] # filters down to only the values greater than
200 -> >
6. OneD.______() # index position of the largest value, not the value
itself -> argmax

Part 3 -- Ways to Create an Array


7. board = [Link]([[0, 1, 0], [1, 1.5, 0]]) # mixing int + float
infers a single ______ for every element -> dtype (e.g. float64)
8. accumulator = np.______((3, 4), fill_value=-1, dtype=np.int16) -> full
9. [Link]() with floats can be off-by-one from float accumulation;
np.______() is the safer choice for an exact count -> linspace
10. [Link](0, 1, 5) guarantees the exact ______ and the exact count
of points -> endpoint(s)

Page 1
NumPy Arrays -- Practice Assignment (Answer Key)

11. [Link](4, k=1) supports an offset diagonal; np.______() only supports


a plain square identity matrix -> identity
12. rng = [Link].default_rng(seed=______) # makes results
reproducible across runs -> 42 (any fixed seed)

Part 4 -- Shape & Dimensionality


13. images = [Link]((2, 4, 4)) # [Link] equals ______ -> 3
14. cube = [Link](2, ______, 4) # lets NumPy infer that axis
size automatically -> -1
15. r = [Link]() shares memory when possible; f = grid.______()
always returns an independent copy -> flatten
16. col = row[:, np.______] # turns a 1-D row into an explicit column
vector -> newaxis
17. [Link](axis=______) # removes only one specific length-1 axis,
leaving others intact -> 0 (or the target axis)
18. grid[1, ::2] and grid[1][::2] usually agree, but ______ indexing is
the idiomatic NumPy way -> comma

Part 5.3 -- Access & Indexing


19. letters[-1] # retrieves the ______ element of the array -> last
20. nums[3:2] # returns an ______ array rather than raising an error ->
empty
21. grid[0, :] returns the full ______ 0, while grid[:, 1] returns the
full column 1 -> row
22. picked = data[[4, 4, 0, 2]] # this is called ______ indexing, and
always returns a copy -> fancy
23. comfortable = temps[(temps > 60) ______ (temps < 80)] # combines
two conditions -- and/or cannot be used here -> &

Part 5.4 -- Modification


24. arr[1:4] = [20, 30, 40] # this is called ______ assignment -> slice
25. grid[1:3, 1:3] = 7 # broadcasts a scalar into an entire ______ of a
2-D array -> block
26. [Link](arr, [4, 5]) # always returns a ______ array; arr itself
is unchanged -> new
27. np.______(arr, 2, 3) # returns a new array with the value 3
inserted at index 2 -> insert
28. [Link](arr, 7) # grows an array by ______ its existing values ->
repeating (padding/repeating)
29. arr += 10 # mutates the existing memory buffer ______, unlike arr =
arr + 10 -> in place

Part 5.5 -- Removal


30. NumPy arrays have no .remove() -- every removal operation returns a
______ array -> new

Page 2
NumPy Arrays -- Practice Assignment (Answer Key)

31. without_col1 = [Link](grid, 1, axis=______) # drops a column


rather than a row -> 1
32. values, counts = [Link](arr, return_______=True) # frequency
table in the same pass -> counts

Part 5.6 -- Querying & Aggregation


33. [Link](lookups, arr) # checks membership for ______ values at once
-> many
34. np.______(dice_rolls) # a full frequency table for non-negative
integer data -> bincount
35. [Link](temps > 70) # returns index ______ where the condition is
True -> positions
36. For a multi-dimensional array, len(grid) measures only ______ 0,
while [Link] measures every element -> axis
37. [Link](axis=0) # collapses the ______ direction, giving a
column-wise result -> row (rows)
38. [Link](axis=1) and [Link](axis=1) work per-______ on a 2-D
boolean array -> axis (row)

Part 5.7 -- Combining & Broadcasting


39. [Link]([top, bottom], axis=0) # extends the number of
______ in a 2-D array -> rows
40. vstack() and hstack() are convenience wrappers around ______() with
the axis already chosen -> concatenate
41. stacked = [Link]([a, b]) # joins two arrays along a brand-new
______ -> axis
42. A * B is elementwise multiplication, while A ______ B performs true
matrix multiplication -> @
43. Broadcasting rule: shape (3, 1) combined with shape (1, 4) broadcasts
to shape ______ -> (3, 4)

Part 5.8 -- Transformation


44. [Link](scores) # returns the ______ order that would sort the
array, not the sorted values -> index
45. grid.T # returns a ______ of the transposed array -- mutating it
mutates the original too -> view
46. [Link](x >= 0, [Link](x), -[Link](-x)) # chains multiple ______
into one vectorized pipeline -> ufuncs
47. squared_vectorized = [Link](values) ** 2 # the loop happens
inside NumPy’s ______ implementation -> C

Part 5.9 -- Copying & Identity


48. v = [Link](); changing v[0] also changes ______, since they share
memory -> arr (the original)
49. c = [Link](); changing c[0] leaves ______ untouched -> arr (the original)

Page 3
NumPy Arrays -- Practice Assignment (Answer Key)

50. middle = arr[1:4] # a classic gotcha -- slicing returns a ______,


not an independent copy -> view
51. [Link] is ______ when an array owns its own memory -> None
52. b is a checks object ______, never elementwise equality -> identity

Part 5.10 -- Common Pitfalls


53. Adding an int array to a float array silently ______ the entire
result to the wider dtype -> upcasts
54. [Link]([127], dtype=np.int8) + 1 # wraps around to ______ instead
of raising an overflow error -> -128
55. A slice like arr[:3] looks read-only but is actually a ______, so
editing it edits the source too -> view
56. Using a plain if on a multi-element NumPy array raises a ______Error -
> Value

Part 5.11 -- Boundary & Contrast


57. [Link](["Alice", 29, True]) # NumPy silently ______ every element
to one common dtype (here, strings) -> upcasts (converts)

Page 4

You might also like