Demo iafit

Description

Train the Optimum-Path Classifier.

Load Data and metric type

 1 from numpy import *
 2 from iaOPF import *
 3 
 4 from scikits.learn import datasets
 5 
 6 def load_iris():
 7     iris = datasets.load_iris()
 8     labels = iris.target
 9     feats = iris['data']
10     return feats, labels
11 
12 def euclidian(X, Y):
13     E = X - Y
14     return sqrt(dot(E, E))
15 
16 feats, labels = load_iris()
17 feats = array(feats)
18 labels = array(labels)
19 labels = labels.reshape(150,1)
20 
21 data = concatenate((feats,labels), axis=1)
22 data = data[0:100,:]
23 nsamples =5;
24 data = random.permutation(data)
25 data = data[0:nsamples,:]
26 #print data
27 feats = data[:,0:4]
28 labels = data[:,4]
29 labels = labels.reshape(nsamples,1)
30 labels = labels.astype('int')

Calculate the adjacente matrix using euclidian distance

1 from scipy.spatial import distance
2 from iaOPF import iaprim, iamstseeds, iadijkstra
3 from numpy import array
4 
5 A = distance.squareform(distance.pdist(feats,euclidian))
6 mmgraphviz(iaadjmxtcreate(A,dist=True), title='Input adjacent matrix')
/media/_xsb/iaOPF/demo_iafit/GRVIZ55465_001.png

Input adjacent matrix

Calculate the MST

1 B = iaprim(A)
2 mmgraphviz(iaadjmxtcreate(B), title='MST')
3 mmgraphviz(iaadjmxtcreate(A,labels,True,False,True), title='General Visualization')
/media/_xsb/iaOPF/demo_iafit/GRVIZ55465_002.png

MST

/media/_xsb/iaOPF/demo_iafit/GRVIZ55465_003.png

General Visualization

Calculate the MST seeds

1 P = iamstseeds(B, labels)
2 print 'seeds'
3 print P
4 mmgraphviz(iaadjmxtcreate(A,labels,False,True,True,True), title='Seeds visualization')
seeds
[1, 2]
/media/_xsb/iaOPF/demo_iafit/GRVIZ55465_004.png

Seeds visualization

Dissipate the seeds labels

1 seeds_labels = labels[P]
2 OPF = iadijkstra(A, P, seeds_labels)

Check results

1 print 'costs to root: ', OPF[0]
2 print 'parents: ', OPF[1]
3 print 'labels: ', OPF[2]
4 
5 #
costs to root:  [ 0.81853528  0.          0.          0.9486833   0.3       ]
parents:  [ 4.  1.  2.  2.  1.]
labels:  [ 0.  0.  1.  1.  0.]

Tie-Zone test

Test- Tie Zone