Skip to content

Instantly share code, notes, and snippets.

@MaxHalford
MaxHalford / ratio_combos.py
Created October 31, 2017 17:24
Ratio combinations
import numpy as np
combos = []
def generate(remainder, n, current_combo=[], i=0, step=0.1):
if n == 1:
combos.append(np.round(current_combo + [remainder], 1))
return
for p in np.arange(step, remainder - (n-2) * step - step / 10, step):
@MaxHalford
MaxHalford / haar_wavelet.py
Created October 31, 2017 18:35
Haar wavelet
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
# Generate some data
a = 1.99
@MaxHalford
MaxHalford / setup.sh
Created April 7, 2018 12:33
AWS Kaggle setup
curl -O https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
bash Anaconda3-5.1.0-Linux-x86_64.sh # Say "yes" and "yes"
anaconda3/bin/pip install kaggle
anaconda3/bin/pip install lightgbm
mkdir .kaggle
echo "{"username":"maxhalford", "key":"secret"}" >> ~/.kaggle/kaggle.json
chmod 600 ~/.kaggle/kaggle.json
@MaxHalford
MaxHalford / multi.py
Last active February 28, 2020 10:36
Find count(s) from percentage(s)
import numpy as np
goal_fracs = np.array([.1, .9])
goal_fracs = np.array([.04, .04, .11, .14, .67])
counts = np.zeros_like(goal_fracs)
n = 1
tol = .001
a = 0
@MaxHalford
MaxHalford / callback.py
Created May 24, 2018 15:20
Keras eval callback
import keras
class EvalCallback(keras.callbacks.Callback):
def __init__(self, X_val, y_val, metric):
super().__init__()
self.X_val = X_val
self.y_val = y_val
self.metric = metric
@MaxHalford
MaxHalford / reverse.py
Created June 19, 2018 19:37
Reverse engineer one-hot encoded categorical variable
import collections
import itertools
df = pd.DataFrame({
'a': [1, 0, 0, 0],
'b': [0, 1, 0, 0],
'c': [0, 0, 1, 0],
'd': [0, 0, 0, 1],
'e': [1, 1, 0, 0],
@MaxHalford
MaxHalford / fit.py
Created August 6, 2018 09:25
Keras CNN blueprint
import glob
import imageio
from keras import layers
from keras import losses
from keras import models
from keras import optimizers
import numpy as np
import pandas as pd
@MaxHalford
MaxHalford / autoencoder.py
Created November 28, 2018 10:20
Keras autoencoder for timeseries
import itertools
from keras import layers as l
from keras import models as m
from keras import preprocessing as p
import numpy as np
import pandas as pd
def generate_series(paths, prepare, chunk_size=1e6):
@MaxHalford
MaxHalford / rpi.py
Last active March 1, 2019 15:36
NCAA rating formulas
"""
Rating percentage index (RPI).
Example taken from https://www.wikiwand.com/en/Rating_percentage_index#/Basketball_formula
"""
X = pd.DataFrame(
data=[
(2010, 'UConn', 64, 'Kansas', 57),
(2010, 'UConn', 82, 'Duke', 68),