Skip to content

Instantly share code, notes, and snippets.

View sergeyf's full-sized avatar
💭
º¿º

Sergey Feldman sergeyf

💭
º¿º
View GitHub Profile
import numpy as np
from matplotlib import pylab as plt
#from mpltools import style # uncomment for prettier plots
#style.use(['ggplot'])
# generate all bernoulli rewards ahead of time
def generate_bernoulli_bandit_data(num_samples,K):
CTRs_that_generated_data = np.tile(np.random.rand(K),(num_samples,1))
true_rewards = np.random.rand(num_samples,K) < CTRs_that_generated_data
return true_rewards,CTRs_that_generated_data
import numpy as np
from sklearn.feature_extraction import image
from sklearn.cluster import MiniBatchKMeans
from sklearn import cross_validation, svm, datasets
from sklearn.datasets import fetch_olivetti_faces, fetch_mldata
from matplotlib import pylab as pl
def HIK_kernel(X,Y):
return np.array([[np.sum(np.minimum(x,y)) for y in Y] for x in X])
@sergeyf
sergeyf / igamma.py
Last active December 20, 2015 10:48
An alternative parameterization for scipy's invgamma which uses the parameters 'alpha' and 'beta'. See https://en.wikipedia.org/wiki/Inverse_gamma for more info. The original is here: http://en.it-usenet.org/thread/16070/1596/#post1596
from scipy.stats import rv_continuous
from scipy.special import gammaln, gammaincinv, gammainc
from numpy import log,exp
class igamma_gen(rv_continuous):
def _pdf(self, x, a, b):
return exp(self._logpdf(x,a,b))
def _logpdf(self, x, a, b):
return a*log(b) - gammaln(a) -(a+1)*log(x) - b/x
def _cdf(self, x, a, b):
import numpy as np
import matplotlib.pyplot
mu, sigma = 3., 1. # mean and standard deviation
s = np.random.lognormal(mu, sigma, 10000)
log_s = np.log(s)
subplot(211)
count,bins,_ = hist(s, 100, normed=True, align='mid')
x = np.linspace(min(bins), max(bins), 10000)
pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) / (x * sigma * np.sqrt(2 * np.pi)))