Skip to content

Instantly share code, notes, and snippets.

@pearcemc
pearcemc / ibp_vis
Last active August 29, 2015 14:14
Visualise the two parameter Indian Buffet Process
import sys
import time
from pylab import *
import scipy.stats as stats
"""
Script to visualise draws from a two parameter Indian Buffet Process.
usage example:
@pearcemc
pearcemc / APTS-modelling1-qc
Created March 24, 2015 17:59
APTS Southampton - Statistical Modelling - script for 1c
plot.aic <- function(fit, new=T, sd=0.1)
{ # code to plot AIC against order of AR model fitted
if (new) plot((1:length(fit$aic))-1,fit$aic,type="l",xlab="Order",ylab="AIC") else
lines((1:length(fit$aic))-1,fit$aic,type="l")
points(rnorm(1,fit$order,sd=sd),rnorm(1,sd=sd),pch=16,col="red")
}
plot.stat_ic <- function(stat, new=T, sd=0.1)
{ # code to plot AIC against order of AR model fitted
if (new) plot((1:length(stat))-1,stat,type="l",xlab="Order",ylab="AIC") else
@pearcemc
pearcemc / blockimage
Created April 28, 2015 11:04
Plotting functions for multidimensional image arrays.
from pylab import *
import numpy
def grid_sq(fr,ncol,bs,npx=2,bval=0):
Z = fr.shape[0]
z=0
vbar = bval*ones((bs[0],npx))
hbar = bval*ones((npx, (bs[1] + npx)*ncol + npx))
grid = [hbar]
row = [vbar]
@pearcemc
pearcemc / simgamma
Created May 21, 2015 10:27
Correct shape rate parameterisation of gamma and inverse gamma with scipy.stats
from pylab import *
import scipy.stats as stats
########################################################################################
#
# QUICK ILLUSTRATION OF USING SCIPY.STATS FOR GAMMA AND INV-GAMMA
# HOW TO DO SHAPE-RATE (alpha, beta) PARAMETERISATION CORRECTLY
#
########################################################################################
"""
Quick and dirty experimental implementation of k-nearest neighbours technique
"""
from scipy import randn
import scipy.stats as stats
import random
from kdata import *
class MultiClassDS:
"""
Generates a dataset of n points from c classes in ratio r
"""
def __init__(self, classes, length=1000, ratio=None):
#set params
self.classes, self.length, self.ratio = classes, length, ratio
#no ratio supplied? set it to even split
if not ratio: self._normalise_ratio()
from django.conf import settings #for RECAPTCHA_PRIVATE key
def verify_recaptcha(request):
import urllib, urllib2, re
values = {
"privatekey": settings.RECAPTCHA_PRIVATE,
"remoteip": request.META['REMOTE_ADDR'],
"challenge": request.POST['recaptcha_challenge_field'],
"response": request.POST['recaptcha_response_field'],
}
def vis3d(ds):
"""Visualise a 3d MultiClassDS"""
fig = plt.figure() #start pyplot
fig.suptitle('3d vis') #add a title
cols = ['g', 'b', 'r', 'y'] #colour options
ax = fig.add_subplot(111, projection="3d") #initialise 3d
ax.set_xlabel('X') #set axis labels
ax.set_ylabel('Y')
ax.set_zlabel('Z')
for i, clas in enumerate(ds.get_vectors()): #loop through the vectorised classes
import scipy as sp
class PDimClass:
"""
A p-dimensional class, which takes a list of Normal distribution parameters,
each of which defines a dimension
"""
#a list of normal distribution parameter tuples, for N(mean, sd)
params = [(0,1), (0,1)]
class TestPredictor:
"""Iterate a KNearest predictor and return its success rate."""
def __init__(self, predictor, times=1000):
#set the KNearest predictor
self.predictor = predictor
#set number of iterations
self.times = times
def test(self, k=1):