Commit 2f7c2732 authored by Taddeüs Kroes's avatar Taddeüs Kroes

StatRed ass3: Changed file structures.

parent 13a9a5bb
SEED=100
knnb:
echo 'nnb:'; \
python classify.py nnb ${SEED}; \
for i in 1 3 5 7 9; do \
echo 'knnb '$$i':'; \
python classify.py knnb $$i ${SEED}; \
done
clean:
rm knnb-*.pdf
from pylab import argmin, tile
class NNb:
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)]
class kNNb:
def __init__(self, X, c, k):
self.n, self.N = X.shape
self.X, self.c, self.k = X, c, k
def classify(self, x):
d = self.X - tile(x.reshape(self.n, 1), self.N);
dsq = sum(d*d, 0)
minindices = dsq.argsort()
# Count class occurrences in k nearest neighbours
hist = {}
for c in self.c[minindices[:self.k]]:
try:
hist[c] += 1
except KeyError:
hist[c] = 1
# Return the majority class
max_nbb = (0, None)
for c, count in hist.iteritems():
if count > max_nbb[0]:
max_nnb = (count, c)
return max_nnb[1]
class MEC:
def __init__(self, X, c):
self.n, self.N = X.shape
self.X, self.c = X, c
def classify(self, x):
# TODO: Implement MEC classifier
pass
from pylab import loadtxt, arange, loadtxt, permutation, transpose,\ from pylab import loadtxt, arange, loadtxt, permutation, transpose,\
zeros, sum, plot, subplot, array, scatter, logical_and, figure,\ zeros, sum, plot, subplot, array, scatter, logical_and, figure,\
show, savefig, tile, argmin, seed savefig, seed
from sys import argv, exit from sys import argv, exit
import classifiers
try: # Parse input
k = int(argv[1]) argc = len(argv)
except IndexError: if argc < 2:
print 'Usage: python %s K [ SEED ] (use K = -1 for the regular NNb method)' % argv[0] print 'Usage: python %s METHOD [ K ] [ SEED ]' % argv[0]
exit() exit()
method = argv[1]
try: if method == 'knnb':
if argc < 3:
print 'Usage: python %s %s K [ SEED ]' % tuple(argv[:2])
exit()
k = int(argv[2])
if argc == 4:
seed(int(argv[3]))
elif method not in ['nnb', 'mec']:
print 'Unknown classification method "%s"' % argv[1]
exit()
elif argc == 3:
seed(int(argv[2])) seed(int(argv[2]))
except IndexError:
pass
class NNb:
def __init__(self, X, c, k):
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)]
class kNNb:
def __init__(self, X, c, k):
self.n, self.N = X.shape
self.X, self.c, self.k = X, c, k
def classify(self, x):
d = self.X - tile(x.reshape(self.n, 1), self.N);
dsq = sum(d*d, 0)
minindices = dsq.argsort()
# Count class occurrences in k nearest neighbours
hist = {}
for c in self.c[minindices[:self.k]]:
try:
hist[c] += 1
except KeyError:
hist[c] = 1
# Return the majority class
max_nbb = (0, None)
for c, count in hist.iteritems():
if count > max_nbb[0]:
max_nnb = (count, c)
return max_nnb[1]
# Load dataset
def cnvt(s): def cnvt(s):
tab = {'Iris-setosa': 1.0, 'Iris-versicolor': 2.0, 'Iris-virginica': 3.0} tab = {'Iris-setosa': 1.0, 'Iris-versicolor': 2.0, 'Iris-virginica': 3.0}
if tab.has_key(s): if tab.has_key(s):
...@@ -60,26 +37,31 @@ ind = permutation(ind) # random permutation ...@@ -60,26 +37,31 @@ ind = permutation(ind) # random permutation
L = ind[0:90] # learning set indices L = ind[0:90] # learning set indices
T = ind[90:] # test set indices T = ind[90:] # test set indices
# Learning Set # Learning set
X = transpose(XC[L,0:4]) X = transpose(XC[L,0:4])
if k == -1: args = [X, XC[L,-1]]
knnc = NNb(X, XC[L,-1], k) if method == 'nnb':
else: method_class = classifiers.NNb
knnc = kNNb(X, XC[L,-1], k) elif method == 'knnb':
method_class = classifiers.kNNb
args.append(k)
elif method == 'mec':
method_class = classifiers.MEC
classifier = method_class(*args)
# Classification of Test Set # Classification of test set
c = zeros(len(T)) c = zeros(len(T))
for i in arange(len(T)): for i in arange(len(T)):
c[i] = knnc.classify(XC[T[i],0:4]) c[i] = classifier.classify(XC[T[i],0:4])
# Confusion Matrix # Confusion matrix
CM = zeros((3, 3)) CM = zeros((3, 3))
for i in range(3): for i in range(3):
for j in range(3): for j in range(3):
CM[i,j] = sum(logical_and(XC[T,4] == i + 1, c == j + 1)) CM[i,j] = sum(logical_and(XC[T,4] == i + 1, c == j + 1))
print CM print CM
# Plot Test Set # Plot test set
figure(1) figure(1)
color = array([[1,0,0], [0,1,0], [0,0,1]]) color = array([[1,0,0], [0,1,0], [0,0,1]])
for i in range(4): for i in range(4):
...@@ -92,5 +74,10 @@ for i in range(4): ...@@ -92,5 +74,10 @@ for i in range(4):
facecolor=[1,1,1]*len(T)) facecolor=[1,1,1]*len(T))
scatter(XC[T, i], XC[T, j], s=30, marker='+', scatter(XC[T, i], XC[T, j], s=30, marker='+',
edgecolor=color[c.astype(int) - 1]) edgecolor=color[c.astype(int) - 1])
#from pylab import show
#show() #show()
savefig('knnb-%d.pdf' % k) 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