iafitpredict_wes() - Function

This function performs the OPF supervised classification algorithm.

  • OPF_labels_test = iaClassifier_wes(test_set,train_set, train_labels)

    • Output

      • OPF_labels_test: Vector with the labels of the tests samples.
    • Input

      • test_set: Matrix with the attributes of the test set. The attributes are disposed by lines.
      • train_set: Matrix with the attributes of the train set. The attributes are disposed by lines.
      • train_labels: Vector with the labels of the training samples.

Function Code

 1 ###########################################
 2 # Fit / Predict
 3 ###########################################
 4 
 5 def EncontrarPrototipos(MST,Rotulos):
 6     ListaPrototipos=[]
 7     LA=GrafoMA2LAZero(MST,True)
 8     while LA:
 9        [origem,destino,custo]=LA.pop()
10        if Rotulos[origem]!=Rotulos[destino]:
11           ListaPrototipos.append(origem)
12           ListaPrototipos.append(destino)
13     return set(ListaPrototipos)
14 
15 def iaFit(Adj,lblTrain):
16    MST = KruskalNew(Adj,'float')
17    Seeds = EncontrarPrototipos(MST,lblTrain)
18    D = DijkstraSeedsNew(Zero2Inf(MST), Seeds)
19    return D
20 
21 
22 def iaPredict(FeatsTest, FeatsTrain, FitDistances, LabelsTrain):
23    Rotulos = zeros(FeatsTest.shape[0])
24 
25    for i in xrange(FeatsTest.shape[0]):
26       Distances = euclidianVectorial(FeatsTest[i],FeatsTrain)
27       Nearest = argmin(maximum(Distances,FitDistances))
28       Rotulos[i]=LabelsTrain[Nearest]
29    return Rotulos
30 
31 def iaClassifier_wes(feats_train, labels_train, feats_test, fdist=euclidean):
32     adj = distance.squareform(distance.pdist(feats_train, fdist))
33     D = iaFit(adj, labels_train)
34     Rotulos = iaPredict(feats_test, feats_train, D, labels_train)
35     return Rotulos

Testes

 1 from numpy import *
 2 from scipy.spatial import distance
 3 from scikits.learn import datasets, metrics
 4 from iafitpredict_wes import *
 5 from iaOPF import *
 6 
 7 def load_digits():
 8     from numpy import arange, random
 9 
10     digits = datasets.load_digits(n_class=6)
11     feats = digits.data
12     labels = digits.target
13     random.seed([0])
14     rnd = random.permutation(arange(labels.shape[0]))
15     return feats[rnd], labels[rnd]
16 
17 ntrain = 0.5 # porcentagem dos dados utilizados no treinamento
18 
19 feats, labels = load_digits()
20 digi_feats_train = feats[:feats.shape[0]*ntrain,:]
21 digi_labels_train = labels[:feats.shape[0]*ntrain]
22 digi_feats_test = feats[feats.shape[0]*(ntrain)::,:]
23 digi_labels_test = labels[feats.shape[0]*(ntrain)::]
24 
25 Rotulos = iaClassifier_wes(digi_feats_train, digi_labels_train, digi_feats_test)
26 
27 print iaconfmtx(digi_labels_test, Rotulos)
[[ 84.   0.   0.   0.   0.   0.]
 [  0.  95.   0.   0.   0.   0.]
 [  0.   0.  95.   1.   0.   0.]
 [  0.   0.   1.  86.   0.   0.]
 [  0.   3.   0.   0.  87.   0.]
 [  0.   0.   0.   0.   0.  90.]]

Performance Test

gravando arquivo: /home/rubens/www/media/Attachments/iaOPF/iafitpredict_wes/testperf.pkl

Autor Funcao Iris   Digits  
Wesley iafitpredict_wes 65.926 ms 0.973333333333 9572.779 ms 0.990774907749

References

[Feof2009]Paulo Feofiloff. Algoritmos para Grafos em C via Sedgewick. IME-USP. http://www.ime.usp.br/~pf/algoritmos_para_grafos

Contributions

  • Wesley Souza, 1o semestre de 2012.