Last active
June 27, 2019 23:42
-
-
Save rhngla/083390be769874d61c8ebea38274ad58 to your computer and use it in GitHub Desktop.
Python tricks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Ref: http://akuederle.com/create-numpy-array-with-for-loop | |
import numpy as np | |
n_results_per_iter = 4 | |
result_array = np.empty((0, n_results_per_iter)) | |
for iter in [1,2,3]: | |
result_this_iter = np.array([0,1,2,3]) | |
result_array = np.append(result_array, [result_this_iter], axis=0) | |
#Slicing rows and columns from a matrix simultaneously. The key is to use broadcasting. | |
#https://stackoverflow.com/questions/22927181/selecting-specific-rows-and-columns-from-numpy-array | |
M = np.random.randint(10,size=(10,10)) | |
rows = np.random.randint(M.shape[0],size=2) | |
cols = np.random.randint(M.shape[1],size=3) | |
M[np.ix_(rows, cols)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment