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
import numpy as np | |
import sklearn.ensemble | |
from sklearn.neural_network import MLPClassifier | |
X = np.random.uniform(low=0, high=5000000,size=(10000,2)) | |
y = np.array(X[:,0] > X[:,1],dtype=int) | |
X_test = np.random.uniform(low=0,high=5000000,size=(10000,2)) | |
y_test = np.array(X_test[:,0] > X_test[:,1],dtype=int) | |
clf_rf = sklearn.ensemble.RandomForestClassifier(n_estimators=50,criterion='entropy', max_depth=2) |
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
%matplotlib | |
import matplotlib | |
matplotlib.rcParams.update({'font.size': 32}) | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
import numpy as np; np.random.seed(10) | |
mean1, cov1 = [0, 2], [(0.5, .25), (.25, 0.5)] | |
mean2, cov2 = [2.5, -0.5], [(0.01, 0.02), (0.02, 0.01)] | |
mean3, cov3 = [3.2, 1.0], [(0.01, 0.02), (0.02, 0.01)] |
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
X_test = np.vstack([test_x1, test_x2, test_x3, test_x4, test_x5, test_x6]) | |
y_test = np.array([1.0, 1.0, 1.0, 1.0, -1.0, -1.0]) | |
clf.model.coef_ = clf.model.coef_.reshape(1,clf.model.coef_.shape[0]) | |
# hack to set the classes | |
try: | |
clf.model.fit([],[0,1]) | |
except: | |
pass |
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
def replace_dot_header(file_name): | |
with open(file_name, 'r+b') as f: | |
line = next(f) # grab first line | |
old = '.' | |
new = '_' | |
f.seek(0) # move file pointer to beginning of file | |
f.write(line.replace(old, new)) |
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
def test_f(x): | |
x1 = x[0] | |
y1 = x[1] | |
return 2*x1**2 + 2*x1 + 10 + y1**2 - 5*y1 | |
""" | |
f : reference to python function that takes | |
x_0 : numpy array of shape (1,N) where N is dependent on function | |
""" | |
def finite_difference_grad(f, x_0, h): |
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
X_g = X[np.where(y==1)] | |
X_b = X[np.where(y==0)] | |
M = X.shape[1] | |
ranges = [] | |
for i in xrange(M): | |
ranges.append((np.min(X[:,i]), np.max(X[:,i]))) | |
importances = [] | |
for i in xrange(M): | |
g_dist = np.histogram(X_g[:,i],bins=50,density=True,range=ranges[i])[0] |
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
# load keras model from disk | |
model_name = "GVC_IncepvtionV3_epoch_6_vanilla_vgg_chute_date_2018_07_27" | |
json_file = open(model_name+'.json', 'r') | |
model_json = json_file.read() | |
json_file.close() | |
model = tensorflow.keras.models.model_from_json(model_json) | |
# load weights into new model | |
model.load_weights(model_name+".h5") | |
print("Loaded model from disk") |
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
def custom_cat_crossentropy(y_true, y_pred): | |
# negate 3x3 sub matrix of income matrix to get cost matrix | |
# check https://github.com/tensorflow/tensorflow/blob/r1.10/tensorflow/python/keras/backend.py#L3461 | |
cost_m = tf.constant([[ 0.00222222, 0.01111111, 0.00222222], | |
[ 0.00222222, -0.05888889, 0.00222222], | |
[ 0.00222222, 1.51111111, 0.00222222]]) | |
y_true = tf.matmul(y_true, cost_m) | |
return tf.keras.losses.categorical_crossentropy(y_true, y_pred) |
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
import scipy.stats | |
import numpy as np | |
import scipy.optimize | |
obs_data = [ | |
0.08982035928143713 | |
,0.06818181818181818 | |
,0.012987012987012988 | |
,0.05357142857142857 | |
,0.045454545454545456 |
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
import glob | |
images = glob.glob("*.jpg") | |
import re | |
import os | |
import matplotlib.pyplot as plt | |
def natural_sort(l): | |
convert = lambda text: int(text) if text.isdigit() else text.lower() | |
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] | |
return sorted(l, key = alphanum_key) |
OlderNewer