Numpy Flashcards
reshape (len(y)L,) vector into (len(y)L, 1L)
y = y.reshape((len(y),1))
column wize means and standard deviations of X np array
means, stds = X.mean(axis=0), X.std(axis=0)
adding column of 1s to np array X (2ways)
X = np.hstack((np.ones((len(X[:,0]),1)),X)) X = np.c_[np.ones((len(X[:,0]),1)),X]
median of y vector
np.median(y)
matrix multiplication for transposed X and X
np.dot(X.T,X)
solve X * v = y for v
v = np.linalg.solve(X, y)
creating pseudorandom number
np.random.seed(seed)
random_ind = np.random.randint(X.shape[0])
calculate euclidean distance between w1 and w0
weight_dist = np.linalg.norm(w1 - w0)
create [4,1] np array with zeros
w_init = np.zeros((4, 1))
create np array from list
and list from np.array
some_array = np.array(some_list)
column wize sum of elements in np_array
np_array.sum(axis=0)
number of rows in np_array
np_array.shape[0]
flatten array a
equivalent to a.reshape(-1)
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 to count the occurrence of certain item in an ndarray in Python
unique, counts = numpy.unique(a, return_counts=True)
dict(zip(unique, counts))
Sort np.array a in descending order
-np.sort(-a)