Skip to content

Instantly share code, notes, and snippets.

@tianhuil
tianhuil / Residua Estimator.py
Last active November 8, 2019 22:54
This is a Residual Regressor and Residual Classifier
from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin
class _ResidualEstimator(BaseEstimator):
def __init__(self, base, residual):
self.base = base
self.residual = residual
def fit(self, X, y):
self.base.fit(X, y)
self.residual.fit(X, y - self.base.predict(X))
@tianhuil
tianhuil / update_branches.ipy
Last active December 3, 2017 18:25
Finds branches that can be safely merged
#!ipython
# to use this run these two steps
# curl https://gist.githubusercontent.com/tianhuil/a6675835a7a0c157fbcb296a743f52d4/raw/704a20201dd4362928f6e39ab0ab0bc0784b2af9/update_branches.ipy
# ipython merged_branches.ipy
!git checkout master
branches = !git branch
other_branches = [b.lstrip() for b in branches if b != '* master']
results = {}
# cd into a new folder and observe the error in the last line.
yes | rm email.py*
echo "from email.utils import formatdate; print 'OK'" > foo.py
PYTHONPATH="" python foo.py
touch email.py
PYTHONPATH="" python foo.py
@tianhuil
tianhuil / Learning_React.md
Created December 27, 2016 03:56
Learning React
@tianhuil
tianhuil / GroupedHorizontalBarGraph.html
Last active July 26, 2016 21:19
Grouped Horizontal Bar Graph in d3. This is simply reversing the x and y axes of http://bl.ocks.org/mbostock/3887051 This version has abstracted the data and graphics layers so that it is easy to reuse.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
@tianhuil
tianhuil / col2tsv.py
Created January 9, 2014 04:28
col2tsv: converts a single column by breaking it up by the first column
#!/usr/bin/env python
import sys
n_rows = int(sys.argv[1])
data = map(lambda x: [], xrange(n_rows))
for k, line in enumerate(sys.stdin):
data[k % n_rows] += [line.rstrip()]
@tianhuil
tianhuil / VarianceBiasPlot.py
Last active January 1, 2016 22:39
Variance-Bias Plots
import numpy as np
from collections import namedtuple
# Create a random dataset
rng = np.random.RandomState(42)
N_points = 10000
X = np.sort(5 * rng.rand(N_points, 1), axis=0)
y = np.sin(X).ravel()
y += .4 * (0.5 - rng.rand(N_points))
@tianhuil
tianhuil / LearningCurve.py
Created January 1, 2014 21:06
Learning Curves for trees of different depths
import numpy as np
from collections import namedtuple
# Create a random dataset
rng = np.random.RandomState(42)
N_points = 100
X = np.sort(5 * rng.rand(N_points, 1), axis=0)
y = np.sin(X).ravel()
y += .4 * (0.5 - rng.rand(N_points))
@tianhuil
tianhuil / DecisionTreeVersusLinearRegression.py
Created January 1, 2014 16:50
Decision tree versus linear regression comparison plot
import numpy as np
# Create a random dataset
rng = np.random.RandomState(1)
X = np.sort(5 * rng.rand(80, 1), axis=0)
y = np.sin(X).ravel()
y += .2 * (0.5 - rng.rand(80))
# Fit regression model
from sklearn.tree import DecisionTreeRegressor
@tianhuil
tianhuil / GroupVerticalBarGraph.html
Created December 18, 2013 19:41
Based on http://bl.ocks.org/mbostock/3887051 but more modular so that it's more reusable.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {