This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Quick and dirty experimental implementation of k-nearest neighbours technique | |
""" | |
from scipy import randn | |
import scipy.stats as stats | |
import random | |
from kdata import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KNearest: | |
"""k-nearest neighbour inferer""" | |
def __init__(self, ds): | |
#set the dataset | |
self.ds = ds | |
def predict(self, p1, k=1): | |
"""Given a test point p1, return the modal class of its knearest neighbours""" | |
distances = [] | |
#calculate the distance between the test point and known data points. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from math import exp, sqrt, pi | |
#the function we're going to map | |
def gausspdf( x, mu, sd2 ): | |
return exp( -1*( (x - mu)**2/(2*sd2)) )/sqrt( 2*pi*sd2 ) | |
#canvas size | |
x = y = np.arange( 0, 10, 0.1 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################## | |
# | |
# APPLYING EM TO A BINOMIAL MIXTURE MODEL | |
# matthew@refute.me.uk | |
# Licence: MIT Licence (http://opensource.org/licenses/MIT) | |
# | |
################################################################## | |
###### EM ALGORITHM FUNCTIONS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
OlderNewer