Skip to content

Instantly share code, notes, and snippets.

View xiaohan2012's full-sized avatar
🍠
eating sweat potatos

Xiao Han xiaohan2012

🍠
eating sweat potatos
View GitHub Profile
/* The API controller
Exports 3 methods:
* post - Creates a new thread
* list - Returns a list of threads
* show - Displays a thread and its posts
*/
var Thread = require('../models/thread.js');
var Post = require('../models/post.js');
@xiaohan2012
xiaohan2012 / Weighted-majority-experiment.readme
Last active July 14, 2018 02:07
Weighted Majority algorithm used in online learning
Three files are included:
1. `weighted_majority.py`: the weighted majority algorithm
2. `main.py`: the main program which takes a specific beta value and make a list of plots
3. `experiment.sh`: bash script that experiments on a list of beta values. This is a good entry of this gist.
@xiaohan2012
xiaohan2012 / n_armed_testbed.m
Last active January 3, 2016 20:39
Using the epsilon-greedy strategy to generate the n-armed bandit testbed.
function [] = n_armed_testbed(nB,nA,nP,sigma)
%
% Generates the 10-armed bandit testbed.
%
% Inputs:
% nB: the number of bandits
% nA: the number of arms
% nP: the number of plays (times we will pull a arm)
% sigma: the standard deviation of the return from each of the arms
%
@xiaohan2012
xiaohan2012 / naive_bayes_spam_classifier.py
Created January 23, 2014 21:43
A Naive Bayes spam classifier
from __future__ import division
from itertools import groupby
from collections import Counter
texts = [('spam', ['FREE', 'online', '!!!']),
('safe', ['results', 'repository','online']),
('spam', ['FREE','online','results','FREE', '!!!']),
('spam', ['!!!', 'registration','FREE','!!!']),
('safe', ['conference', 'online', 'registration', 'conference']),
('safe', ['conference', 'results', 'repository', 'rsults'])]
@xiaohan2012
xiaohan2012 / hmm.py
Created February 1, 2014 19:40
A demo for Hidden Markov Model Inference. Forward-backward algorithm and Viterbi algorithm involved.
from __future__ import division
from collections import Counter, defaultdict
import operator
A = 'A'; H = 'H'
STATES = ['alpha', 'beta']; OBS = [A, H]
#given a list of training data, list of (sts, obs) pairs,
#derive the HMM model parameters
@xiaohan2012
xiaohan2012 / MLE_MAP_param_estimation.py
Created February 12, 2014 12:27
MLE and MAP paramter estimation demo
from __future__ import division
class Data ():
def __init__ (self, data, prev={}):
#prev is used to determined the alpha value in the MAP parameter estimation
self.d = data;
self.prev = prev
def filter (self, **kwargs):
return Data(filter(lambda row: reduce (lambda acc, (field, value): acc and (row [field] == value), kwargs.items (), True),
@xiaohan2012
xiaohan2012 / em.m
Last active August 29, 2015 13:56
Expectation Maximization algorithm for Gaussian Mixture Model
%% -----------------------------------------------------------
%% The EM part!
%% -----------------------------------------------------------
function [means, stds, P] = em (X, K)
%input:
%% X, the data points
%% K, the component number
[N, featureNumber] = size (X);
<!DOCTYPE html>
<meta charset="utf-8">
<style>
circle,
path {
cursor: pointer;
}
circle {
@xiaohan2012
xiaohan2012 / profiler.py
Last active August 29, 2015 14:06
Easy-to-use profiler
##Usage:
# with profiler_manager():
# do want you want
from contextlib import contextmanager
import cProfile, pstats, StringIO
@contextmanager
def profiler_manager():
pr = cProfile.Profile()
@xiaohan2012
xiaohan2012 / cyk.py
Created October 25, 2014 11:18
CYK parsing algorithm
"""
The CYK parsing algorithm
"""
from collections import defaultdict
from itertools import product
import texttable #needs to be installed, can be accessed at http://foutaise.org/code/texttable/
def _print_tabel(table, nrow, ncol):
"""Utility function to print to dynamic programming table"""
t = texttable.Texttable()