Numpy Flashcards

1
Q

np.array([1, 2, 3, 4], dtype=’float32’)

A

explicitly set the data type of the resulting array

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

np.array([range(i, i + 3) for i in [2, 4, 6]])

A

nested lists result in multi-dimensional arrays

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

np.zeros(10, dtype=int)

A

Create a length-10 integer array filled with zeros

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

np.ones((3, 5), dtype=float)

A

Create a 3x5 floating-point array filled with ones

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

np.full((3, 5), 3.14)

A

Create a 3x5 array filled with 3.14

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

np.arange(0, 20, 2)

A
# Create an array filled with a linear sequence
# Starting at 0, ending at 20, stepping by 2
# (this is similar to the built-in range() function)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

np.linspace(0, 1, 5)

A

Create an array of five values evenly spaced between 0 and 1

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

np.random.random((3, 3))

A
# Create a 3x3 array of uniformly distributed
# random values between 0 and 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

np.random.normal(0, 1, (3, 3))

A
# Create a 3x3 array of normally distributed random values
# with mean 0 and standard deviation 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

np.random.randint(0, 10, (3, 3))

A

Create a 3x3 array of random integers in the interval [0, 10)

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

np.eye(3

A

Create a 3x3 identity matrix

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

np.empty(3)

A
# Create an uninitialized array of three integers
# The values will be whatever happens to already exist at that memory location
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

x2[1:2, 0]

A

In a multi-dimensional array, items can be accessed using a comma-separated tuple of indices

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

x2[0:, 0] = 15.8

x2

A

Values can also be modified using any of the above index notation

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

x[1::2]

A

every other element, starting at index 1

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

x[:2:-1]

A

all elements, reversed

17
Q

x2[:3, ::2]

A

all rows, every other column

18
Q

x2[0, :]

A

first row of x2

19
Q

x2[:, 0]

A

first column of x2

20
Q

x2_sub_copy = x2[:2, :2].copy()

A

explicitly copy the data within an array or a subarray

21
Q

grid = np.arange(1, 10).reshape((3, 3))

A

the size of the initial array must match the size of the reshaped array

22
Q

x[np.newaxis, :]

A

row vector via newaxis

23
Q

np.concatenate([grid, grid], axis=1)

A

concatenate along the second axis (zero-indexed)

24
Q

np.vstack([x, grid])

A

vertically stack the arrays

25
Q

np.hstack([grid, y])

A

horizontally stack the arrays

26
Q

x = [1, 2, 3, 99, 99, 3, 2, 1]

x1, x2, x3 = np.split(x, [3, 6])

A

opposite of concatenation is splitting

pass a list of indices giving the split points

27
Q

upper, lower = np.vsplit(grid, [2])

left, right = np.hsplit(grid, [1])

A

N + 1 subarrays. The related functions np.hsplit and np.vsplit are similar