Numpy4 Flashcards

1
Q

[x[3], x[7], x[2]]

A

we want to access three different elements
of
[51 92 14 71 60 20 82 86 74 74]

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

ind = [3, 7, 4]

x[ind]

A

pass a single list or array of indices to obtain the same result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
ind = np.array([[3, 7],
                [4, 5]])
x[ind]
array([[71, 86],
       [60, 20]])
A

When using fancy indexing, the shape of the result reflects the shape of the index arrays rather than the shape of the array being indexed

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

X[row, col]

A

Like with standard indexing, the first index refers to the row, and the second to the column:

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

X[1:, [2, 0, 1]]

A

We can also combine fancy indexing with slicing

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

x = np.arange(10)
i = np.array([2, 1, 8, 4])
x[i] = 99
print(x)

A

[ 0 99 99 3 99 5 6 7 99 9]

can also be used to modify parts of an array

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

x = np.zeros(10)
np.add.at(x, i, 2)
print(x)

A

The at() method does an in-place application of the given operator at the specified indices (here, i) with the specified value (here, 1). Another method that is similar in spirit is the reduceat() method of ufuncs, which you can read about in the NumPy documentation.

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

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

np.sort(x)

A

return a sorted version of the array without modifying the input

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

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

A

related function is argsort, which instead returns the indices of the sorted elements

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

np.sort(X, axis=0)

A

sort each column of X

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

np.sort(X, axis=1)

A

sort each row of X

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

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

np.partition(x, 3)

A

np.partition takes an array and a number K; the result is a new array with the smallest K values to the left of the partition, and the remaining values to the right, in arbitrary order:

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

np.partition(X, 2, axis=1)

A

Similarly to sorting, we can partition along an arbitrary axis of a multidimensional array

The result is an array where the first two slots in each row contain the smallest values from that row, with the remaining values filling the remaining slots.

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

dist_sq = np.sum((X[:, np.newaxis, :] - X[np.newaxis, :, :]) ** 2, axis=-1)

A

Now we’ll compute the distance between each pair of points. Recall that the squared-distance between two points is the sum of the squared differences in each dimension; using the efficient broadcasting (Computation on Arrays: Broadcasting) and aggregation (Aggregations: Min, Max, and Everything In Between) routines provided by NumPy we can compute the matrix of square distances in a single line of code:

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