Numpy2 Flashcards

1
Q

np.add(x, 2)

A

arithmetic operations are simply convenient wrappers around specific functions built into NumPy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
\+	np.add	Addition (e.g., 1 + 1 = 2)
-	np.subtract	Subtraction (e.g., 3 - 2 = 1)
-	np.negative	Unary negation (e.g., -2)
*	np.multiply	Multiplication (e.g., 2 * 3 = 6)
/	np.divide	Division (e.g., 3 / 2 = 1.5)
//	np.floor_divide	Floor division (e.g., 3 // 2 = 1)
**	np.power	Exponentiation (e.g., 2 ** 3 = 8)
%	np.mod	Modulus/remainder (e.g., 9 % 4 = 1)
A

Simple Numpy Operators

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

x = np.arange(5)
y = np.empty(5)
np.multiply(x, 10, out=y)

A

specify the array where the result of the calculation will be stored. Rather than creating a temporary array

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

y = np.zeros(10)

np.power(2, x, out=y[::2])

A

used with array views. For example, we can write the results of a computation to every other element of a specified array
[ 1. 0. 2. 0. 4. 0. 8. 0. 16. 0.]

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

x = np.arange(1, 6)

np.multiply.reduce(x)

A

if we’d like to reduce an array with a particular operation, we can use the reduce method of any ufunc. A reduce repeatedly applies a given operation to the elements of an array until only a single result remains.

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

np. add.accumulate(x)

np. multiply.accumulate(x)

A

store all the intermediate results of the computation, we can instead use accumulate

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

x = np.arange(1, 6)

np.multiply.outer(x, x)

A

compute the output of all pairs of two different inputs using the outer method

array([[ 1,  2,  3,  4,  5],
       [ 2,  4,  6,  8, 10],
       [ 3,  6,  9, 12, 15],
       [ 4,  8, 12, 16, 20],
       [ 5, 10, 15, 20, 25]])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

np.min(big_array), np.max(big_array)

A

find the minimum value and maximum value of any given array

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

M.sum()

A

each NumPy aggregation function will return the aggregate over the entire array

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

M.min(axis=0)

A

Aggregation functions take an additional argument specifying the axis along which the aggregate is computed. For example, we can find the minimum value within each column by specifying axis=0

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

np. sum np.nansum Compute sum of elements
np. prod np.nanprod Compute product of elements
np. mean np.nanmean Compute mean of elements
np. std np.nanstd Compute standard deviation
np. var np.nanvar Compute variance
np. min np.nanmin Find minimum value
np. max np.nanmax Find maximum value
np. argmin np.nanargmin Find index of minimum value
np. argmax np.nanargmax Find index of maximum value
np. median np.nanmedian Compute median of elements
np. percentile np.nanpercentile Compute rank-based statistics of elements

A

Various Aggregation functions

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

np. any

np. all

A

N/A Evaluate whether any elements are true

N/A Evaluate whether all elements are true

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

print(“25th percentile: “, np.percentile(heights, 25))
print(“Median: “, np.median(heights))
print(“75th percentile: “, np.percentile(heights, 75))

A

the aggregation operation reduced the entire array to a single summarizing value, which gives us information about the distribution of values. We may also wish to compute quantiles

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