Numpy3 Flashcards

1
Q

a = np.array([0, 1, 2])
b = np.array([5, 5, 5])
a + b

A

array([5, 6, 7])

for arrays of the same size, binary operations are performed on an element-by-element basis

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Rule 1: If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded with ones on its leading (left) side.
Rule 2: If the shape of the two arrays does not match in any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
Rule 3: If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

A

Rules of Broadcasting

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

np.newaxis

A

is used to increase the dimension of the existing array by one more dimension, when used once. Thus,

1D array will become 2D array

2D array will become 3D array

3D array will become 4D array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

x < 3

A

array([ True, True, False, False, False], dtype=bool)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

(2 * x) == (x ** 2)

A

element-wise comparison of two arrays, and to include compound expressions
array([False, True, False, False, False], dtype=bool)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

== np.equal != np.not_equal
< np.less <= np.less_equal
> np.greater >= np.greater_equal

A

comparison operators are implemented as ufuncs in NumPy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
# how many values less than 6?
#np.count_nonzero(x < 6)
np.sum(inches >.1)
A

To count the number of True entries in a Boolean array, np.count_nonzero is useful:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

np.sum(x < 6, axis=1)

A

how many values less than 6 in each row?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
# are there any values greater than 8?
np.any(x > 8)
A

quickly checking whether any or all the values are true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

np.all(x < 10)

# are all values equal to 6?
np.all(x == 6)
A

quickly checking whether any or all the values are true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

np.sum((inches > 0.5) & (inches < 1))

A

bitwise logic operators, &, |, ^, and ~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

& np.bitwise_and | np.bitwise_or

^ np.bitwise_xor ~ np.bitwise_not

A

bitwise Boolean operators and their equivalent ufuncs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

“Number days without rain: “, np.sum(inches == 0))
“Number days with rain: “, np.sum(inches != 0))
“Days with more than 0.5 inches:”, np.sum(inches > 0.5))
“Rainy days with < 0.2 inches :”, np.sum((inches > 0) &
(inches < 0.2)))

A

bitwise Examples

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

x[x < 5]

A

to select these values from the array, we can simply index on this Boolean array; this is known as a masking operation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

np.median(inches[rainy & ~summer]

A

Example operation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly