Commit bd525558 authored by Taddeüs Kroes's avatar Taddeüs Kroes

StatRed ass3: Implemented part 2.

parent 2f7c2732
from pylab import argmin, tile
from pylab import argmin, argmax, tile, unique, argwhere, array, mean, \
newaxis, dot, pi, e, matrix
class NNb:
def __init__(self, X, c):
......@@ -37,7 +38,24 @@ class MEC:
def __init__(self, X, c):
self.n, self.N = X.shape
self.X, self.c = X, c
self.classes = unique(c)
self.estimate()
def estimate(self):
"""Estimate the mean and covariance matrix each class in the learning
set."""
self.class_data = []
for c in self.classes:
indices = argwhere(array(map(lambda x: x if x == c else 0,
self.c))).T[0]
X = self.X[:,indices]
mu = mean(X, 1)
Yzm = X - tile(mu[:,newaxis], X.shape[1])
S = matrix(dot(Yzm, Yzm.T) / (self.n - 1))
coeff = 1 / (S.A**-.5 * (2 * pi)**(self.n / 2))
self.class_data.append((mu, S, coeff))
def classify(self, x):
# TODO: Implement MEC classifier
pass
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])]
......@@ -38,7 +38,7 @@ L = ind[0:90] # learning set indices
T = ind[90:] # test set indices
# Learning set
X = transpose(XC[L,0:4])
X = XC[L,0:4]
args = [X, XC[L,-1]]
if method == 'nnb':
method_class = classifiers.NNb
......@@ -74,10 +74,10 @@ for i in range(4):
facecolor=[1,1,1]*len(T))
scatter(XC[T, i], XC[T, j], s=30, marker='+',
edgecolor=color[c.astype(int) - 1])
#from pylab import show
#show()
if method == 'knnb':
filename = 'knnb-%d.pdf' % k
else:
filename = '%s.pdf' % method
savefig(filename)
from pylab import show
show()
#if method == 'knnb':
# filename = 'knnb-%d.pdf' % k
#else:
# filename = '%s.pdf' % method
#savefig(filename)
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