NumPy & SciPy Flashcards Preview

INF3331 > NumPy & SciPy > Flashcards

Flashcards in NumPy & SciPy Deck (29)
Loading flashcards...
1

create three-dimensional array of floats

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

2

get array/matrix dimensions

a.shape

3

get array/matrix datatype

a.dtype

4

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

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

5

create a list of uniformly spaced coordinates

linspace(start, end, steps)

6

construct array from list

array(list)

7

construct 2D-array from two lists

array([x, y])

8

from array to list

a.tolist()

9

convert to array

asarray(list)

10

change array dimensions

a.shape = (3, 2)
a = a.reshape(3, 2)

11

get number of elements in array

a.size

12

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

a[2:4] = 5

13

set last element equal to first

a[-1] = a[0]

14

set all elements of array equal to 0

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

15

print column k

print a[:, k]

16

print row l

print a[l, :]

17

a[:, ::2]

returns first and even indexed elements in all rows

18

a[:, 2::2]

returns even indexed elements in all rows

19

list[:] is a ... of the data
array[:] is a ... of the data

copy
reference

20

copy array

b = a.copy()

21

loop over 2D-array

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

22

perform 2x + 1 on every element in a

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

23

power function

x**2

24

exponential function

exp(a)

25

square root function

sqrt(a)

26

get mean, variance and standard deviation of array a

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

27

covariance

cov(x, y)

28

change type of array/matrix

a.astype(int)

29

module for curve plotting

matplotlib