1/28
Looks like no tags are added yet.
What does x > 0 produce when x is an array?
A Boolean array of True/False, one result per entry.
How do you select only the positive values of x?
x[x > 0]
How do you select the negative values of x?
x[x < 0]
How do you exclude all zeros from x?
x[x != 0]
For x=[-2,0,3], what is x[x>0]?
[3]
How do you select values between 0 and 3 in array x?
x[(x >= 0) & (x <= 3)]
What joins two NumPy array masks: and or &?
Use & for elementwise AND; use parentheses around comparisons.
In a 2D array A, what does A[1,2] mean?
Row index 1, column index 2 (second row, third column).
What does A[:,0] select for a 2D array?
Every row of the first column.
What does A[:2,:] select for a 2D array?
The first two rows and all columns.
What is the shape of a 4-row, 3-column array?
(4, 3)
For x=np.array([-2,0,3]), what mask is x>0?
[False False True]
For x=np.array([-2,0,3]), what is x[x!=0]?
[-2 3]
For x=np.array([-2,0,3]), what is np.abs(x[x!=0])?
[2 3]
What does (x>0).sum() count in a NumPy array?
How many entries are positive, since True counts as 1.
If x=[-1,0,2,4] as NumPy array, how many positives are there?
2; (x>0).sum() returns 2.
How do you select NumPy array entries with absolute value > 2?
x[np.abs(x) > 2]
What does x[(x<0)|(x>5)] keep?
Values less than 0 OR greater than 5.
Why use parentheses in (x>0)&(x<3)?
They ensure each comparison happens before combining masks.
For A=[[1,2,3],[4,5,6]] as NumPy array, what is A[1,0]?
4
For A=[[1,2,3],[4,5,6]] as NumPy array, what is A[0,2]?
3
For A=[[1,2,3],[4,5,6]] as NumPy array, what is A[0,:]?
[1 2 3] (entire first row).
For A=[[1,2,3],[4,5,6]] as NumPy array, what is A[:,1]?
[2 5] (second column).
For A=[[1,2,3],[4,5,6]] as NumPy array, what is A[:,:2]?
[[1 2], [4 5]] (first two columns).
Write code to copy the third row of array A
row = A[2,:].copy()
Write code to select the last column of a 2D array A
A[:,-1]
What is np.array([[1,2],[3,4]]).shape?
(2,2)
Why is 2D array indexing written A[row,column]?
The first index chooses a row, then the second a column.
What does x[x>0] = 0 do?
Sets all positive entries in x to zero.