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

StatRed ass3: Bug fix.

parent bd525558
...@@ -59,3 +59,13 @@ class MEC: ...@@ -59,3 +59,13 @@ class MEC:
p = [coeff * e**(-.5 * dot(x - mu, dot(S.I, array([x - 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] mu]).T)).tolist()[0][0]) for mu, S, coeff in self.class_data]
return self.classes[argmax([i.sum() for i in p])] 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,\ from pylab import loadtxt, arange, loadtxt, permutation, zeros, sum, \
zeros, sum, plot, subplot, array, scatter, logical_and, figure,\ plot, subplot, array, scatter, logical_and, figure, savefig, seed
savefig, seed
from sys import argv, exit from sys import argv, exit
import classifiers import classifiers
...@@ -17,7 +16,7 @@ if method == 'knnb': ...@@ -17,7 +16,7 @@ if method == 'knnb':
k = int(argv[2]) k = int(argv[2])
if argc == 4: if argc == 4:
seed(int(argv[3])) seed(int(argv[3]))
elif method not in ['nnb', 'mec']: elif method not in ['nnb', 'mec', 'svm']:
print 'Unknown classification method "%s"' % argv[1] print 'Unknown classification method "%s"' % argv[1]
exit() exit()
elif argc == 3: elif argc == 3:
...@@ -38,15 +37,11 @@ L = ind[0:90] # learning set indices ...@@ -38,15 +37,11 @@ L = ind[0:90] # learning set indices
T = ind[90:] # test set indices T = ind[90:] # test set indices
# Learning set # Learning set
X = XC[L,0:4] args = [XC[L,0:4].T, XC[L,-1]]
args = [X, XC[L,-1]] if method == 'knnb':
if method == 'nnb':
method_class = classifiers.NNb
elif method == 'knnb':
method_class = classifiers.kNNb
args.append(k) args.append(k)
elif method == 'mec': method_class = {'nnb': classifiers.NNb, 'knnb': classifiers.kNNb,
method_class = classifiers.MEC 'mec': classifiers.MEC, 'svm': classifiers.SVM}[method]
classifier = method_class(*args) classifier = method_class(*args)
# Classification of test set # 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