# Synopse

This function performs the OPF supervised classification algorithm.

• OPF_test = iafitpredict_fer(feats_test,feats_train, labels_train)

• Output

• OPF_test: Vector with the labels for the classified test samples.
• Input

• feats_test: NArray. Matrix with the test samples and its features
• feats_train: NArray. Matrix with the training samples and its features
• labels_train: Array. Vector with the labels of the training samples.

# Description

Implementation of the functions fit and predict for the OPF classificator

# Function Code

``` 1 from numpy import *
2 from scipy.spatial import distance
3 from iamstk_fer import iamstk_fer
4 from courseIA368Q1S2012.fer_lib import dijkstraMSourceOPF,Matrix2List
5
6 def myEuclidean(M, v):
7     E = M - ones((M.shape[0],1))*v
8
9     return sqrt(diagonal(dot(E, E.transpose())))
10
11 # encontra prototipos da OPF
12 def findSeeds(MST, L):
13
14     A = Matrix2List(MST,True)
15     S = zeros((MST.shape[0],), dtype=int32)
16
17     for i in A:
18         if(L[i[0]] != L[i[1]]):
19             S[i[0]] = 1
20             S[i[1]] = 1
21
22     return where(S==1)[0]
23
24
25 def iafit_fer(feats, labels):
26
27     M = distance.squareform(distance.pdist(feats, 'euclidean'))
28     MST = iamstk_fer(M)
29
30     S = findSeeds(MST, labels)
31
32     [C, parents] = dijkstraMSourceOPF(MST,S)
33
34     OPF_label = zeros((M.shape[0],), dtype=int32)
35     OPF_label = labels[parents]
36
37     return C, OPF_label
38
39
40 def iapredict_fer(feats_train, feats_test, OPF_costs, OPF_labels):
41
42     R = zeros((feats_test.shape[0],))+Inf
43
44     for i in range(feats_test.shape[0]):
45         dist = myEuclidean(feats_train, feats_test[i,:])
46         p = argmin(maximum(dist,OPF_costs))
47         R[i] = OPF_labels[p]
48
49     return R
50
51
52 def iafitpredict_fer(feats_test,feats_train, labels_train):
53     [OPF_costs, OPF_labels] = iafit_fer(feats_train, labels_train)
54     OPF_test = iapredict_fer(feats_train, feats_test, OPF_costs, OPF_labels)
55     return OPF_test
```

## Example 1:

```Confusion Matrix
[[ 25.   0.   0.]
[  0.  25.   3.]
[  0.   2.  20.]]
```

## Performance Test

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

Autor Funcao Iris   Digits
Fernanda iafitpredict_fer 248.718 ms 0.96 27617.909 ms 0.992619926199

# Contributions

• Fernanda Brandão Silva, 1o semestre de 2012.