NumPy Arrays and Arithmetic - Exam 1

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/32

encourage image

There's no tags or description

Looks like no tags are added yet.

33 Terms

1
New cards

How do you create a NumPy array of [2,4,6]?

np.array([2, 4, 6])

2
New cards

How do you make 5 zeros?

np.zeros(5)

3
New cards

How do you make 4 ones?

np.ones(4)

4
New cards

What does x.size tell you?

The total number of elements in the array.

5
New cards

What does x.shape tell you?

The dimensions of the array, e.g. (4,) or (3,2)

6
New cards

What does x.ndim tell you?

The number of array dimensions

7
New cards

What does np.abs(x) do to a NumPy array?

Takes absolute value of each element

8
New cards

How do you discard zeros and take absolute values?

np.abs(x[x != 0])

9
New cards

What does x*3 do for a NumPy array?

Multiplies every element by 3

10
New cards

What does x**2 do for a NumPy array?

Squares every element separately

11
New cards

How does x+y work for same-shaped NumPy arrays?

Adds corresponding elements (elementwise)

12
New cards

What is the safest pattern for an independent array copy?

y = x.copy()

13
New cards

Why can y=x be risky for a NumPy array?

Both names refer to the same array; editing it through either name affects both

14
New cards

What does a NumPy array let one variable store?

Multiple values, often numbers, in a single array object

15
New cards

Make the NumPy array [1, -2, 3]

x = np.array([1, -2, 3])

16
New cards

What does np.zeros(3) produce?

[0. 0. 0.] (three zero values)

17
New cards

What does np.ones(2)*5 produce?

[5. 5.]

18
New cards

If x=np.array([2,4,6]), what is x+3?

[5 7 9]; 3 is added to every element

19
New cards

If x=np.array([2,4,6]), what is 2*x?

[4 8 12]

20
New cards

If x=np.array([2,4,6]), what is x**2?

[4 16 36]

21
New cards

For x=[1,2,3], y=[4,5,6] as NumPy arrays, what is x+y?

[5 7 9]

22
New cards

For NumPy x=[1,2], y=[3,4], what is x*y?

[3 8]; ordinary * multiplies corresponding entries

23
New cards

Does * between same-shaped NumPy arrays normally mean matrix
multiplication?

No. It is elementwise multiplication.

24
New cards

If x has 5 elements, what is x.shape?

(5,), a one-entry tuple describing the 1-D shape

25
New cards

If A has 3 rows and 4 columns, what is A.size?

12 (total entries)

26
New cards

If A is a 3x4 array, what is A.ndim?

2

27
New cards

How do you check the data type of variable x?

type(x)

28
New cards

What does x=np.array([-3,0,4]); np.abs(x) return?

[3 0 4]

29
New cards

What does a=4; b=a; a=9; print(b) output?

4; ordinary numeric assignment does not link values like arrays

30
New cards

What changes when you use b=a.copy() instead of b=a for arrays?

b becomes an independent array; edits to a do not modify b

31
New cards

What happens when adding NumPy arrays of incompatible shapes?

Usually a ValueError unless NumPy broadcasting rules allow them

32
New cards

What NumPy command creates 6 zeros ready to fill in a loop?

values = np.zeros(6)

33
New cards

For x=np.array([1,2,3]), what is x/2?

[0.5 1. 1.5]