# Description

Predict using the Optimum-Path Classifier.

# Synopse

• label = iapredict(feats, path, labels, fdist)
• feats: Feature matrix.
• labels: Labels.
• fdist: Distance function beetween feature vectors.
``` 1 def iapredict(feat_predict, \
2               feat_train, pcost_train, labels_train, fdist):
3
4     from numpy import zeros
5
6     label = zeros(feat_predict.shape[0])
7
8     for i in xrange(feat_predict.shape[0]):
9         c_label = -1
10         minCost = float("inf")
11
12         for j in xrange(feat_train.shape[0]):
13             weight = fdist(feat_train[j],
14                            feat_predict[i])
15
16             tmp = max(pcost_train[j], weight)
17             if (tmp < minCost):
18                 minCost = tmp
19                 c_label = labels_train[j]
20
21         label[i] = c_label
22
23     return label
```
``` 1 def iapredict2(feat_predict, \
2               feat_train, pcost_train, labels_train, fdist):
3
4     from numpy import zeros
5
6     label = zeros(feat_predict.shape[0])
7     cost = zeros(feat_predict.shape[0])
8
9     for i in xrange(feat_predict.shape[0]):
10         c_label = -1
11         minCost = float("inf")
12
13         for j in xrange(feat_train.shape[0]):
14             weight = fdist(feat_train[j],
15                            feat_predict[i])
16
17             tmp = max(pcost_train[j], weight)
18             if (tmp < minCost):
19                 minCost = tmp
20                 c_label = labels_train[j]
21
22         label[i] = c_label
23         cost[i] = minCost
24
25     return label, cost
```

# Examples

``` 1 from numpy import *
2 from iaOPF import *
3
4 from iaOPF import iafit, iapredict,iaconfmtx
5
6 from scikits.learn import datasets, metrics
7 from scikits.learn.cross_val import StratifiedKFold
8
9 from libopf_py import OPF
10
13     labels = iris.target
14     feats = iris['data']
15
16     rnd = random.permutation(arange(labels.shape[0]))
17     return feats[rnd], labels[rnd]
18
20     lfw_people = datasets.fetch_lfw_people(min_faces_per_person=70, resize=0.4)
21     k = lfw_people.data
22     feats = k.reshape((k.shape[0], -1)).astype(float64)
23     labels = lfw_people.target.astype(int32)
24     rnd = random.permutation(arange(labels.shape[0]))
25     return feats[rnd], labels[rnd]
26
27 def euclidian(X, Y):
28     E = X - Y
29     return dot(E, E)
30
31 def run(feats, labels):
32     for i, (train, test) in\
33         enumerate(StratifiedKFold(labels, 5)):
34
35         opf_pc, _, opf_labels = iafit(feats[train],
36                                       labels[train],
37                                       euclidian)
38
39         predicted = iapredict(feats[test], feats[train],
40                               opf_pc, opf_labels, euclidian)
41
42         print "iaopf", metrics.f1_score(labels[test], predicted)
43         print iaconfmtx(labels[test], predicted)
44
45
46         opf_pc, _, opf_labels = iafit(feats[train],
47                                       labels[train],
48                                       euclidian,True)
49
50         predicted = iapredict(feats[test], feats[train],
51                               opf_pc, opf_labels, euclidian)
52
53         print "iaopf mst", metrics.f1_score(labels[test], predicted)
54         print iaconfmtx(labels[test], predicted)
55
56         O = OPF()
57         O.fit(feats[train], labels[train].astype(int32))
58         predicted = O.predict(feats[test])
59
60         print "libopf3", metrics.f1_score(labels[test].astype(int32), predicted)
61         print iaconfmtx(labels[test], predicted)
62
63 print "== PETALS =="
65 run(feats, labels)
66 print "== FACES =="
68 run(feats, labels)
```
```== PETALS ==
iaopf 1.0
[[ 10.   0.   0.]
[  0.  10.   0.]
[  0.   0.  10.]]
iaopf mst 1.0
[[ 10.   0.   0.]
[  0.  10.   0.]
[  0.   0.  10.]]
libopf3 1.0
[[ 10.   0.   0.]
[  0.  10.   0.]
[  0.   0.  10.]]
iaopf 0.933333333333
[[ 10.   0.   0.]
[  0.   9.   1.]
[  0.   1.   9.]]
iaopf mst 0.933333333333
[[ 10.   0.   0.]
[  0.   9.   1.]
[  0.   1.   9.]]
libopf3 0.933333333333
[[ 10.   0.   0.]
[  0.   9.   1.]
[  0.   1.   9.]]
iaopf 1.0
[[ 10.   0.   0.]
[  0.  10.   0.]
[  0.   0.  10.]]
iaopf mst 1.0
[[ 10.   0.   0.]
[  0.  10.   0.]
[  0.   0.  10.]]
libopf3 1.0
[[ 10.   0.   0.]
[  0.  10.   0.]
[  0.   0.  10.]]
iaopf 0.933333333333
[[ 10.   0.   0.]
[  0.   9.   1.]
[  0.   1.   9.]]
iaopf mst 0.933333333333
[[ 10.   0.   0.]
[  0.   9.   1.]
[  0.   1.   9.]]
libopf3 0.933333333333
[[ 10.   0.   0.]
[  0.   9.   1.]
[  0.   1.   9.]]
iaopf 0.933333333333
[[ 10.   0.   0.]
[  0.   9.   1.]
[  0.   1.   9.]]
iaopf mst 0.933333333333
[[ 10.   0.   0.]
[  0.   9.   1.]
[  0.   1.   9.]]
libopf3 0.933333333333
[[ 10.   0.   0.]
[  0.   9.   1.]
[  0.   1.   9.]]
== FACES ==
iaopf 0.540755418487
[[  5.   3.   1.   6.   1.   0.   0.]
[  3.  28.   2.   9.   0.   0.   5.]
[  1.   4.   8.   7.   2.   0.   2.]
[  0.   3.   6.  87.   0.   1.   9.]
[  0.   2.   1.   8.   4.   0.   7.]
[  0.   2.   1.   6.   2.   2.   1.]
[  0.   2.   1.   9.   5.   1.  11.]]
iaopf mst 0.540755418487
[[  5.   3.   1.   6.   1.   0.   0.]
[  3.  28.   2.   9.   0.   0.   5.]
[  1.   4.   8.   7.   2.   0.   2.]
[  0.   3.   6.  87.   0.   1.   9.]
[  0.   2.   1.   8.   4.   0.   7.]
[  0.   2.   1.   6.   2.   2.   1.]
[  0.   2.   1.   9.   5.   1.  11.]]
libopf3 0.540755418487
[[  5.   3.   1.   6.   1.   0.   0.]
[  3.  28.   2.   9.   0.   0.   5.]
[  1.   4.   8.   7.   2.   0.   2.]
[  0.   3.   6.  87.   0.   1.   9.]
[  0.   2.   1.   8.   4.   0.   7.]
[  0.   2.   1.   6.   2.   2.   1.]
[  0.   2.   1.   9.   5.   1.  11.]]
iaopf 0.562969923884
[[  6.   2.   1.   2.   2.   0.   3.]
[  0.  27.   6.   6.   2.   3.   3.]
[  0.   4.  12.   4.   2.   0.   2.]
[  2.   7.   8.  82.   1.   3.   3.]
[  1.   0.   0.   8.   5.   3.   5.]
[  0.   2.   0.   4.   1.   5.   2.]
[  0.   2.   1.  11.   4.   1.  10.]]
iaopf mst 0.562969923884
[[  6.   2.   1.   2.   2.   0.   3.]
[  0.  27.   6.   6.   2.   3.   3.]
[  0.   4.  12.   4.   2.   0.   2.]
[  2.   7.   8.  82.   1.   3.   3.]
[  1.   0.   0.   8.   5.   3.   5.]
[  0.   2.   0.   4.   1.   5.   2.]
[  0.   2.   1.  11.   4.   1.  10.]]
libopf3 0.562969923884
[[  6.   2.   1.   2.   2.   0.   3.]
[  0.  27.   6.   6.   2.   3.   3.]
[  0.   4.  12.   4.   2.   0.   2.]
[  2.   7.   8.  82.   1.   3.   3.]
[  1.   0.   0.   8.   5.   3.   5.]
[  0.   2.   0.   4.   1.   5.   2.]
[  0.   2.   1.  11.   4.   1.  10.]]
iaopf 0.545079161524
[[  5.   5.   1.   3.   0.   1.   0.]
[  1.  30.   4.   9.   1.   0.   3.]
[  3.   2.   8.   8.   0.   3.   0.]
[  5.  10.   4.  70.   3.   3.  11.]
[  0.   0.   2.   8.   6.   1.   5.]
[  0.   0.   1.   3.   0.   8.   2.]
[  0.   1.   1.  11.   1.   0.  15.]]
iaopf mst 0.545079161524
[[  5.   5.   1.   3.   0.   1.   0.]
[  1.  30.   4.   9.   1.   0.   3.]
[  3.   2.   8.   8.   0.   3.   0.]
[  5.  10.   4.  70.   3.   3.  11.]
[  0.   0.   2.   8.   6.   1.   5.]
[  0.   0.   1.   3.   0.   8.   2.]
[  0.   1.   1.  11.   1.   0.  15.]]
libopf3 0.545079161524
[[  5.   5.   1.   3.   0.   1.   0.]
[  1.  30.   4.   9.   1.   0.   3.]
[  3.   2.   8.   8.   0.   3.   0.]
[  5.  10.   4.  70.   3.   3.  11.]
[  0.   0.   2.   8.   6.   1.   5.]
[  0.   0.   1.   3.   0.   8.   2.]
[  0.   1.   1.  11.   1.   0.  15.]]
iaopf 0.598125491561
[[  5.   6.   0.   2.   0.   0.   2.]
[  4.  25.   4.   7.   2.   2.   3.]
[  0.   4.  15.   4.   1.   0.   1.]
[  2.   7.   4.  78.   3.   4.   8.]
[  0.   2.   2.   5.   8.   1.   3.]
[  0.   1.   0.   2.   1.   9.   2.]
[  3.   1.   0.   9.   1.   0.  14.]]
iaopf mst 0.598125491561
[[  5.   6.   0.   2.   0.   0.   2.]
[  4.  25.   4.   7.   2.   2.   3.]
[  0.   4.  15.   4.   1.   0.   1.]
[  2.   7.   4.  78.   3.   4.   8.]
[  0.   2.   2.   5.   8.   1.   3.]
[  0.   1.   0.   2.   1.   9.   2.]
[  3.   1.   0.   9.   1.   0.  14.]]
libopf3 0.598125491561
[[  5.   6.   0.   2.   0.   0.   2.]
[  4.  25.   4.   7.   2.   2.   3.]
[  0.   4.  15.   4.   1.   0.   1.]
[  2.   7.   4.  78.   3.   4.   8.]
[  0.   2.   2.   5.   8.   1.   3.]
[  0.   1.   0.   2.   1.   9.   2.]
[  3.   1.   0.   9.   1.   0.  14.]]
iaopf 0.533168178349
[[  3.   4.   2.   4.   0.   0.   2.]
[  4.  28.   4.   8.   0.   0.   3.]
[  3.   3.  10.   7.   0.   0.   1.]
[  1.  12.   7.  66.   5.   5.  10.]
[  2.   2.   1.   3.   9.   1.   4.]
[  0.   1.   1.   5.   0.   7.   0.]
[  0.   0.   1.   9.   5.   0.  14.]]
iaopf mst 0.533168178349
[[  3.   4.   2.   4.   0.   0.   2.]
[  4.  28.   4.   8.   0.   0.   3.]
[  3.   3.  10.   7.   0.   0.   1.]
[  1.  12.   7.  66.   5.   5.  10.]
[  2.   2.   1.   3.   9.   1.   4.]
[  0.   1.   1.   5.   0.   7.   0.]
[  0.   0.   1.   9.   5.   0.  14.]]
libopf3 0.533168178349
[[  3.   4.   2.   4.   0.   0.   2.]
[  4.  28.   4.   8.   0.   0.   3.]
[  3.   3.  10.   7.   0.   0.   1.]
[  1.  12.   7.  66.   5.   5.  10.]
[  2.   2.   1.   3.   9.   1.   4.]
[  0.   1.   1.   5.   0.   7.   0.]
[  0.   0.   1.   9.   5.   0.  14.]]
```