Commit 443e0415 authored by Taddeüs Kroes's avatar Taddeüs Kroes

StatRed ass3: Bug fix.

parent bd525558
......@@ -59,3 +59,13 @@ class MEC:
p = [coeff * e**(-.5 * dot(x - mu, dot(S.I, array([x -
mu]).T)).tolist()[0][0]) for mu, S, coeff in self.class_data]
return self.classes[argmax([i.sum() for i in p])]
class SVM:
def __init__(self, X, c):
self.n, self.N = X.shape
self.X, self.c = X, c
def classify(self, x):
d = self.X - tile(x.reshape(self.n, 1), self.N);
dsq = sum(d*d, 0)
return self.c[argmin(dsq)]
from pylab import loadtxt, arange, loadtxt, permutation, transpose,\
zeros, sum, plot, subplot, array, scatter, logical_and, figure,\
savefig, seed
from pylab import loadtxt, arange, loadtxt, permutation, zeros, sum, \
plot, subplot, array, scatter, logical_and, figure, savefig, seed
from sys import argv, exit
import classifiers
......@@ -17,7 +16,7 @@ if method == 'knnb':
k = int(argv[2])
if argc == 4:
seed(int(argv[3]))
elif method not in ['nnb', 'mec']:
elif method not in ['nnb', 'mec', 'svm']:
print 'Unknown classification method "%s"' % argv[1]
exit()
elif argc == 3:
......@@ -38,15 +37,11 @@ L = ind[0:90] # learning set indices
T = ind[90:] # test set indices
# Learning set
X = XC[L,0:4]
args = [X, XC[L,-1]]
if method == 'nnb':
method_class = classifiers.NNb
elif method == 'knnb':
method_class = classifiers.kNNb
args = [XC[L,0:4].T, XC[L,-1]]
if method == 'knnb':
args.append(k)
elif method == 'mec':
method_class = classifiers.MEC
method_class = {'nnb': classifiers.NNb, 'knnb': classifiers.kNNb,
'mec': classifiers.MEC, 'svm': classifiers.SVM}[method]
classifier = method_class(*args)
# Classification of test set
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment