Numpy Flashcards

1
Q

reshape (len(y)L,) vector into (len(y)L, 1L)

A

y = y.reshape((len(y),1))

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

column wize means and standard deviations of X np array

A

means, stds = X.mean(axis=0), X.std(axis=0)

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

adding column of 1s to np array X (2ways)

A
X = np.hstack((np.ones((len(X[:,0]),1)),X))
X = np.c_[np.ones((len(X[:,0]),1)),X]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

median of y vector

A

np.median(y)

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

matrix multiplication for transposed X and X

A

np.dot(X.T,X)

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

solve X * v = y for v

A

v = np.linalg.solve(X, y)

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

creating pseudorandom number

A

np.random.seed(seed)

random_ind = np.random.randint(X.shape[0])

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

calculate euclidean distance between w1 and w0

A

weight_dist = np.linalg.norm(w1 - w0)

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

create [4,1] np array with zeros

A

w_init = np.zeros((4, 1))

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

create np array from list

and list from np.array

A

some_array = np.array(some_list)

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

column wize sum of elements in np_array

A

np_array.sum(axis=0)

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

number of rows in np_array

A

np_array.shape[0]

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

flatten array a

equivalent to a.reshape(-1)

A

np.ravel(a,order=’C’)
1)possible orders: {‘C’,’F’, ‘A’, ‘K’}
‘C’ means to index the elements in row-major
‘F’ means to index the elements in column-major
‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise
‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative

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

How to count the occurrence of certain item in an ndarray in Python

A

unique, counts = numpy.unique(a, return_counts=True)

dict(zip(unique, counts))

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

Sort np.array a in descending order

A

-np.sort(-a)

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

correlation coefficient between numpy list1 and list2

A

numpy.corrcoef(list1, list2)[0, 1]

17
Q

index of maximum element in numpy array

A

a.argmax(axis=0)

18
Q

Convert predictions into categories

A

import numpy as np

preds = [1.22, 3.32, 6.54, 3.22, 6.78, 8.1, 2.01]
splits = [0, 1.5, 2.5, 3, 4.2, 5.8, 6.5, 7]

response = np.digitize(preds, splits)

19
Q

add vectors of different sizes

A

placebo_zeros = zeros((11034))
placebo_ones = ones((189))
placebo_ones.resize(placebo_zeros.shape)

placebo_data = placebo_zeros + placebo_ones