1. Miha Stajdohar
  2. orange-modelmaps

Source

orange-modelmaps / archive / knn2matrix.py

from tools import *
from classifier2matrix import *

print "kNN: calculating classifiers"

data_d = getData(ROOT + "tab/breast-cancer-wisconsin-c.tab")
method = 7

distanceConstructors = [orange.ExamplesDistanceConstructor_Euclidean(),
                        orange.ExamplesDistanceConstructor_Manhattan(),
                        orange.ExamplesDistanceConstructor_Hamming(),
                        orange.ExamplesDistanceConstructor_Maximal()]
ks = [5, 9, 15]

print 'reading results'
modelData = orange.ExampleTable(ROOT + "dst/breast-allmodels-283.tab")
CAs, attributes = zip(*sorted([(ex['CA'].value, ex['label'].value.split(', ')) for ex in modelData])[-50:])

print 'constructing classifiers'
classifiers = []
labels = []
for atts in attributes:
    for k in ks:
        exclude = [att for att in data_d.domain if att.name not in atts + [data_d.domain.classVar.name]]
        data2 = orange.Preprocessor_ignore(data_d, attributes = exclude)
        
        for distanceConstructor in distanceConstructors:
            knn = orange.kNNLearner()
            knn.k = k
            knn.distanceConstructor = distanceConstructor
            classifiers.append(knn(data2))
            labels.append('KNN %s k=%d' % (str(distanceConstructor).split(' ')[0][29:], k))

print 'classifiers to matrix'
smx, RV = classifier2matrix(data_d, method, classifiers, OUT_FILE + "-knns", labels)
methods, uuids, res, projections, classifiers, attrs = RV