One-dimensional Numpy Flashcards

1
Q

Creating a numpy array

A

a = np.array([0,1,2,3,4])

a

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

Changing an element in an array - same as in list

A

We can change the value of the array, consider the array c:

# Create numpy array
​
c = np.array([20, 1, 2, 3, 4])
c
array([20,  1,  2,  3,  4])
We can change the first element of the array to 100 as follows:

c[0]=100
c

array([100, 1, 2, 3, 4])

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

Slicing an array

A

d = c[1:4]
d
array([1, 2, 3])

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

Select multiple elements

A

Similarly, we can use a list to select a specific index. The list ‘ select ‘ contains several values:

0,2,3
# Create the index list
select = [0,2,3]

We can use the list as an argument in the brackets. The output is the elements corresponding to the particular index:

# Use List to select elements
d = c[select]
d

array([100, 2, 300])

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

Number of elements in an array

A

a.size

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

Number of dimensions in an array

A

a.ndim

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

Mean of an array

A

a.mean()

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

Max value

A

a.max()

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

Min value

A

a.min()

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

Dot product

A

np.dot(u, v)

7

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

Add a value to every elements in an array

A

u + 1

array([2, 3, 4, 0])

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

Linspace fucntion

A

np.linspace(-2, 2, num=5)

array([-2., -1., 0., 1., 2.])

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

Plot vectors

A

a = np.array([-1, 1])
b = np.array([1, 1])
Plotvec2(a, b)

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