NumPy & SciPy Flashcards

1
Q

create three-dimensional array of floats

A

import numpy
x = y = z = 5
zeros((x, y, z), np.float)

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

get array/matrix dimensions

A

a.shape

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

get array/matrix datatype

A

a.dtype

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

given an array a, make a new array of same dimension and data type

A

x = zeros(a.shape, a.dtype)

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

create a list of uniformly spaced coordinates

A

linspace(start, end, steps)

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

construct array from list

A

array(list)

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

construct 2D-array from two lists

A

array([x, y])

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

from array to list

A

a.tolist()

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

convert to array

A

asarray(list)

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

change array dimensions

A
a.shape = (3, 2)
a = a.reshape(3, 2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

get number of elements in array

A

a.size

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

set a[2] and a[3] equal to 5

A

a[2:4] = 5

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

set last element equal to first

A

a[-1] = a[0]

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

set all elements of array equal to 0

A

a[:] = 0
a[:, :] = 0
a.fill(0)

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

print column k

A

print a[:, k]

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

print row l

A

print a[l, :]

17
Q

a[:, ::2]

A

returns first and even indexed elements in all rows

18
Q

a[:, 2::2]

A

returns even indexed elements in all rows

19
Q

list[:] is a … of the data

array[:] is a … of the data

A

copy

reference

20
Q

copy array

A

b = a.copy()

21
Q

loop over 2D-array

A

for y in xrange(a.shape[0]):
for x in xrange(a.shape[1]):
print a[y, x]

22
Q

perform 2x + 1 on every element in a

A
a = 2*a + 1
# this is much faster than element-wise operations
23
Q

power function

A

x**2

24
Q

exponential function

A

exp(a)

25
Q

square root function

A

sqrt(a)

26
Q

get mean, variance and standard deviation of array a

A

a. mean(), mean(a)
a. var(), var(a)
a. std(), std(a)

27
Q

covariance

A

cov(x, y)

28
Q

change type of array/matrix

A

a.astype(int)

29
Q

module for curve plotting

A

matplotlib