Skip to content

Instantly share code, notes, and snippets.

@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 {
@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 / Timer.py
Last active December 30, 2015 12:29
Python's timer module only takes statements as strings. Here's a timer class that you can use with a "with" statement to time even0 multi-line statements.
import datetime
class Timer:
def __init__(self, string=None):
self.string = string
def __enter__(self):
self.t1 = datetime.datetime.now()
def __exit__(self, exc_type, exc_value, traceback):