Python NumPy2 Create, Restructure, Arrays Flashcards

1
Q

How can you easily create a vector of zeroes?

A

Produce a vector of 3 zeros like this:
np.zeros(3)

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

How would you create a 2X3 matrix of zeros?

A

np.zeros((2,3))
Notice the second pair of parentheses
i.e. the argument is a tuple

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

OK, how to create a vector of four 1’s?

A

np.ones(4)
Notice there is no half-open interval business going on here. You are directly specifying how many elements you want.

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

And what about matrix of 1’s. Do a 4 X3

A

np.ones((4,3))

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

How would you create 11 equally spaced points between 0 and 5?

A

np.linspace(0,5,11)
You know the endpoints and number of points, it calculates the interval for you. Contrast with arange, which given the endpoints and interval, it deduces the number of points. «np.linspace(0,5,11) and np.arange(0,5,.5) return the same thing»

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

How would you create a 4X4 identity matrix?

A

Using the “eye” function
np.eye(4)

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

How do you reshape a vector or dimension?

A

Using the reshape method. For example, np.eye(4).reshape((2,8)), also see image

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

How do you get the minimum, maximum, and standard deviation of an array?

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

Never mind finding the maximum value of an array; how would you find the position of the maximum value of an array?

A

use the argmax function.

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

How can you query the shape or data type of an array?

A

Use the attributes shape and dtype. Note they do not have parentheses.

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