Function iafitpredict_rob

Synopse

This function performs the OPF supervised classification algorithm.

  • OPF_test = iaCLASSIFY_rob(test_set,train_set, train_labels)

    • Output

      • OPF_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.

Description

The set of functions displayed below implement the functions necessary to perform the OPF supervised classification algorithm (Fit and Predict steps). It is also implemented the auxiliary functions such as findPrototypes and vectorEuclidian.

Function Code

 1 from numpy import*
 2 from sklearn import datasets, metrics
 3 from scipy.spatial import distance
 4 from iaOPF import*
 5 
 6 
 7 def vectorEuclidian(train_set,test_vector):
 8 
 9    d = zeros(len(train_set[:,0]))
10    temp = (train_set - test_vector)
11 
12    for i in range(len(train_set[:,0])):
13       d[i] = sqrt(dot(temp[i,:],temp[i,:]))
14 
15    return d
16 
17 def findPrototypes(MST,labels):
18 
19    prototypes = []
20 
21    for i in range(MST.shape[0]):
22       neighbors = where(MST[i,:]!=float('inf'))[0]
23 
24       if any(labels[neighbors]!=labels[i]):
25          prototypes.append(i)
26          prototypes.extend(list(neighbors[labels[neighbors]!=labels[i]]))
27 
28 
29 
30       MST[i,:] = float('inf')
31       MST[:,i] = float('inf')
32    return list(set(prototypes))
33 
34 def euclidian(X, Y):
35    E = X - Y
36    return sqrt(dot(E, E))
37 
38 
39 def iaFIT_rob(train_set,labels):
40 
41    graph = distance.squareform(distance.pdist(train_set, euclidian))
42    temp = range(len(graph[0,:]))
43    graph[temp,temp] = float('inf')
44 
45 
46    MST = iamstk_rob(graph)
47    prototypes = findPrototypes(MST,labels)
48    OPF_train = ones((3,len(graph[0])))
49    OPF_train[0:2,:] = iadijkstra_rob(graph,array(prototypes))
50    OPF_train[2,:] = labels[OPF_train[0,:].astype(int)]
51 
52    return OPF_train
53 
54 
55 def iaPREDICT_rob(OPF_train,test_set,train_set):
56 
57    test_set_length = len(test_set[:,0])
58    OPF_test  = zeros(test_set_length)
59    test_set_labels= zeros(test_set_length)
60 
61    for i in range(test_set_length):
62       distances_vector = vectorEuclidian(train_set,test_set[i,:])
63       index = argmin(maximum(distances_vector,OPF_train[1,:]))
64       OPF_test[i]=OPF_train[2,index]
65 
66    return OPF_test
67 
68 
69 def iaCLASSIFY_rob(test_set,train_set, train_labels):
70     OPF_train = iaFIT_rob(train_set,train_labels)
71     OPF_test = iaPREDICT_rob(OPF_train,test_set,train_set)
72     return OPF_test

Example 1:

 1 from numpy import*
 2 from courseIA368Q1S2012.rob_biblio import*
 3 from iafitpredict_rob import*
 4 import courseIA368Q1S2012.Ex09 as ex
 5 import time
 6 import iaOPF as ia
 7 
 8 t1=time.time()
 9 OPF_train = iaFIT_rob(ex.iris_feats_train,array(ex.iris_labels_train))
10 OPF_test = iaPREDICT_rob(OPF_train,ex.iris_feats_test,ex.iris_feats_train).astype(int)
11 t2=time.time()
12 
13 print 'Confusion Matrix found by my OPF'
14 cm = ia.iaconfmtx(ex.iris_labels_test,OPF_test)
15 print cm
16 print 'My OPF execution time'
17 print t2-t1
18 print 'Accuracy = ', iaaccuCalculation(cm)
ERROR execute

------------------------------------------------------------
*** Exception while evaluating code:
  File "<string>", line 2, in <module>
  File "/awmedia/www/packages/courseIA368Q1S2012/rob_biblio.py", line 2, in <module>
    from rob_iaplotdecisionboundaries import*
  File "/awmedia/www/packages/courseIA368Q1S2012/rob_iaplotdecisionboundaries.py", line 3, in <module>
    from rob_biblio2 import*
  File "/awmedia/www/packages/courseIA368Q1S2012/rob_biblio2.py", line 12, in <module>
    from sklearn import NeighborsClassifier
ImportError: cannot import name NeighborsClassifier

------------------------------------------------------------

Performance Test

ERROR execute

------------------------------------------------------------
*** Exception while evaluating code:
  File "<string>", line 1, in <module>
  File "/awmedia/www/packages/iaOPF/iafitpred_test.py", line 10, in <module>
    import courseIA368Q1S2012.Ex09 as ex
  File "/awmedia/www/packages/courseIA368Q1S2012/Ex09.py", line 3, in <module>
    from scikits.learn import datasets, metrics
ImportError: No module named learn

------------------------------------------------------------

ERROR execute

------------------------------------------------------------
*** Exception while evaluating code:
  File "<string>", line 1, in <module>
NameError: name 'objTestes' is not defined

------------------------------------------------------------

References

See also

Contributions

  • Roberto Souza, 1o semestre de 2012.

References

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