Skip to content

Instantly share code, notes, and snippets.

View iandewancker's full-sized avatar

Ian Dewancker iandewancker

View GitHub Profile
@iandewancker
iandewancker / gist:a13357e98231e51864612b805422d08f
Created May 12, 2017 20:31
RF vs MLP sandbox for learning the "if" function
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)
@iandewancker
iandewancker / gist:e698014cdfdfa2ba562a479b75fc9766
Created August 11, 2017 22:28
Comparing decision function surfaces
%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)]
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
@iandewancker
iandewancker / gist:58c8e684e3120ebe139fd7ab951a9c8a
Created September 7, 2017 20:44
Replace CSV header names in place
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))
@iandewancker
iandewancker / gist:61186c21bfe411343adb434180ffdf0d
Created February 5, 2018 19:34
quick numerical gradient in numpy
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):
@iandewancker
iandewancker / gist:66afc25e49d073701fb179766572b31a
Created February 8, 2018 06:27
Feature importances in binary classifier
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]
@iandewancker
iandewancker / gist:0206cc059472e0839a4af5ee40d1a370
Created August 16, 2018 14:44
Convert keras model to pb file
# 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")
@iandewancker
iandewancker / gist:840748d4f9e2ff80c1dbcd57719d6bd3
Created August 23, 2018 14:24
custom weighted categorical loss for keras
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)
@iandewancker
iandewancker / gist:1231df22575771d8d709f00f78cb71a8
Created March 4, 2019 17:57
Fit and plot max likelihood Beta distribution to data
import scipy.stats
import numpy as np
import scipy.optimize
obs_data = [
0.08982035928143713
,0.06818181818181818
,0.012987012987012988
,0.05357142857142857
,0.045454545454545456
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)