This file contains hidden or 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
# sort array with regards to nth column | |
# copied from https://gist.github.com/stevenvo/e3dad127598842459b68#file-numpy-array-sort-py | |
arr = arr[arr[:,n].argsort()] |
This file contains hidden or 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
import itertools | |
colors = ['u', 'g', 'r', 'i', 'z'] | |
subsets_size_two = list(itertools.combinations(colors, 2)) | |
# subsets_size_two: | |
# [('u', 'g'), | |
# ('u', 'r'), | |
# ('u', 'i'), | |
# ('u', 'z'), |
This file contains hidden or 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
from sklearn.model_selection import KFold | |
kf = KFold(n_splits=k, random_state=0, shuffle=True) | |
for i, (train, test) in enumerate(kf.split(X, y)): | |
model.fit(X[train], y[train]) | |
y_pred = model.predict(X[test]) |
This file contains hidden or 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
import random | |
import string | |
def generate_random_key(length): | |
return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(length)) | |
print generate_random_key(30) |
This file contains hidden or 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
// Tested on Swift 4.2 | |
func pathFromComponents(_ _arr: String...) -> String { | |
var arr = _arr | |
for k in 0..<arr.count { | |
if arr[k][0] == "/" { | |
arr[k] = String(arr[k].dropFirst()) | |
} | |
This file contains hidden or 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
def latexRowToArray(s): | |
nums = s.split("&") | |
for k in range(len(nums)): | |
nums[k] = ''.join([i for i in nums[k] if i.isdigit() or i in ['.']]) | |
nums[k] = float(nums[k]) | |
return np.array(nums) | |
taus = '$0.00325$ & $0.00228$ & $0.00253$ & $0.00284$ & $0.00272$ & $0.00272$ \\ \hline' | |
latexRowToArray(taus) | |
#output: array([0.00325, 0.00228, 0.00253, 0.00284, 0.00272, 0.00272]) |
This file contains hidden or 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
def arrayToLatexRow(a): | |
ns = [] | |
for n in a: | |
ns.append("${0}$".format(n)) | |
ns = ' & '.join(ns) | |
ns = ns + " \\ \hline" | |
return ns | |
tauarr = np.array([0.00325, 0.00228, 0.00253, 0.00284, 0.00272, 0.00272]) |
This file contains hidden or 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
def find_nearest_index(array, value): | |
array = np.asarray(array) | |
idx = (np.abs(array - value)).argmin() | |
return idx |
This file contains hidden or 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
def split_scientific_float(n_prec, _f): | |
v=('{:.'+str(n_prec)+'g}').format(_f).split('e') | |
if len(v)==1: v.append(0) | |
return v | |
split_scientific_float(5, 4.311237638482733e-91) # Output: ['4.3112', '-91'] |
This file contains hidden or 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
import time | |
## 24 hour format ## | |
def print_t(str_): | |
print( "[" + time.strftime("%Y-%m-%d %H:%M:%S") + "] " + str(str_)) |
OlderNewer