Boolean Indexing and 2D Arrays - 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/28

encourage image

There's no tags or description

Looks like no tags are added yet.

29 Terms

1
New cards

What does x > 0 produce when x is an array?

A Boolean array of True/False, one result per entry.

2
New cards

How do you select only the positive values of x?

x[x > 0]

3
New cards

How do you select the negative values of x?

x[x < 0]

4
New cards

How do you exclude all zeros from x?

x[x != 0]

5
New cards

For x=[-2,0,3], what is x[x>0]?

[3]

6
New cards

How do you select values between 0 and 3 in array x?

x[(x >= 0) & (x <= 3)]

7
New cards

What joins two NumPy array masks: and or &?

Use & for elementwise AND; use parentheses around comparisons.

8
New cards

In a 2D array A, what does A[1,2] mean?

Row index 1, column index 2 (second row, third column).

9
New cards

What does A[:,0] select for a 2D array?

Every row of the first column.

10
New cards

What does A[:2,:] select for a 2D array?

The first two rows and all columns.

11
New cards

What is the shape of a 4-row, 3-column array?

(4, 3)

12
New cards

For x=np.array([-2,0,3]), what mask is x>0?

[False False True]

13
New cards

For x=np.array([-2,0,3]), what is x[x!=0]?

[-2 3]

14
New cards

For x=np.array([-2,0,3]), what is np.abs(x[x!=0])?

[2 3]

15
New cards

What does (x>0).sum() count in a NumPy array?

How many entries are positive, since True counts as 1.

16
New cards

If x=[-1,0,2,4] as NumPy array, how many positives are there?

2; (x>0).sum() returns 2.

17
New cards

How do you select NumPy array entries with absolute value > 2?

x[np.abs(x) > 2]

18
New cards

What does x[(x<0)|(x>5)] keep?

Values less than 0 OR greater than 5.

19
New cards

Why use parentheses in (x>0)&(x<3)?

They ensure each comparison happens before combining masks.

20
New cards

For A=[[1,2,3],[4,5,6]] as NumPy array, what is A[1,0]?

4

21
New cards

For A=[[1,2,3],[4,5,6]] as NumPy array, what is A[0,2]?

3

22
New cards

For A=[[1,2,3],[4,5,6]] as NumPy array, what is A[0,:]?

[1 2 3] (entire first row).

23
New cards

For A=[[1,2,3],[4,5,6]] as NumPy array, what is A[:,1]?

[2 5] (second column).

24
New cards

For A=[[1,2,3],[4,5,6]] as NumPy array, what is A[:,:2]?

[[1 2], [4 5]] (first two columns).

25
New cards

Write code to copy the third row of array A

row = A[2,:].copy()

26
New cards

Write code to select the last column of a 2D array A

A[:,-1]

27
New cards

What is np.array([[1,2],[3,4]]).shape?

(2,2)

28
New cards

Why is 2D array indexing written A[row,column]?

The first index chooses a row, then the second a column.

29
New cards

What does x[x>0] = 0 do?

Sets all positive entries in x to zero.