Skip to content

Instantly share code, notes, and snippets.

View dnnagy's full-sized avatar

Nagy Dániel dnnagy

  • Wigner Research Centre for Physics
  • Budapest
  • 06:57 (UTC +02:00)
View GitHub Profile
# 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()]
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'),
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])
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)
// 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())
}
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])
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])
def find_nearest_index(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return idx
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']
import time
## 24 hour format ##
def print_t(str_):
print( "[" + time.strftime("%Y-%m-%d %H:%M:%S") + "] " + str(str_))