Training(1)

and write(save) the tree to pickle


In [101]:
import pickle
from pforest.master import master
m=master(dsetname='CS213')
m.reset()
m.train()
with open('CS213/out_tree1.pic', 'wb') as pickleFile:
    pickle.dump(m.root, pickleFile, pickle.HIGHEST_PROTOCOL)


master>>init() dsetname: CS213
master>> create dview
master>> init engine
Found pforest
debug:master:__init__: ['CS213/dataset00.pic', 'CS213/dataset01.pic', 'CS213/dataset02.pic', 'CS213/dataset03.pic']
master>> init local variables
master>>reset()
master>>reset() H: 2.8257
master>>reset() Q: 00612
master::train() node20*-
master::train() node19R-
master::train() node18R-
master::train() node17RG
master::train() node17LG
master::train() node18L-
master::train() node17R-
master::train() node16RG
master::train() node16L-
master::train() node15RG
master::train() node15L-
master::train() node14R-
master::train() node13R-
master::train() node12R-
master::train() node11RG
master::train() node11L-
master::train() node10R-
master::train() node9RG
master::train() node9L-
master::train() node8RG
master::train() node8LQ
master::train() node10LG
master::train() node12LQ
master::train() node13LQ
master::train() node14LG
master::train() node17L-
master::train() node16R-
master::train() node15RQ
master::train() node15LG
master::train() node16L-
master::train() node15R-
master::train() node14RG
master::train() node14L-
master::train() node13RG
master::train() node13L-
master::train() node12RG
master::train() node12LG
master::train() node15L-
master::train() node14RG
master::train() node14L-
master::train() node13RG
master::train() node13L-
master::train() node12RG
master::train() node12L-
master::train() node11R-
master::train() node10R-
master::train() node9RG
master::train() node9L-
master::train() node8RG
master::train() node8L-
master::train() node7RG
master::train() node7L-
master::train() node6R-
master::train() node5RG
master::train() node5LG
master::train() node6L-
master::train() node5RG
master::train() node5L-
master::train() node4RG
master::train() node4LG
master::train() node10L-
master::train() node9RG
master::train() node9L-
master::train() node8RQ
master::train() node8LG
master::train() node11L-
master::train() node10RG
master::train() node10LG
master::train() node19L-
master::train() node18R-
master::train() node17R-
master::train() node16R-
master::train() node15R-
master::train() node14RG
master::train() node14L-
master::train() node13R-
master::train() node12RG
master::train() node12LQ
master::train() node13LG
master::train() node15L-
master::train() node14RG
master::train() node14LG
master::train() node16L-
master::train() node15RG
master::train() node15LQ
master::train() node17L-
master::train() node16R-
master::train() node15RG
master::train() node15L-
master::train() node14R-
master::train() node13R-
master::train() node12RG
master::train() node12LG
master::train() node13L-
master::train() node12RG
master::train() node12L-
master::train() node11RG
master::train() node11LQ
master::train() node14L-
master::train() node13RG
master::train() node13LG
master::train() node16LG
master::train() node18L-
master::train() node17R-
master::train() node16RG
master::train() node16L-
master::train() node15RG
master::train() node15L-
master::train() node14RG
master::train() node14LG
master::train() node17L-
master::train() node16R-
master::train() node15R-
master::train() node14R-
master::train() node13R-
master::train() node12R-
master::train() node11RG
master::train() node11LG
master::train() node12LG
master::train() node13L-
master::train() node12R-
master::train() node11RG
master::train() node11L-
master::train() node10RG
master::train() node10LG
master::train() node12L-
master::train() node11RG
master::train() node11LG
master::train() node14LG
master::train() node15L-
master::train() node14R-
master::train() node13R-
master::train() node12R-
master::train() node11R-
master::train() node10RQ
master::train() node10L-
master::train() node9RG
master::train() node9LQ
master::train() node11L-
master::train() node10RG
master::train() node10LG
master::train() node12LG
master::train() node13L-
master::train() node12R-
master::train() node11RG
master::train() node11LQ
master::train() node12LQ
master::train() node14LG
master::train() node16L-
master::train() node15R-
master::train() node14RG
master::train() node14LG
master::train() node15L-
master::train() node14R-
master::train() node13RG
master::train() node13L-
master::train() node12RG
master::train() node12L-
master::train() node11RQ
master::train() node11LG
master::train() node14L-
master::train() node13RG
master::train() node13L-
master::train() node12R-
master::train() node11RG
master::train() node11L-
master::train() node10RG
master::train() node10L-
master::train() node9RG
master::train() node9L-
master::train() node8RQ
master::train() node8LG
master::train() node12LG

Read the tree

The result decision tree


In [102]:
from pforest.dataset import dataset
from pforest.tree import tree
with open('CS213/out_tree1.pic', 'rb') as pickleFile:
    root = pickle.load(pickleFile)
#init the test tree
t=tree()
t.settree(root)
t.show()


*- 20 H:2.826e+00,Q:000612 tau:10.0 theta:[ 19.]
L- 19 H:2.660e+00,Q:000357 tau:6.0 theta:[ 25.]
L- 18 H:2.573e+00,Q:000258 tau:11.0 theta:[ 20.]
L- 17 H:2.433e+00,Q:000222 tau:8.0 theta:[ 22.]
L- 16 H:2.342e+00,Q:000089 tau:3.0 theta:[ 97.]
L- 15 H:2.169e+00,Q:000070 tau:11.0 theta:[ 189.]
L- 14 H:1.972e+00,Q:000048 tau:9.0 theta:[ 161.]
L- 13 H:1.994e+00,Q:000044 tau:9.0 theta:[ 196.]
LG 12 H:1.344e+00,Q:000008 (cl,P):(008,0.38) (011,0.25) (010,0.12)
R- 12 H:1.772e+00,Q:000036 tau:9.0 theta:[ 20.]
L- 11 H:1.488e+00,Q:000024 tau:12.0 theta:[ 179.]
L- 10 H:1.285e+00,Q:000019 tau:11.0 theta:[ 169.]
L- 09 H:1.275e+00,Q:000015 tau:7.0 theta:[ 125.]
LG 08 H:1.201e+00,Q:000013 (cl,P):(006,0.31) (008,0.23) (009,0.15)
RQ 08 H:1.925e-35,Q:000002 (cl,P):(009,0.50) (005,0.50) (013,0.00)
RG 09 H:5.000e-01,Q:000004 (cl,P):(009,0.25) (008,0.25) (007,0.25)
RG 10 H:1.371e+00,Q:000005 (cl,P):(009,0.60) (008,0.20) (007,0.20)
RG 11 H:1.471e+00,Q:000012 (cl,P):(007,0.42) (008,0.25) (011,0.08)
RG 13 H:6.887e-01,Q:000004 (cl,P):(005,0.50) (008,0.25) (006,0.25)
R- 14 H:1.789e+00,Q:000022 tau:8.0 theta:[ 130.]
L- 13 H:1.501e+00,Q:000018 tau:12.0 theta:[ 189.]
L- 12 H:1.000e+00,Q:000008 tau:11.0 theta:[ 160.]
LG 11 H:1.126e+00,Q:000006 (cl,P):(006,0.33) (010,0.17) (009,0.17)
RQ 11 H:1.925e-35,Q:000002 (cl,P):(006,1.00) (013,0.00) (012,0.00)
RG 12 H:1.436e+00,Q:000010 (cl,P):(007,0.30) (009,0.20) (008,0.20)
RG 13 H:6.887e-01,Q:000004 (cl,P):(008,0.75) (010,0.25) (013,0.00)
R- 15 H:1.803e+00,Q:000019 tau:11.0 theta:[ 174.]
LG 14 H:1.672e+00,Q:000016 (cl,P):(005,0.31) (007,0.25) (008,0.19)
RG 14 H:6.667e-01,Q:000003 (cl,P):(010,0.33) (008,0.33) (007,0.33)
R- 16 H:2.183e+00,Q:000133 tau:9.0 theta:[ 22.]
L- 15 H:1.902e+00,Q:000049 tau:8.0 theta:[ 20.]
LG 14 H:9.183e-01,Q:000006 (cl,P):(008,0.33) (007,0.33) (006,0.33)
R- 14 H:1.844e+00,Q:000043 tau:8.0 theta:[ 160.]
L- 13 H:1.351e+00,Q:000016 tau:5.0 theta:[ 125.]
LQ 12 H:1.925e-35,Q:000001 (cl,P):(007,1.00) (013,0.00) (012,0.00)
R- 12 H:1.367e+00,Q:000015 tau:9.0 theta:[ 162.]
LQ 11 H:1.925e-35,Q:000004 (cl,P):(008,0.50) (007,0.25) (006,0.25)
RG 11 H:1.341e+00,Q:000011 (cl,P):(009,0.36) (007,0.36) (006,0.18)
R- 13 H:1.649e+00,Q:000027 tau:9.0 theta:[ 19.]
LG 12 H:6.793e-01,Q:000007 (cl,P):(009,0.29) (008,0.29) (007,0.29)
R- 12 H:1.548e+00,Q:000020 tau:12.0 theta:[ 196.]
L- 11 H:1.501e+00,Q:000009 tau:12.0 theta:[ 178.]
LG 10 H:5.510e-01,Q:000005 (cl,P):(008,0.40) (007,0.40) (009,0.20)
RG 10 H:1.189e+00,Q:000004 (cl,P):(009,0.25) (008,0.25) (007,0.25)
R- 11 H:1.204e+00,Q:000011 tau:9.0 theta:[ 130.]
L- 10 H:1.000e+00,Q:000010 tau:10.0 theta:[ 162.]
LQ 09 H:1.925e-35,Q:000002 (cl,P):(009,0.50) (008,0.50) (013,0.00)
RG 09 H:9.387e-01,Q:000008 (cl,P):(009,0.38) (008,0.38) (007,0.12)
RQ 10 H:1.925e-35,Q:000001 (cl,P):(009,1.00) (013,0.00) (012,0.00)
R- 15 H:2.011e+00,Q:000084 tau:9.0 theta:[ 162.]
LG 14 H:4.591e-01,Q:000006 (cl,P):(009,0.50) (008,0.50) (013,0.00)
R- 14 H:2.022e+00,Q:000078 tau:10.0 theta:[ 99.]
L- 13 H:1.718e+00,Q:000044 tau:7.0 theta:[ 191.]
L- 12 H:1.097e+00,Q:000013 tau:9.0 theta:[ 98.]
LG 11 H:4.591e-01,Q:000006 (cl,P):(009,0.67) (008,0.17) (005,0.17)
RG 11 H:6.793e-01,Q:000007 (cl,P):(008,0.57) (011,0.14) (009,0.14)
R- 12 H:1.621e+00,Q:000031 tau:12.0 theta:[ 98.]
L- 11 H:1.414e+00,Q:000027 tau:10.0 theta:[ 154.]
LG 10 H:1.200e+00,Q:000015 (cl,P):(009,0.60) (008,0.20) (007,0.13)
RG 10 H:1.116e+00,Q:000012 (cl,P):(009,0.50) (008,0.25) (007,0.17)
RG 11 H:1.189e+00,Q:000004 (cl,P):(010,0.50) (009,0.25) (005,0.25)
R- 13 H:1.848e+00,Q:000034 tau:7.0 theta:[ 125.]
LG 12 H:1.404e+00,Q:000017 (cl,P):(008,0.47) (009,0.24) (007,0.18)
R- 12 H:1.765e+00,Q:000017 tau:12.0 theta:[ 196.]
LG 11 H:1.638e+00,Q:000012 (cl,P):(008,0.33) (010,0.25) (005,0.17)
RG 11 H:7.763e-36,Q:000005 (cl,P):(009,0.80) (011,0.20) (013,0.00)
R- 17 H:1.855e+00,Q:000036 tau:8.0 theta:[ 191.]
L- 16 H:1.305e+00,Q:000019 tau:8.0 theta:[ 126.]
L- 15 H:9.182e-01,Q:000014 tau:12.0 theta:[ 20.]
LG 14 H:5.714e-01,Q:000007 (cl,P):(009,0.71) (010,0.14) (007,0.14)
RG 14 H:3.936e-01,Q:000007 (cl,P):(011,0.29) (010,0.29) (009,0.29)
RG 15 H:5.510e-01,Q:000005 (cl,P):(010,0.60) (011,0.20) (008,0.20)
RG 16 H:1.646e+00,Q:000017 (cl,P):(011,0.29) (010,0.29) (009,0.12)
R- 18 H:2.317e+00,Q:000099 tau:7.0 theta:[ 47.]
L- 17 H:2.044e+00,Q:000048 tau:8.0 theta:[ 34.]
LG 16 H:1.361e+00,Q:000010 (cl,P):(006,0.30) (009,0.20) (008,0.20)
R- 16 H:1.834e+00,Q:000038 tau:11.0 theta:[ 162.]
L- 15 H:1.845e+00,Q:000031 tau:9.0 theta:[ 35.]
L- 14 H:9.610e-01,Q:000010 tau:9.0 theta:[ 162.]
LG 13 H:6.667e-01,Q:000003 (cl,P):(009,0.67) (010,0.33) (013,0.00)
RG 13 H:9.650e-01,Q:000007 (cl,P):(010,0.29) (009,0.29) (008,0.29)
R- 14 H:1.701e+00,Q:000021 tau:7.0 theta:[ 58.]
L- 13 H:1.286e+00,Q:000014 tau:12.0 theta:[ 103.]
L- 12 H:1.046e+00,Q:000011 tau:9.0 theta:[ 199.]
LQ 11 H:1.925e-35,Q:000002 (cl,P):(010,0.50) (007,0.50) (013,0.00)
RG 11 H:8.455e-01,Q:000009 (cl,P):(010,0.33) (009,0.33) (006,0.22)
RG 12 H:6.667e-01,Q:000003 (cl,P):(010,0.33) (009,0.33) (008,0.33)
R- 13 H:8.014e-01,Q:000007 tau:10.0 theta:[ 39.]
LG 12 H:1.000e+00,Q:000002 (cl,P):(010,0.50) (009,0.50) (013,0.00)
RG 12 H:4.000e-01,Q:000005 (cl,P):(009,0.80) (008,0.20) (013,0.00)
RG 15 H:8.571e-01,Q:000007 (cl,P):(010,0.43) (008,0.43) (009,0.14)
R- 17 H:2.001e+00,Q:000051 tau:7.0 theta:[ 98.]
L- 16 H:6.887e-01,Q:000008 tau:6.0 theta:[ 99.]
LQ 15 H:1.925e-35,Q:000003 (cl,P):(013,0.33) (010,0.33) (008,0.33)
RG 15 H:4.000e-01,Q:000005 (cl,P):(009,0.40) (011,0.20) (008,0.20)
R- 16 H:1.826e+00,Q:000043 tau:9.0 theta:[ 50.]
L- 15 H:9.610e-01,Q:000010 tau:7.0 theta:[ 201.]
LG 14 H:5.510e-01,Q:000005 (cl,P):(010,0.40) (008,0.40) (009,0.20)
RG 14 H:1.161e-35,Q:000005 (cl,P):(009,1.00) (013,0.00) (012,0.00)
R- 15 H:1.624e+00,Q:000033 tau:9.0 theta:[ 201.]
L- 14 H:1.386e+00,Q:000024 tau:9.0 theta:[ 160.]
LG 13 H:1.367e+00,Q:000012 (cl,P):(010,0.33) (009,0.33) (011,0.25)
R- 13 H:9.406e-01,Q:000012 tau:10.0 theta:[ 31.]
LQ 12 H:1.925e-35,Q:000003 (cl,P):(009,0.67) (010,0.33) (013,0.00)
RG 12 H:1.141e+00,Q:000009 (cl,P):(010,0.56) (009,0.33) (012,0.11)
RG 14 H:9.839e-01,Q:000009 (cl,P):(011,0.56) (012,0.22) (010,0.11)
R- 19 H:2.592e+00,Q:000255 tau:13.0 theta:[ 20.]
L- 18 H:2.508e+00,Q:000222 tau:10.0 theta:[ 125.]
L- 17 H:2.360e+00,Q:000169 tau:9.0 theta:[ 130.]
L- 16 H:2.306e+00,Q:000160 tau:10.0 theta:[ 22.]
L- 15 H:2.109e+00,Q:000109 tau:11.0 theta:[ 32.]
L- 14 H:2.012e+00,Q:000102 tau:11.0 theta:[ 103.]
L- 13 H:1.892e+00,Q:000098 tau:9.0 theta:[ 126.]
L- 12 H:1.870e+00,Q:000091 tau:9.0 theta:[ 99.]
L- 11 H:1.600e+00,Q:000025 tau:10.0 theta:[ 162.]
LG 10 H:8.265e-01,Q:000010 (cl,P):(009,0.50) (008,0.30) (010,0.20)
RG 10 H:1.332e+00,Q:000015 (cl,P):(009,0.47) (006,0.20) (008,0.13)
R- 11 H:1.692e+00,Q:000066 tau:10.0 theta:[ 189.]
L- 10 H:1.362e+00,Q:000008 tau:8.0 theta:[ 191.]
L- 09 H:1.200e+00,Q:000005 tau:10.0 theta:[ 199.]
LG 08 H:1.189e+00,Q:000004 (cl,P):(007,0.50) (009,0.25) (008,0.25)
RQ 08 H:1.925e-35,Q:000001 (cl,P):(007,1.00) (013,0.00) (012,0.00)
RG 09 H:9.183e-01,Q:000003 (cl,P):(009,0.67) (008,0.33) (013,0.00)
R- 10 H:1.575e+00,Q:000058 tau:10.0 theta:[ 160.]
L- 09 H:1.523e+00,Q:000046 tau:11.0 theta:[ 19.]
L- 08 H:1.313e+00,Q:000032 tau:9.0 theta:[ 125.]
L- 07 H:1.316e+00,Q:000026 tau:12.0 theta:[ 196.]
L- 06 H:1.439e+00,Q:000016 tau:11.0 theta:[ 99.]
L- 05 H:1.237e+00,Q:000011 tau:10.0 theta:[ 20.]
LG 04 H:7.925e-01,Q:000006 (cl,P):(008,0.50) (009,0.33) (007,0.17)
RG 04 H:8.000e-01,Q:000005 (cl,P):(009,0.40) (011,0.20) (010,0.20)
RG 05 H:6.490e-01,Q:000005 (cl,P):(009,0.80) (008,0.20) (013,0.00)
R- 06 H:7.245e-01,Q:000010 tau:11.0 theta:[ 162.]
LG 05 H:5.000e-01,Q:000008 (cl,P):(009,0.75) (008,0.25) (013,0.00)
RG 05 H:1.000e+00,Q:000002 (cl,P):(009,0.50) (008,0.50) (013,0.00)
RG 07 H:7.925e-01,Q:000006 (cl,P):(009,0.67) (010,0.17) (008,0.17)
RG 08 H:1.115e+00,Q:000014 (cl,P):(008,0.36) (010,0.29) (009,0.21)
RG 09 H:1.138e+00,Q:000012 (cl,P):(009,0.67) (011,0.08) (010,0.08)
RG 12 H:6.793e-01,Q:000007 (cl,P):(009,0.43) (010,0.29) (013,0.14)
RG 13 H:6.887e-01,Q:000004 (cl,P):(012,0.50) (011,0.25) (010,0.25)
RG 14 H:7.871e-01,Q:000007 (cl,P):(011,0.57) (010,0.43) (013,0.00)
R- 15 H:1.757e+00,Q:000051 tau:9.0 theta:[ 160.]
L- 14 H:1.476e+00,Q:000024 tau:9.0 theta:[ 125.]
L- 13 H:1.413e+00,Q:000013 tau:8.0 theta:[ 160.]
LG 12 H:6.556e-01,Q:000008 (cl,P):(009,0.38) (011,0.25) (013,0.12)
RG 12 H:4.000e-01,Q:000005 (cl,P):(010,0.40) (011,0.20) (009,0.20)
RG 13 H:8.736e-01,Q:000011 (cl,P):(011,0.27) (010,0.27) (009,0.27)
RG 14 H:1.501e+00,Q:000027 (cl,P):(010,0.48) (009,0.30) (013,0.07)
R- 16 H:8.455e-01,Q:000009 tau:12.0 theta:[ 139.]
LG 15 H:9.056e-01,Q:000008 (cl,P):(011,0.50) (013,0.25) (012,0.12)
RQ 15 H:1.925e-35,Q:000001 (cl,P):(011,1.00) (013,0.00) (012,0.00)
R- 17 H:2.223e+00,Q:000053 tau:12.0 theta:[ 20.]
L- 16 H:1.980e+00,Q:000038 tau:11.0 theta:[ 196.]
L- 15 H:1.684e+00,Q:000023 tau:11.0 theta:[ 20.]
LG 14 H:9.650e-01,Q:000007 (cl,P):(009,0.57) (010,0.29) (011,0.14)
R- 14 H:1.657e+00,Q:000016 tau:7.0 theta:[ 98.]
LQ 13 H:1.925e-35,Q:000001 (cl,P):(013,1.00) (012,0.00) (011,0.00)
R- 13 H:1.684e+00,Q:000015 tau:9.0 theta:[ 99.]
LQ 12 H:1.925e-35,Q:000001 (cl,P):(009,1.00) (013,0.00) (012,0.00)
R- 12 H:1.608e+00,Q:000014 tau:12.0 theta:[ 98.]
L- 11 H:1.151e+00,Q:000010 tau:10.0 theta:[ 98.]
LG 10 H:9.704e-36,Q:000002 (cl,P):(010,1.00) (013,0.00) (012,0.00)
R- 10 H:1.094e+00,Q:000008 tau:11.0 theta:[ 125.]
L- 09 H:1.000e+00,Q:000004 tau:12.0 theta:[ 189.]
LQ 08 H:1.925e-35,Q:000001 (cl,P):(013,1.00) (012,0.00) (011,0.00)
RG 08 H:6.667e-01,Q:000003 (cl,P):(011,0.67) (010,0.33) (013,0.00)
RG 09 H:5.000e-01,Q:000004 (cl,P):(013,0.50) (011,0.25) (009,0.25)
RG 11 H:5.000e-01,Q:000004 (cl,P):(012,0.50) (009,0.50) (013,0.00)
RG 15 H:1.550e+00,Q:000015 (cl,P):(009,0.40) (010,0.27) (012,0.13)
RG 16 H:1.592e+00,Q:000015 (cl,P):(013,0.40) (012,0.27) (011,0.27)
R- 18 H:1.644e+00,Q:000033 tau:11.0 theta:[ 154.]
LG 17 H:1.500e+00,Q:000020 (cl,P):(013,0.40) (012,0.30) (010,0.15)
RG 17 H:8.477e-01,Q:000013 (cl,P):(013,0.46) (012,0.38) (011,0.15)

Test Classify(1)

zeros(no register) is training data


In [103]:
import numpy as np
u=np.zeros(203)
Prob=t.classify(u)

In [104]:
Prob


Out[104]:
array([ 0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.125,  0.   ,  0.125,
        0.375,  0.   ,  0.125,  0.25 ,  0.   ,  0.   ])

In [105]:
predictedGrade=np.argmax(Prob)

In [106]:
Grade=['A', 'B+', 'B', 'C+', 'C' , 'D+' , 'D' , 'F' , 'W' , 'S' , 'S#' , 'U' , 'U#']
Grade[predictedGrade]


Out[106]:
'W'

Import file transform.xlsx


In [107]:
import pandas as pd
df_file = pd.read_excel('../src/transform.xlsx')
df_file = df_file.fillna(0)
df_file = df_file.replace(['A', 'B+', 'B', 'C+', 'C' , 'D+' , 'D' , 'F' , 'W' , 'S' , 'S#' , 'U' , 'U#'], 
                     [13, 12, 11, 10 , 9, 8, 7, 6, 5, 4, 3, 2, 1])
df_file_new = df_file.drop(['0STUDENTID','1ACADYEAR','2SEMESTER','3COURSEID','4RESULT'],1)

classify one column from transform.xlsx


In [108]:
df_file16 = df_file_new[16:17]
import numpy as np
u = df_file16.as_matrix()[0]
Prob=t.classify(u)

In [109]:
u


Out[109]:
array([  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   9.,   9.,   0.,
         8.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   7.,
         6.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   6.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
        11.,   0.,   0.,   0.,   0.,   0.,   6.,   0.,   9.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   9.,   0.,  10.,   0.,   0.,   0.,   0.,   8.,   0.,
        12.,  11.,   0.,   7.,   0.])

In [110]:
u.shape


Out[110]:
(203L,)

In [111]:
Prob


Out[111]:
array([ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.18181818,  0.36363636,  0.09090909,  0.36363636,
        0.        ,  0.        ,  0.        ,  0.        ])

In [112]:
predictedGrade=np.argmax(Prob)
print predictedGrade


7

In [113]:
Grade=['A', 'B+', 'B', 'C+', 'C' , 'D+' , 'D' , 'F' , 'W' , 'S' , 'S#' , 'U' , 'U#','na']
Grade[::-1][predictedGrade]


Out[113]:
'D'

In [114]:
df_file[16:17]


Out[114]:
0STUDENTID 1ACADYEAR 2SEMESTER 3COURSEID 4RESULT AN201 AS171 AS177 AS178 AT207 ... TU113 TU115 TU116 TU120 TU121 TU122 TU130 TU153 TU154 TU156
16 316644 2553 1 CS213 9 0 0 0 0 0 ... 0 0 0 8 0 12 11 0 7 0

1 rows × 208 columns


In [115]:
df_file[123:124]


Out[115]:
0STUDENTID 1ACADYEAR 2SEMESTER 3COURSEID 4RESULT AN201 AS171 AS177 AS178 AT207 ... TU113 TU115 TU116 TU120 TU121 TU122 TU130 TU153 TU154 TU156
123 321848 2553 1 CS213 10 0 0 0 0 0 ... 0 0 0 8 0 0 0 0 13 0

1 rows × 208 columns


In [116]:
df_file[415:416]


Out[116]:
0STUDENTID 1ACADYEAR 2SEMESTER 3COURSEID 4RESULT AN201 AS171 AS177 AS178 AT207 ... TU113 TU115 TU116 TU120 TU121 TU122 TU130 TU153 TU154 TU156
415 329171 2554 2 CS213 11 0 0 0 0 0 ... 0 0 0 10 0 0 11 0 8 0

1 rows × 208 columns

Read(load) tree from pickle to classify


In [119]:
with open('CS213/dataset04.pic', 'rb') as pickleFile:
     data = pickle.load(pickleFile)

In [120]:
I=data[-2];L=data[-3]

Check the file size


In [121]:
ls train


 Volume in drive D is DATA
 Volume Serial Number is E617-8A3A

 Directory of D:\project\forcast\src\train

04/07/2016  05:03 PM    <DIR>          .
04/07/2016  05:03 PM    <DIR>          ..
04/07/2016  05:53 PM           249,910 dataset00.pic
04/07/2016  05:53 PM           249,910 dataset01.pic
04/07/2016  05:53 PM           249,910 dataset02.pic
04/07/2016  05:53 PM           249,910 dataset03.pic
04/07/2016  05:53 PM           249,910 dataset04.pic
04/07/2016  05:06 PM           160,983 out_tree.pic
04/07/2016  03:37 PM            44,363 out_tree1.pic
04/07/2016  03:43 PM            42,899 out_tree2.pic
04/07/2016  03:46 PM            42,905 out_tree3.pic
04/07/2016  04:00 PM            45,815 out_tree4.pic
04/07/2016  04:04 PM            48,779 out_tree5.pic
              11 File(s)      1,635,294 bytes
               2 Dir(s)  258,435,641,344 bytes free

In [122]:
I


Out[122]:
array([[ 0,  0,  0, ...,  0,  8,  0],
       [ 0,  0,  0, ...,  0,  8,  0],
       [ 0,  0,  0, ...,  0, 10,  0],
       ..., 
       [ 0,  0,  0, ...,  0, 10,  0],
       [ 0,  0,  0, ...,  0,  0,  0],
       [ 0,  0,  0, ...,  0,  0,  0]], dtype=int64)

In [123]:
I.shape


Out[123]:
(153L, 203L)

In [124]:
L


Out[124]:
array([ 6,  8,  6,  9,  6, 10,  7,  9,  6, 13,  5,  9,  6,  5,  9,  9,  6,
        9,  8,  8,  6,  9,  6, 13,  6, 13, 13, 13, 13,  6,  6, 10,  6,  6,
        8,  7,  7,  6,  8, 12,  6,  9, 13,  7, 10,  6,  6, 10,  6,  6, 13,
        6,  6,  8,  7,  6,  6,  9,  7,  7,  7,  6,  6, 12,  7,  7,  6,  6,
        6,  9,  8,  6, 11,  7, 10,  6,  7, 10,  8, 10,  7,  7,  6, 11,  6,
        6,  6,  6,  6,  6,  7,  6,  7,  6,  7, 13,  8, 12,  8, 13,  6,  8,
        9, 13,  8,  6,  6,  6,  6,  8,  6, 13,  6,  6,  6,  7,  6,  8,  6,
        6,  6,  5,  7,  6, 11,  6,  6,  6,  5,  7,  6,  8,  6,  6,  6,  6,
        6,  5,  6,  8,  6,  6,  5,  6,  7,  6,  7,  5,  6,  5,  7,  7,  7], dtype=int64)

In [125]:
L.shape


Out[125]:
(153L,)

Show prediction compare with actual


In [126]:
i=0
for i in xrange(len(L)):
    Prob=t.classify(I[i])
    predictedGrade=np.argmax(Prob)
    print "actual grade:%d predicted grade:%d"%(L[i],predictedGrade)


actual grade:6 predicted grade:7
actual grade:8 predicted grade:6
actual grade:6 predicted grade:5
actual grade:9 predicted grade:9
actual grade:6 predicted grade:9
actual grade:10 predicted grade:9
actual grade:7 predicted grade:7
actual grade:9 predicted grade:8
actual grade:6 predicted grade:5
actual grade:13 predicted grade:13
actual grade:5 predicted grade:9
actual grade:9 predicted grade:9
actual grade:6 predicted grade:5
actual grade:5 predicted grade:6
actual grade:9 predicted grade:8
actual grade:9 predicted grade:5
actual grade:6 predicted grade:9
actual grade:9 predicted grade:6
actual grade:8 predicted grade:9
actual grade:8 predicted grade:9
actual grade:6 predicted grade:6
actual grade:9 predicted grade:9
actual grade:6 predicted grade:6
actual grade:13 predicted grade:13
actual grade:6 predicted grade:5
actual grade:13 predicted grade:13
actual grade:13 predicted grade:13
actual grade:13 predicted grade:13
actual grade:13 predicted grade:13
actual grade:6 predicted grade:5
actual grade:6 predicted grade:9
actual grade:10 predicted grade:9
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:8 predicted grade:8
actual grade:7 predicted grade:9
actual grade:7 predicted grade:13
actual grade:6 predicted grade:13
actual grade:8 predicted grade:7
actual grade:12 predicted grade:9
actual grade:6 predicted grade:8
actual grade:9 predicted grade:10
actual grade:13 predicted grade:13
actual grade:7 predicted grade:7
actual grade:10 predicted grade:13
actual grade:6 predicted grade:8
actual grade:6 predicted grade:7
actual grade:10 predicted grade:9
actual grade:6 predicted grade:5
actual grade:6 predicted grade:8
actual grade:13 predicted grade:9
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:8 predicted grade:9
actual grade:7 predicted grade:9
actual grade:6 predicted grade:8
actual grade:6 predicted grade:8
actual grade:9 predicted grade:9
actual grade:7 predicted grade:8
actual grade:7 predicted grade:8
actual grade:7 predicted grade:8
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:12 predicted grade:8
actual grade:7 predicted grade:8
actual grade:7 predicted grade:9
actual grade:6 predicted grade:8
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:8
actual grade:6 predicted grade:9
actual grade:11 predicted grade:10
actual grade:7 predicted grade:8
actual grade:10 predicted grade:8
actual grade:6 predicted grade:8
actual grade:7 predicted grade:8
actual grade:10 predicted grade:8
actual grade:8 predicted grade:8
actual grade:10 predicted grade:9
actual grade:7 predicted grade:8
actual grade:7 predicted grade:9
actual grade:6 predicted grade:5
actual grade:11 predicted grade:13
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:8
actual grade:7 predicted grade:8
actual grade:6 predicted grade:8
actual grade:7 predicted grade:10
actual grade:6 predicted grade:8
actual grade:7 predicted grade:9
actual grade:13 predicted grade:8
actual grade:8 predicted grade:9
actual grade:12 predicted grade:9
actual grade:8 predicted grade:9
actual grade:13 predicted grade:13
actual grade:6 predicted grade:7
actual grade:8 predicted grade:9
actual grade:9 predicted grade:10
actual grade:13 predicted grade:13
actual grade:8 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:6
actual grade:8 predicted grade:9
actual grade:6 predicted grade:6
actual grade:13 predicted grade:10
actual grade:6 predicted grade:9
actual grade:6 predicted grade:6
actual grade:6 predicted grade:9
actual grade:7 predicted grade:8
actual grade:6 predicted grade:8
actual grade:8 predicted grade:10
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:5
actual grade:5 predicted grade:5
actual grade:7 predicted grade:7
actual grade:6 predicted grade:9
actual grade:11 predicted grade:10
actual grade:6 predicted grade:5
actual grade:6 predicted grade:5
actual grade:6 predicted grade:7
actual grade:5 predicted grade:9
actual grade:7 predicted grade:9
actual grade:6 predicted grade:9
actual grade:8 predicted grade:13
actual grade:6 predicted grade:9
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:6 predicted grade:6
actual grade:6 predicted grade:5
actual grade:5 predicted grade:6
actual grade:6 predicted grade:5
actual grade:8 predicted grade:13
actual grade:6 predicted grade:7
actual grade:6 predicted grade:7
actual grade:5 predicted grade:7
actual grade:6 predicted grade:7
actual grade:7 predicted grade:9
actual grade:6 predicted grade:9
actual grade:7 predicted grade:5
actual grade:5 predicted grade:8
actual grade:6 predicted grade:5
actual grade:5 predicted grade:5
actual grade:7 predicted grade:9
actual grade:7 predicted grade:8
actual grade:7 predicted grade:8

In [127]:
i=0
actual1=np.array(L)
predict1=np.zeros(len(L))
for i in xrange(len(L)):
    Prob=t.classify(I[i])
    predict1[i]=np.argmax(Prob)
   
    print "actual grade:%d predicted grade:%d"%(actual1[i],predict1[i])


actual grade:6 predicted grade:7
actual grade:8 predicted grade:6
actual grade:6 predicted grade:5
actual grade:9 predicted grade:9
actual grade:6 predicted grade:9
actual grade:10 predicted grade:9
actual grade:7 predicted grade:7
actual grade:9 predicted grade:8
actual grade:6 predicted grade:5
actual grade:13 predicted grade:13
actual grade:5 predicted grade:9
actual grade:9 predicted grade:9
actual grade:6 predicted grade:5
actual grade:5 predicted grade:6
actual grade:9 predicted grade:8
actual grade:9 predicted grade:5
actual grade:6 predicted grade:9
actual grade:9 predicted grade:6
actual grade:8 predicted grade:9
actual grade:8 predicted grade:9
actual grade:6 predicted grade:6
actual grade:9 predicted grade:9
actual grade:6 predicted grade:6
actual grade:13 predicted grade:13
actual grade:6 predicted grade:5
actual grade:13 predicted grade:13
actual grade:13 predicted grade:13
actual grade:13 predicted grade:13
actual grade:13 predicted grade:13
actual grade:6 predicted grade:5
actual grade:6 predicted grade:9
actual grade:10 predicted grade:9
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:8 predicted grade:8
actual grade:7 predicted grade:9
actual grade:7 predicted grade:13
actual grade:6 predicted grade:13
actual grade:8 predicted grade:7
actual grade:12 predicted grade:9
actual grade:6 predicted grade:8
actual grade:9 predicted grade:10
actual grade:13 predicted grade:13
actual grade:7 predicted grade:7
actual grade:10 predicted grade:13
actual grade:6 predicted grade:8
actual grade:6 predicted grade:7
actual grade:10 predicted grade:9
actual grade:6 predicted grade:5
actual grade:6 predicted grade:8
actual grade:13 predicted grade:9
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:8 predicted grade:9
actual grade:7 predicted grade:9
actual grade:6 predicted grade:8
actual grade:6 predicted grade:8
actual grade:9 predicted grade:9
actual grade:7 predicted grade:8
actual grade:7 predicted grade:8
actual grade:7 predicted grade:8
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:12 predicted grade:8
actual grade:7 predicted grade:8
actual grade:7 predicted grade:9
actual grade:6 predicted grade:8
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:8
actual grade:6 predicted grade:9
actual grade:11 predicted grade:10
actual grade:7 predicted grade:8
actual grade:10 predicted grade:8
actual grade:6 predicted grade:8
actual grade:7 predicted grade:8
actual grade:10 predicted grade:8
actual grade:8 predicted grade:8
actual grade:10 predicted grade:9
actual grade:7 predicted grade:8
actual grade:7 predicted grade:9
actual grade:6 predicted grade:5
actual grade:11 predicted grade:13
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:8
actual grade:7 predicted grade:8
actual grade:6 predicted grade:8
actual grade:7 predicted grade:10
actual grade:6 predicted grade:8
actual grade:7 predicted grade:9
actual grade:13 predicted grade:8
actual grade:8 predicted grade:9
actual grade:12 predicted grade:9
actual grade:8 predicted grade:9
actual grade:13 predicted grade:13
actual grade:6 predicted grade:7
actual grade:8 predicted grade:9
actual grade:9 predicted grade:10
actual grade:13 predicted grade:13
actual grade:8 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:6
actual grade:8 predicted grade:9
actual grade:6 predicted grade:6
actual grade:13 predicted grade:10
actual grade:6 predicted grade:9
actual grade:6 predicted grade:6
actual grade:6 predicted grade:9
actual grade:7 predicted grade:8
actual grade:6 predicted grade:8
actual grade:8 predicted grade:10
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:5
actual grade:5 predicted grade:5
actual grade:7 predicted grade:7
actual grade:6 predicted grade:9
actual grade:11 predicted grade:10
actual grade:6 predicted grade:5
actual grade:6 predicted grade:5
actual grade:6 predicted grade:7
actual grade:5 predicted grade:9
actual grade:7 predicted grade:9
actual grade:6 predicted grade:9
actual grade:8 predicted grade:13
actual grade:6 predicted grade:9
actual grade:6 predicted grade:8
actual grade:6 predicted grade:9
actual grade:6 predicted grade:6
actual grade:6 predicted grade:5
actual grade:5 predicted grade:6
actual grade:6 predicted grade:5
actual grade:8 predicted grade:13
actual grade:6 predicted grade:7
actual grade:6 predicted grade:7
actual grade:5 predicted grade:7
actual grade:6 predicted grade:7
actual grade:7 predicted grade:9
actual grade:6 predicted grade:9
actual grade:7 predicted grade:5
actual grade:5 predicted grade:8
actual grade:6 predicted grade:5
actual grade:5 predicted grade:5
actual grade:7 predicted grade:9
actual grade:7 predicted grade:8
actual grade:7 predicted grade:8

find Average


In [128]:
np.sqrt(np.sum((actual1-predict1)**2)/len(L))


Out[128]:
2.1374347828601441

Plot graph

number of correct prediction


In [129]:
from matplotlib import pyplot as plt
%pylab inline
plt.hist(actual1-predict1,50)


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['cm', 'sum']
`%matplotlib` prevents importing * from pylab and numpy
Out[129]:
(array([  1.,   0.,   0.,   0.,   1.,   0.,   0.,   0.,   2.,   0.,   0.,
          0.,   2.,   0.,   0.,   0.,  27.,   0.,   0.,   0.,  28.,   0.,
          0.,   0.,   0.,  30.,   0.,   0.,   0.,  28.,   0.,   0.,   0.,
         22.,   0.,   0.,   0.,   4.,   0.,   0.,   0.,   4.,   0.,   0.,
          0.,   3.,   0.,   0.,   0.,   1.]),
 array([-7.  , -6.76, -6.52, -6.28, -6.04, -5.8 , -5.56, -5.32, -5.08,
        -4.84, -4.6 , -4.36, -4.12, -3.88, -3.64, -3.4 , -3.16, -2.92,
        -2.68, -2.44, -2.2 , -1.96, -1.72, -1.48, -1.24, -1.  , -0.76,
        -0.52, -0.28, -0.04,  0.2 ,  0.44,  0.68,  0.92,  1.16,  1.4 ,
         1.64,  1.88,  2.12,  2.36,  2.6 ,  2.84,  3.08,  3.32,  3.56,
         3.8 ,  4.04,  4.28,  4.52,  4.76,  5.  ]),
 <a list of 50 Patch objects>)

Create confusion matrix


In [130]:
predict1[0]


Out[130]:
7.0

In [131]:
actual1[0]


Out[131]:
6

In [132]:
n = 14
cm1 = np.zeros((n,n))
for i in xrange(len(actual1)):
    cm1[predict1[i],actual1[i]] +=1
print cm1


[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   2.  13.   1.   0.   1.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   2.   6.   0.   1.   1.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   1.   7.   3.   1.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   1.  17.  11.   3.   2.   2.   0.   1.   1.]
 [  0.   0.   0.   0.   0.   2.  24.   8.   8.   5.   4.   0.   2.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   1.   1.   2.   0.   2.   0.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   1.   1.   2.   0.   1.   1.   0.   9.]]

In [133]:
#find TP
from collections import defaultdict
tp = defaultdict(list)
fp = defaultdict(list)
for i in range(0,14):
    for j in range(0,14):
        if i == j: 
            tp[i] = cm1[i,j]
            TP = tp[i]+tp[i-1]+tp[i-2]+tp[i-3]+tp[i-4]+tp[i-5]+tp[i-6]+tp[i-7]+tp[i-8]+tp[i-9]+tp[i-10]+tp[i-11]+tp[i-12]+tp[i-13]
        
            
print "TP is %f"%(TP)
print "--------------------------------------------"
sum = cm1.sum()
FP = sum-TP
print "FP is %f"%(FP)

precision = TP/(FP+TP)
print "precision is %f"%(precision)


TP is 28.000000
--------------------------------------------
FP is 125.000000
precision is 0.183007

In [ ]:

Training(2)


In [134]:
import pickle
from pforest.master import master
m=master(dsetname='CS213')
m.reset()
m.train()
with open('CS213/out_tree2.pic', 'wb') as pickleFile:
    pickle.dump(m.root, pickleFile, pickle.HIGHEST_PROTOCOL)


master>>init() dsetname: CS213
master>> create dview
master>> init engine
Found pforest
debug:master:__init__: ['CS213/dataset00.pic', 'CS213/dataset01.pic', 'CS213/dataset02.pic', 'CS213/dataset03.pic']
master>> init local variables
master>>reset()
master>>reset() H: 2.8257
master>>reset() Q: 00612
master::train() node20*-
master::train() node19R-
master::train() node18R-
master::train() node17R-
master::train() node16R-
master::train() node15RG
master::train() node15LQ
master::train() node16LG
master::train() node17L-
master::train() node16R-
master::train() node15R-
master::train() node14R-
master::train() node13R-
master::train() node12RQ
master::train() node12L-
master::train() node11R-
master::train() node10RG
master::train() node10LG
master::train() node11LQ
master::train() node13LG
master::train() node14L-
master::train() node13R-
master::train() node12RG
master::train() node12LQ
master::train() node13LQ
master::train() node15LG
master::train() node16LG
master::train() node18L-
master::train() node17R-
master::train() node16R-
master::train() node15RQ
master::train() node15L-
master::train() node14RG
master::train() node14L-
master::train() node13RG
master::train() node13LG
master::train() node16L-
master::train() node15RG
master::train() node15L-
master::train() node14R-
master::train() node13RG
master::train() node13LG
master::train() node14L-
master::train() node13RG
master::train() node13L-
master::train() node12RQ
master::train() node12LG
master::train() node17L-
master::train() node16R-
master::train() node15R-
master::train() node14R-
master::train() node13R-
master::train() node12RG
master::train() node12L-
master::train() node11R-
master::train() node10R-
master::train() node9RG
master::train() node9L-
master::train() node8RG
master::train() node8LG
master::train() node10LG
master::train() node11L-
master::train() node10RQ
master::train() node10LG
master::train() node13L-
master::train() node12R-
master::train() node11RG
master::train() node11LG
master::train() node12LG
master::train() node14L-
master::train() node13R-
master::train() node12RQ
master::train() node12L-
master::train() node11RG
master::train() node11LG
master::train() node13L-
master::train() node12RG
master::train() node12LG
master::train() node15L-
master::train() node14R-
master::train() node13RG
master::train() node13L-
master::train() node12RG
master::train() node12LG
master::train() node14L-
master::train() node13RQ
master::train() node13L-
master::train() node12R-
master::train() node11RG
master::train() node11LG
master::train() node12L-
master::train() node11RG
master::train() node11LG
master::train() node16L-
master::train() node15R-
master::train() node14R-
master::train() node13R-
master::train() node12RQ
master::train() node12L-
master::train() node11RQ
master::train() node11L-
master::train() node10RG
master::train() node10LG
master::train() node13L-
master::train() node12R-
master::train() node11R-
master::train() node10RG
master::train() node10L-
master::train() node9R-
master::train() node8RG
master::train() node8LG
master::train() node9LG
master::train() node11L-
master::train() node10RG
master::train() node10LQ
master::train() node12LG
master::train() node14LG
master::train() node15L-
master::train() node14RG
master::train() node14LG
master::train() node19L-
master::train() node18R-
master::train() node17R-
master::train() node16RG
master::train() node16L-
master::train() node15R-
master::train() node14RG
master::train() node14LG
master::train() node15LG
master::train() node17L-
master::train() node16RG
master::train() node16L-
master::train() node15RG
master::train() node15L-
master::train() node14RQ
master::train() node14L-
master::train() node13RG
master::train() node13LG
master::train() node18L-
master::train() node17R-
master::train() node16R-
master::train() node15R-
master::train() node14RG
master::train() node14L-
master::train() node13RG
master::train() node13LG
master::train() node15LG
master::train() node16LG
master::train() node17L-
master::train() node16R-
master::train() node15RG
master::train() node15L-
master::train() node14RG
master::train() node14LG
master::train() node16L-
master::train() node15RG
master::train() node15LG

In [45]:
from pforest.dataset import dataset
from pforest.tree import tree
with open('CS213/out_tree2.pic', 'rb') as pickleFile:
    root = pickle.load(pickleFile)
#init the test treetree213.pic
t=tree()
t.settree(root)
t.show()


*- 20 H:2.842e+00,Q:000306 tau:11.0 theta:[ 19.]
L- 19 H:2.603e+00,Q:000241 tau:10.0 theta:[ 189.]
L- 18 H:2.686e+00,Q:000083 tau:7.0 theta:[ 25.]
L- 17 H:2.350e+00,Q:000064 tau:7.0 theta:[ 98.]
L- 16 H:1.275e+00,Q:000010 tau:11.0 theta:[ 20.]
L- 15 H:1.094e+00,Q:000008 tau:7.0 theta:[ 22.]
LG 14 H:9.704e-36,Q:000002 (cl,P):(006,1.00) (013,0.00) (012,0.00)
R- 14 H:5.409e-01,Q:000006 tau:9.0 theta:[ 160.]
LG 13 H:5.000e-01,Q:000004 (cl,P):(009,0.50) (008,0.25) (007,0.25)
RG 13 H:9.704e-36,Q:000002 (cl,P):(007,1.00) (013,0.00) (012,0.00)
RG 15 H:9.704e-36,Q:000002 (cl,P):(010,1.00) (013,0.00) (012,0.00)
R- 16 H:2.189e+00,Q:000054 tau:9.0 theta:[ 20.]
L- 15 H:2.007e+00,Q:000024 tau:9.0 theta:[ 60.]
L- 14 H:1.869e+00,Q:000022 tau:8.0 theta:[ 20.]
L- 13 H:1.500e+00,Q:000012 tau:11.0 theta:[ 98.]
LG 12 H:1.206e+00,Q:000009 (cl,P):(007,0.44) (006,0.33) (008,0.11)
RG 12 H:9.183e-01,Q:000003 (cl,P):(005,0.67) (006,0.33) (013,0.00)
RG 13 H:1.751e+00,Q:000010 (cl,P):(009,0.30) (007,0.30) (005,0.20)
RG 14 H:1.000e+00,Q:000002 (cl,P):(010,0.50) (008,0.50) (013,0.00)
R- 15 H:1.903e+00,Q:000030 tau:9.0 theta:[ 98.]
LG 14 H:1.623e+00,Q:000017 (cl,P):(009,0.41) (008,0.35) (007,0.12)
RG 14 H:1.397e+00,Q:000013 (cl,P):(009,0.38) (010,0.23) (007,0.23)
R- 17 H:2.276e+00,Q:000019 tau:9.0 theta:[ 126.]
LG 16 H:1.556e+00,Q:000009 (cl,P):(010,0.44) (009,0.22) (011,0.11)
R- 16 H:1.925e+00,Q:000010 tau:8.0 theta:[ 98.]
LG 15 H:9.183e-01,Q:000003 (cl,P):(010,0.67) (006,0.33) (013,0.00)
R- 15 H:1.265e+00,Q:000007 tau:8.0 theta:[ 125.]
L- 14 H:1.000e+00,Q:000004 tau:9.0 theta:[ 29.]
LQ 13 H:1.925e-35,Q:000001 (cl,P):(011,1.00) (013,0.00) (012,0.00)
RG 13 H:6.667e-01,Q:000003 (cl,P):(013,0.33) (010,0.33) (009,0.33)
R- 14 H:9.183e-01,Q:000003 tau:8.0 theta:[ 191.]
LQ 13 H:1.925e-35,Q:000001 (cl,P):(011,1.00) (013,0.00) (012,0.00)
RG 13 H:1.000e+00,Q:000002 (cl,P):(012,0.50) (011,0.50) (013,0.00)
R- 18 H:2.405e+00,Q:000158 tau:9.0 theta:[ 201.]
L- 17 H:2.361e+00,Q:000067 tau:9.0 theta:[ 22.]
L- 16 H:2.207e+00,Q:000033 tau:7.0 theta:[ 130.]
LG 15 H:1.926e+00,Q:000022 (cl,P):(007,0.32) (009,0.23) (008,0.23)
R- 15 H:1.798e+00,Q:000011 tau:9.0 theta:[ 160.]
L- 14 H:1.644e+00,Q:000007 tau:9.0 theta:[ 86.]
L- 13 H:1.200e+00,Q:000005 tau:8.0 theta:[ 130.]
LQ 12 H:1.925e-35,Q:000002 (cl,P):(010,0.50) (009,0.50) (013,0.00)
RG 12 H:9.183e-01,Q:000003 (cl,P):(008,0.67) (011,0.33) (013,0.00)
RG 13 H:9.704e-36,Q:000002 (cl,P):(009,1.00) (013,0.00) (012,0.00)
RG 14 H:1.189e+00,Q:000004 (cl,P):(009,0.50) (011,0.25) (006,0.25)
R- 16 H:2.083e+00,Q:000034 tau:11.0 theta:[ 99.]
L- 15 H:2.065e+00,Q:000024 tau:9.0 theta:[ 31.]
L- 14 H:1.956e+00,Q:000021 tau:9.0 theta:[ 34.]
L- 13 H:1.816e+00,Q:000018 tau:8.0 theta:[ 191.]
L- 12 H:1.609e+00,Q:000013 tau:12.0 theta:[ 169.]
L- 11 H:1.667e+00,Q:000012 tau:9.0 theta:[ 98.]
LG 10 H:1.333e+00,Q:000006 (cl,P):(009,0.50) (008,0.17) (007,0.17)
RG 10 H:1.333e+00,Q:000006 (cl,P):(009,0.50) (011,0.17) (008,0.17)
RQ 11 H:1.925e-35,Q:000001 (cl,P):(009,1.00) (013,0.00) (012,0.00)
RG 12 H:1.600e+00,Q:000005 (cl,P):(009,0.40) (010,0.20) (008,0.20)
RG 13 H:9.183e-01,Q:000003 (cl,P):(010,0.67) (008,0.33) (013,0.00)
RG 14 H:6.667e-01,Q:000003 (cl,P):(010,0.67) (008,0.33) (013,0.00)
RG 15 H:1.216e+00,Q:000010 (cl,P):(009,0.50) (008,0.30) (011,0.20)
R- 17 H:2.290e+00,Q:000091 tau:10.0 theta:[ 160.]
L- 16 H:2.318e+00,Q:000064 tau:10.0 theta:[ 201.]
L- 15 H:2.249e+00,Q:000036 tau:7.0 theta:[ 38.]
L- 14 H:2.112e+00,Q:000031 tau:6.0 theta:[ 160.]
LG 13 H:1.201e+00,Q:000008 (cl,P):(009,0.50) (010,0.25) (011,0.12)
RG 13 H:2.031e+00,Q:000023 (cl,P):(008,0.30) (009,0.22) (007,0.22)
R- 14 H:1.371e+00,Q:000005 tau:11.0 theta:[ 15.]
L- 13 H:9.183e-01,Q:000003 tau:12.0 theta:[ 189.]
LQ 12 H:1.925e-35,Q:000001 (cl,P):(008,1.00) (013,0.00) (012,0.00)
RG 12 H:1.000e+00,Q:000002 (cl,P):(010,0.50) (008,0.50) (013,0.00)
RG 13 H:1.000e+00,Q:000002 (cl,P):(011,0.50) (008,0.50) (013,0.00)
RG 15 H:2.014e+00,Q:000028 (cl,P):(009,0.32) (010,0.25) (008,0.21)
R- 16 H:1.722e+00,Q:000027 tau:12.0 theta:[ 98.]
L- 15 H:1.824e+00,Q:000023 tau:7.0 theta:[ 196.]
L- 14 H:9.183e-01,Q:000003 tau:3.0 theta:[ 97.]
LQ 13 H:1.925e-35,Q:000001 (cl,P):(009,1.00) (013,0.00) (012,0.00)
RG 13 H:9.704e-36,Q:000002 (cl,P):(010,1.00) (013,0.00) (012,0.00)
R- 14 H:1.795e+00,Q:000020 tau:10.0 theta:[ 19.]
L- 13 H:1.670e+00,Q:000011 tau:10.0 theta:[ 103.]
LG 12 H:1.675e+00,Q:000010 (cl,P):(009,0.40) (008,0.30) (010,0.20)
RQ 12 H:1.925e-35,Q:000001 (cl,P):(011,1.00) (013,0.00) (012,0.00)
RG 13 H:1.279e+00,Q:000009 (cl,P):(009,0.44) (010,0.33) (011,0.22)
RG 15 H:4.890e-36,Q:000004 (cl,P):(009,1.00) (013,0.00) (012,0.00)
R- 19 H:2.574e+00,Q:000065 tau:11.0 theta:[ 22.]
L- 18 H:2.269e+00,Q:000037 tau:9.0 theta:[ 98.]
LG 17 H:2.440e+00,Q:000012 (cl,P):(011,0.25) (010,0.17) (009,0.17)
RG 17 H:1.760e+00,Q:000025 (cl,P):(010,0.48) (009,0.16) (011,0.12)
R- 18 H:1.728e+00,Q:000028 tau:10.0 theta:[ 125.]
LG 17 H:1.333e+00,Q:000006 (cl,P):(013,0.33) (012,0.33) (011,0.33)
R- 17 H:1.632e+00,Q:000022 tau:12.0 theta:[ 162.]
L- 16 H:1.495e+00,Q:000020 tau:9.0 theta:[ 162.]
LQ 15 H:1.925e-35,Q:000001 (cl,P):(012,1.00) (013,0.00) (011,0.00)
RG 15 H:1.483e+00,Q:000019 (cl,P):(013,0.47) (012,0.32) (011,0.16)
RQ 16 H:1.925e-35,Q:000002 (cl,P):(012,0.50) (010,0.50) (013,0.00)


In [49]:
with open('CS213/dataset04.pic', 'rb') as pickleFile:
     data = pickle.load(pickleFile)

In [50]:
I=data[-2];L=data[-3]

In [51]:
I


Out[51]:
array([[0, 0, 0, ..., 0, 5, 0],
       [0, 0, 0, ..., 0, 5, 0],
       [0, 0, 0, ..., 0, 5, 0],
       ..., 
       [0, 0, 0, ..., 0, 9, 0],
       [0, 0, 0, ..., 0, 9, 0],
       [0, 0, 0, ..., 0, 6, 0]], dtype=int64)

In [52]:
L


Out[52]:
array([ 9,  7, 10, 13,  7, 10,  9,  6,  9, 10,  8, 10,  7,  8,  7,  8,  9,
        9,  7,  9,  9,  8,  9,  8,  8, 10, 10, 11, 10,  7,  5,  6,  5,  9,
       10,  8, 11,  7,  8, 10,  7,  8, 10, 11,  9,  6,  8, 11, 11, 10,  7,
        9,  6,  9, 13,  9, 11,  9, 10,  9, 10,  9, 11, 12,  9, 10, 13, 12,
        9,  9, 12, 11,  9,  9,  8, 10,  9,  9, 13, 10,  9, 13,  9, 13,  9,
        8,  8, 11,  8, 10,  8, 10, 12,  9,  8,  9, 10, 12, 10,  9,  9,  9,
        7, 10, 11, 10,  8,  9,  8, 11, 11, 10,  8,  9,  8,  9, 10,  9, 10,
        9,  9, 11,  8,  9,  9, 10,  9,  9,  6,  7,  9,  8,  7,  7, 10,  8,
        7, 10,  9,  8,  9, 10,  9,  6, 11, 12,  6,  7,  7, 10, 10, 10,  6], dtype=int64)

In [53]:
i=0
actual2=np.array(L)
predict2=np.zeros(len(L))
for i in xrange(len(L)):
    Prob=t.classify(I[i])
    predict2[i]=np.argmax(Prob)
   
    print "actual grade:%d predicted grade:%d"%(actual2[i],predict2[i])


actual grade:9 predicted grade:9
actual grade:7 predicted grade:9
actual grade:10 predicted grade:10
actual grade:13 predicted grade:11
actual grade:7 predicted grade:9
actual grade:10 predicted grade:9
actual grade:9 predicted grade:9
actual grade:6 predicted grade:7
actual grade:9 predicted grade:7
actual grade:10 predicted grade:10
actual grade:8 predicted grade:10
actual grade:10 predicted grade:10
actual grade:7 predicted grade:7
actual grade:8 predicted grade:7
actual grade:7 predicted grade:7
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:8
actual grade:7 predicted grade:7
actual grade:9 predicted grade:9
actual grade:9 predicted grade:8
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:8 predicted grade:10
actual grade:10 predicted grade:10
actual grade:10 predicted grade:10
actual grade:11 predicted grade:10
actual grade:10 predicted grade:10
actual grade:7 predicted grade:7
actual grade:5 predicted grade:5
actual grade:6 predicted grade:5
actual grade:5 predicted grade:5
actual grade:9 predicted grade:9
actual grade:10 predicted grade:10
actual grade:8 predicted grade:9
actual grade:11 predicted grade:10
actual grade:7 predicted grade:9
actual grade:8 predicted grade:10
actual grade:10 predicted grade:10
actual grade:7 predicted grade:7
actual grade:8 predicted grade:10
actual grade:10 predicted grade:9
actual grade:11 predicted grade:9
actual grade:9 predicted grade:7
actual grade:6 predicted grade:7
actual grade:8 predicted grade:9
actual grade:11 predicted grade:11
actual grade:11 predicted grade:11
actual grade:10 predicted grade:9
actual grade:7 predicted grade:7
actual grade:9 predicted grade:9
actual grade:6 predicted grade:7
actual grade:9 predicted grade:9
actual grade:13 predicted grade:11
actual grade:9 predicted grade:9
actual grade:11 predicted grade:9
actual grade:9 predicted grade:9
actual grade:10 predicted grade:9
actual grade:9 predicted grade:9
actual grade:10 predicted grade:10
actual grade:9 predicted grade:9
actual grade:11 predicted grade:9
actual grade:12 predicted grade:13
actual grade:9 predicted grade:9
actual grade:10 predicted grade:10
actual grade:13 predicted grade:13
actual grade:12 predicted grade:9
actual grade:9 predicted grade:7
actual grade:9 predicted grade:9
actual grade:12 predicted grade:10
actual grade:11 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:7
actual grade:8 predicted grade:9
actual grade:10 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:13 predicted grade:13
actual grade:10 predicted grade:9
actual grade:9 predicted grade:9
actual grade:13 predicted grade:13
actual grade:9 predicted grade:9
actual grade:13 predicted grade:13
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:8 predicted grade:8
actual grade:11 predicted grade:9
actual grade:8 predicted grade:10
actual grade:10 predicted grade:9
actual grade:8 predicted grade:9
actual grade:10 predicted grade:10
actual grade:12 predicted grade:12
actual grade:9 predicted grade:9
actual grade:8 predicted grade:8
actual grade:9 predicted grade:9
actual grade:10 predicted grade:13
actual grade:12 predicted grade:13
actual grade:10 predicted grade:10
actual grade:9 predicted grade:9
actual grade:9 predicted grade:11
actual grade:9 predicted grade:7
actual grade:7 predicted grade:7
actual grade:10 predicted grade:10
actual grade:11 predicted grade:13
actual grade:10 predicted grade:10
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:8
actual grade:11 predicted grade:11
actual grade:11 predicted grade:13
actual grade:10 predicted grade:9
actual grade:8 predicted grade:7
actual grade:9 predicted grade:9
actual grade:8 predicted grade:8
actual grade:9 predicted grade:9
actual grade:10 predicted grade:10
actual grade:9 predicted grade:10
actual grade:10 predicted grade:10
actual grade:9 predicted grade:9
actual grade:9 predicted grade:7
actual grade:11 predicted grade:10
actual grade:8 predicted grade:10
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:10 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:8
actual grade:6 predicted grade:7
actual grade:7 predicted grade:7
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:7 predicted grade:9
actual grade:7 predicted grade:9
actual grade:10 predicted grade:8
actual grade:8 predicted grade:8
actual grade:7 predicted grade:9
actual grade:10 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:7
actual grade:9 predicted grade:9
actual grade:10 predicted grade:9
actual grade:9 predicted grade:9
actual grade:6 predicted grade:7
actual grade:11 predicted grade:13
actual grade:12 predicted grade:13
actual grade:6 predicted grade:7
actual grade:7 predicted grade:8
actual grade:7 predicted grade:9
actual grade:10 predicted grade:10
actual grade:10 predicted grade:8
actual grade:10 predicted grade:9
actual grade:6 predicted grade:7

In [54]:
np.sqrt(np.sum((actual2-predict2)**2)/len(L))


Out[54]:
1.1260434521851945

In [55]:
from matplotlib import pyplot as plt
%pylab inline
plt.hist(actual2-predict2,30)


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['sum']
`%matplotlib` prevents importing * from pylab and numpy
Out[55]:
(array([  1.,   0.,   0.,   0.,   0.,  17.,   0.,   0.,   0.,   0.,  22.,
          0.,   0.,   0.,   0.,  74.,   0.,   0.,   0.,   0.,  22.,   0.,
          0.,   0.,   0.,  16.,   0.,   0.,   0.,   1.]),
 array([-3. , -2.8, -2.6, -2.4, -2.2, -2. , -1.8, -1.6, -1.4, -1.2, -1. ,
        -0.8, -0.6, -0.4, -0.2,  0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,
         1.4,  1.6,  1.8,  2. ,  2.2,  2.4,  2.6,  2.8,  3. ]),
 <a list of 30 Patch objects>)

In [56]:
n = 14
cm2 = np.zeros((n,n))
for i in xrange(len(actual2)):
    cm2[predict2[i],actual2[i]] +=1
print cm2


[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   2.   1.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   7.   8.   3.   6.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   1.   5.   3.   2.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   7.  10.  34.  12.   5.   1.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   6.   1.  17.   3.   1.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   0.   3.   0.   2.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   3.   3.   4.]]

In [57]:
#find TP
from collections import defaultdict
tp = defaultdict(list)
fp = defaultdict(list)
for i in range(0,14):
    for j in range(0,14):
        if i == j: 
            tp[i] = cm2[i,j]
            TP = tp[i]+tp[i-1]+tp[i-2]+tp[i-3]+tp[i-4]+tp[i-5]+tp[i-6]+tp[i-7]+tp[i-8]+tp[i-9]+tp[i-10]+tp[i-11]+tp[i-12]+tp[i-13]
        
            
print "TP is %f"%(TP)
print "--------------------------------------------"
sum = cm2.sum()
FP = sum-TP
print "FP is %f"%(FP)

precision = TP/(FP+TP)
print "precision is %f"%(precision)


TP is 74.000000
--------------------------------------------
FP is 79.000000
precision is 0.483660

Training(3)


In [58]:
import pickle
from pforest.master import master
m=master(dsetname='CS213')
m.reset()
m.train()
with open('CS213/out_tree3.pic', 'wb') as pickleFile:
    pickle.dump(m.root, pickleFile, pickle.HIGHEST_PROTOCOL)


master>>init() dsetname: CS213
master>> create dview
master>> init engine
Found pforest
debug:master:__init__: ['CS213/dataset00.pic', 'CS213/dataset01.pic']
master>> init local variables
master>>reset()
master>>reset() H: 2.7776
master>>reset() Q: 00306
master::train() node20*-
master::train() node19R-
master::train() node18R-
master::train() node17R-
master::train() node16RG
master::train() node16L-
master::train() node15R-
master::train() node14R-
master::train() node13R-
master::train() node12R-
master::train() node11RG
master::train() node11LG
master::train() node12LG
master::train() node13LG
master::train() node14LG
master::train() node15LG
master::train() node17L-
master::train() node16R-
master::train() node15R-
master::train() node14RG
master::train() node14LG
master::train() node15L-
master::train() node14RQ
master::train() node14LG
master::train() node16L-
master::train() node15R-
master::train() node14RG
master::train() node14LG
master::train() node15LG
master::train() node18L-
master::train() node17R-
master::train() node16R-
master::train() node15RG
master::train() node15LG
master::train() node16L-
master::train() node15RQ
master::train() node15LQ
master::train() node17L-
master::train() node16RG
master::train() node16L-
master::train() node15RG
master::train() node15L-
master::train() node14R-
master::train() node13RG
master::train() node13LG
master::train() node14LQ
master::train() node19L-
master::train() node18R-
master::train() node17R-
master::train() node16R-
master::train() node15RG
master::train() node15LG
master::train() node16L-
master::train() node15R-
master::train() node14RQ
master::train() node14L-
master::train() node13RQ
master::train() node13LG
master::train() node15LG
master::train() node17L-
master::train() node16RG
master::train() node16L-
master::train() node15R-
master::train() node14RG
master::train() node14L-
master::train() node13RQ
master::train() node13L-
master::train() node12RG
master::train() node12L-
master::train() node11RG
master::train() node11LG
master::train() node15LG
master::train() node18L-
master::train() node17RG
master::train() node17L-
master::train() node16RQ
master::train() node16L-
master::train() node15RQ
master::train() node15LG

In [60]:
from pforest.dataset import dataset
from pforest.tree import tree
with open('CS213/out_tree3.pic', 'rb') as pickleFile:
    root = pickle.load(pickleFile)
#init the test tree
t=tree()
t.settree(root)
t.show()


*- 20 H:2.778e+00,Q:000306 tau:9.0 theta:[ 22.]
L- 19 H:2.449e+00,Q:000098 tau:6.0 theta:[ 191.]
L- 18 H:1.995e+00,Q:000049 tau:7.0 theta:[ 160.]
L- 17 H:2.094e+00,Q:000016 tau:13.0 theta:[ 172.]
L- 16 H:2.004e+00,Q:000014 tau:11.0 theta:[ 19.]
LG 15 H:1.998e+00,Q:000013 (cl,P):(006,0.38) (010,0.15) (008,0.15)
RQ 15 H:1.925e-35,Q:000001 (cl,P):(010,1.00) (013,0.00) (012,0.00)
RQ 16 H:1.925e-35,Q:000002 (cl,P):(007,1.00) (013,0.00) (012,0.00)
RG 17 H:1.641e+00,Q:000033 (cl,P):(009,0.36) (006,0.27) (007,0.24)
R- 18 H:2.393e+00,Q:000049 tau:5.0 theta:[ 130.]
L- 17 H:1.841e+00,Q:000023 tau:10.0 theta:[ 102.]
L- 16 H:1.835e+00,Q:000020 tau:8.0 theta:[ 22.]
LG 15 H:6.667e-01,Q:000003 (cl,P):(007,0.67) (008,0.33) (013,0.00)
R- 15 H:1.829e+00,Q:000017 tau:12.0 theta:[ 169.]
L- 14 H:1.846e+00,Q:000015 tau:11.0 theta:[ 98.]
L- 13 H:1.786e+00,Q:000013 tau:8.0 theta:[ 201.]
L- 12 H:1.268e+00,Q:000006 tau:7.0 theta:[ 160.]
LG 11 H:6.887e-01,Q:000004 (cl,P):(007,0.75) (006,0.25) (013,0.00)
RG 11 H:9.704e-36,Q:000002 (cl,P):(009,1.00) (013,0.00) (012,0.00)
RG 12 H:1.373e+00,Q:000007 (cl,P):(009,0.43) (008,0.29) (007,0.14)
RQ 13 H:1.925e-35,Q:000002 (cl,P):(009,0.50) (008,0.50) (013,0.00)
RG 14 H:1.000e+00,Q:000002 (cl,P):(009,0.50) (008,0.50) (013,0.00)
RG 16 H:9.183e-01,Q:000003 (cl,P):(008,0.67) (006,0.33) (013,0.00)
R- 17 H:2.294e+00,Q:000026 tau:7.0 theta:[ 64.]
L- 16 H:2.410e+00,Q:000019 tau:10.0 theta:[ 183.]
LG 15 H:1.925e+00,Q:000010 (cl,P):(010,0.30) (009,0.30) (012,0.10)
R- 15 H:1.501e+00,Q:000009 tau:7.0 theta:[ 58.]
L- 14 H:1.265e+00,Q:000007 tau:11.0 theta:[ 99.]
LG 13 H:1.333e+00,Q:000006 (cl,P):(009,0.33) (008,0.33) (011,0.17)
RQ 13 H:1.925e-35,Q:000001 (cl,P):(009,1.00) (013,0.00) (012,0.00)
RQ 14 H:1.925e-35,Q:000002 (cl,P):(008,0.50) (007,0.50) (013,0.00)
R- 16 H:1.143e+00,Q:000007 tau:11.0 theta:[ 189.]
LG 15 H:1.000e+00,Q:000004 (cl,P):(010,0.50) (009,0.25) (008,0.25)
RG 15 H:1.289e-35,Q:000003 (cl,P):(009,1.00) (013,0.00) (012,0.00)
R- 19 H:2.590e+00,Q:000208 tau:11.0 theta:[ 189.]
L- 18 H:2.262e+00,Q:000059 tau:7.0 theta:[ 34.]
L- 17 H:2.238e+00,Q:000047 tau:12.0 theta:[ 196.]
L- 16 H:1.915e+00,Q:000024 tau:10.0 theta:[ 201.]
L- 15 H:1.803e+00,Q:000019 tau:8.0 theta:[ 20.]
LQ 14 H:1.925e-35,Q:000001 (cl,P):(008,1.00) (013,0.00) (012,0.00)
R- 14 H:1.673e+00,Q:000018 tau:10.0 theta:[ 20.]
LG 13 H:1.241e+00,Q:000011 (cl,P):(009,0.55) (007,0.27) (008,0.18)
RG 13 H:1.265e+00,Q:000007 (cl,P):(009,0.57) (011,0.29) (010,0.14)
RG 15 H:8.000e-01,Q:000005 (cl,P):(010,0.40) (009,0.40) (013,0.20)
RG 16 H:2.209e+00,Q:000023 (cl,P):(009,0.30) (008,0.26) (010,0.22)
R- 17 H:1.626e+00,Q:000012 tau:5.0 theta:[ 47.]
L- 16 H:1.000e+00,Q:000002 tau:12.0 theta:[ 111.]
LQ 15 H:1.925e-35,Q:000001 (cl,P):(012,1.00) (013,0.00) (011,0.00)
RQ 15 H:1.925e-35,Q:000001 (cl,P):(009,1.00) (013,0.00) (012,0.00)
R- 16 H:1.475e+00,Q:000010 tau:12.0 theta:[ 196.]
LG 15 H:1.000e+00,Q:000004 (cl,P):(013,0.25) (011,0.25) (010,0.25)
RG 15 H:6.667e-01,Q:000006 (cl,P):(009,0.67) (010,0.33) (013,0.00)
R- 18 H:2.598e+00,Q:000149 tau:11.0 theta:[ 19.]
L- 17 H:2.232e+00,Q:000093 tau:12.0 theta:[ 189.]
L- 16 H:2.127e+00,Q:000053 tau:8.0 theta:[ 125.]
LG 15 H:1.790e+00,Q:000025 (cl,P):(009,0.40) (008,0.32) (010,0.16)
R- 15 H:2.071e+00,Q:000028 tau:10.0 theta:[ 162.]
LG 14 H:1.667e+00,Q:000012 (cl,P):(010,0.42) (009,0.25) (008,0.25)
RG 14 H:2.070e+00,Q:000016 (cl,P):(010,0.38) (009,0.25) (011,0.12)
R- 16 H:1.981e+00,Q:000040 tau:10.0 theta:[ 22.]
L- 15 H:1.388e+00,Q:000017 tau:12.0 theta:[ 159.]
LG 14 H:1.398e+00,Q:000016 (cl,P):(009,0.56) (005,0.19) (006,0.12)
RQ 14 H:1.925e-35,Q:000001 (cl,P):(009,1.00) (013,0.00) (012,0.00)
R- 15 H:1.878e+00,Q:000023 tau:8.0 theta:[ 160.]
LG 14 H:1.602e+00,Q:000006 (cl,P):(011,0.33) (009,0.33) (010,0.17)
RG 14 H:1.559e+00,Q:000017 (cl,P):(009,0.53) (010,0.24) (008,0.12)
R- 17 H:2.294e+00,Q:000056 tau:13.0 theta:[ 22.]
L- 16 H:2.277e+00,Q:000041 tau:11.0 theta:[ 20.]
LG 15 H:8.042e-01,Q:000010 (cl,P):(010,0.80) (011,0.10) (008,0.10)
R- 15 H:2.380e+00,Q:000031 tau:10.0 theta:[ 154.]
LG 14 H:9.510e-01,Q:000005 (cl,P):(009,0.40) (011,0.20) (010,0.20)
R- 14 H:2.290e+00,Q:000026 tau:10.0 theta:[ 201.]
LG 13 H:1.951e+00,Q:000010 (cl,P):(013,0.30) (010,0.30) (011,0.20)
R- 13 H:1.712e+00,Q:000016 tau:12.0 theta:[ 154.]
LG 12 H:1.565e+00,Q:000010 (cl,P):(012,0.40) (010,0.30) (011,0.20)
R- 12 H:8.742e-01,Q:000006 tau:11.0 theta:[ 98.]
LG 11 H:9.183e-01,Q:000003 (cl,P):(011,0.67) (012,0.33) (013,0.00)
RG 11 H:6.667e-01,Q:000003 (cl,P):(013,0.33) (012,0.33) (011,0.33)
RG 16 H:9.592e-01,Q:000015 (cl,P):(013,0.73) (012,0.20) (011,0.07)


In [61]:
with open('CS213/dataset04.pic', 'rb') as pickleFile:
     data = pickle.load(pickleFile)
I=data[-2];L=data[-3]

In [62]:
I


Out[62]:
array([[ 0,  0,  0, ...,  0,  7,  0],
       [ 0,  0,  0, ...,  0, 13,  0],
       [ 0,  0,  0, ...,  0, 11,  0],
       ..., 
       [ 0,  0,  0, ...,  0,  7,  0],
       [ 0,  0,  0, ...,  0,  7,  0],
       [ 0,  0,  0, ...,  0,  5,  0]], dtype=int64)

In [63]:
L


Out[63]:
array([10, 13,  9, 11,  9,  8,  9,  9,  9,  6,  6,  9, 10,  7,  8, 10, 12,
       10,  9,  6,  9,  9,  8, 10,  8, 12,  9,  9,  9,  8,  9,  9,  6,  8,
        6,  9,  9, 12,  9, 11,  8,  9,  7,  9,  9,  6,  9,  8,  9, 10, 10,
        8,  9,  8, 13, 10,  6,  8,  8,  9,  7, 13,  7,  9,  9,  8,  9,  7,
       13, 13, 13, 11, 13, 11,  9,  9,  9,  7, 11,  9, 10,  9,  9,  9, 10,
       10, 10, 10, 11, 10,  5,  6,  8,  5,  5,  6,  7, 12, 11,  9,  9,  9,
        9,  9,  9, 12, 13,  5, 10, 11, 10, 12, 10,  9, 10,  9, 11,  8, 13,
       13, 11,  6,  9, 12,  9,  8,  8,  8, 10, 10,  8, 10, 13,  9,  9,  8,
       13,  8, 13,  8, 10,  8,  9,  6,  8, 11,  9,  9,  6,  5,  7,  9,  7], dtype=int64)

In [64]:
i=0
actual3=np.array(L)
predict3=np.zeros(len(L))
for i in xrange(len(L)):
    Prob=t.classify(I[i])
    predict3[i]=np.argmax(Prob)
   
    print "actual grade:%d predicted grade:%d"%(actual3[i],predict3[i])


actual grade:10 predicted grade:10
actual grade:13 predicted grade:11
actual grade:9 predicted grade:9
actual grade:11 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:6 predicted grade:9
actual grade:6 predicted grade:9
actual grade:9 predicted grade:9
actual grade:10 predicted grade:10
actual grade:7 predicted grade:9
actual grade:8 predicted grade:9
actual grade:10 predicted grade:9
actual grade:12 predicted grade:12
actual grade:10 predicted grade:10
actual grade:9 predicted grade:9
actual grade:6 predicted grade:8
actual grade:9 predicted grade:10
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:10 predicted grade:9
actual grade:8 predicted grade:9
actual grade:12 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:6
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:6 predicted grade:9
actual grade:8 predicted grade:8
actual grade:6 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:12 predicted grade:13
actual grade:9 predicted grade:9
actual grade:11 predicted grade:9
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:7 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:10
actual grade:6 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:10 predicted grade:10
actual grade:10 predicted grade:9
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:10
actual grade:13 predicted grade:10
actual grade:10 predicted grade:9
actual grade:6 predicted grade:9
actual grade:8 predicted grade:8
actual grade:8 predicted grade:7
actual grade:9 predicted grade:9
actual grade:7 predicted grade:9
actual grade:13 predicted grade:13
actual grade:7 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:7 predicted grade:7
actual grade:13 predicted grade:9
actual grade:13 predicted grade:13
actual grade:13 predicted grade:13
actual grade:11 predicted grade:12
actual grade:13 predicted grade:13
actual grade:11 predicted grade:10
actual grade:9 predicted grade:10
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:7 predicted grade:9
actual grade:11 predicted grade:8
actual grade:9 predicted grade:9
actual grade:10 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:10 predicted grade:9
actual grade:10 predicted grade:10
actual grade:10 predicted grade:10
actual grade:10 predicted grade:10
actual grade:11 predicted grade:9
actual grade:10 predicted grade:10
actual grade:5 predicted grade:9
actual grade:6 predicted grade:9
actual grade:8 predicted grade:9
actual grade:5 predicted grade:9
actual grade:5 predicted grade:9
actual grade:6 predicted grade:9
actual grade:7 predicted grade:9
actual grade:12 predicted grade:12
actual grade:11 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:12 predicted grade:11
actual grade:13 predicted grade:13
actual grade:5 predicted grade:9
actual grade:10 predicted grade:10
actual grade:11 predicted grade:6
actual grade:10 predicted grade:9
actual grade:12 predicted grade:12
actual grade:10 predicted grade:10
actual grade:9 predicted grade:9
actual grade:10 predicted grade:10
actual grade:9 predicted grade:10
actual grade:11 predicted grade:9
actual grade:8 predicted grade:8
actual grade:13 predicted grade:10
actual grade:13 predicted grade:13
actual grade:11 predicted grade:10
actual grade:6 predicted grade:9
actual grade:9 predicted grade:9
actual grade:12 predicted grade:13
actual grade:9 predicted grade:9
actual grade:8 predicted grade:8
actual grade:8 predicted grade:9
actual grade:8 predicted grade:9
actual grade:10 predicted grade:9
actual grade:10 predicted grade:9
actual grade:8 predicted grade:9
actual grade:10 predicted grade:9
actual grade:13 predicted grade:13
actual grade:9 predicted grade:10
actual grade:9 predicted grade:9
actual grade:8 predicted grade:10
actual grade:13 predicted grade:10
actual grade:8 predicted grade:8
actual grade:13 predicted grade:8
actual grade:8 predicted grade:10
actual grade:10 predicted grade:10
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:6 predicted grade:6
actual grade:8 predicted grade:9
actual grade:11 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:6 predicted grade:9
actual grade:5 predicted grade:8
actual grade:7 predicted grade:7
actual grade:9 predicted grade:9
actual grade:7 predicted grade:7

In [65]:
np.sqrt(np.sum((actual3-predict3)**2)/len(L))


Out[65]:
1.5801050680710416

In [66]:
from matplotlib import pyplot as plt
%pylab inline
plt.hist(actual3-predict3,30)


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['sum']
`%matplotlib` prevents importing * from pylab and numpy
Out[66]:
(array([  4.,   0.,   0.,  11.,   0.,   0.,  10.,   0.,   0.,   0.,  22.,
          0.,   0.,  76.,   0.,   0.,  14.,   0.,   0.,   0.,   8.,   0.,
          0.,   5.,   0.,   0.,   1.,   0.,   0.,   2.]),
 array([-4. , -3.7, -3.4, -3.1, -2.8, -2.5, -2.2, -1.9, -1.6, -1.3, -1. ,
        -0.7, -0.4, -0.1,  0.2,  0.5,  0.8,  1.1,  1.4,  1.7,  2. ,  2.3,
         2.6,  2.9,  3.2,  3.5,  3.8,  4.1,  4.4,  4.7,  5. ]),
 <a list of 30 Patch objects>)

In [67]:
n = 14
cm3 = np.zeros((n,n))
for i in xrange(len(actual3)):
    cm3[predict3[i],actual3[i]] +=1
print cm3


[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   1.   0.   1.   0.   0.   1.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   3.   1.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   1.   1.   0.   5.   0.   0.   1.   0.   1.]
 [  0.   0.   0.   0.   0.   4.  10.   6.  14.  45.  10.   6.   1.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   3.   5.  12.   2.   0.   3.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   3.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   2.   7.]]

In [68]:
#find TP
from collections import defaultdict
tp = defaultdict(list)
fp = defaultdict(list)
for i in range(0,14):
    for j in range(0,14):
        if i == j: 
            tp[i] = cm3[i,j]
            TP = tp[i]+tp[i-1]+tp[i-2]+tp[i-3]+tp[i-4]+tp[i-5]+tp[i-6]+tp[i-7]+tp[i-8]+tp[i-9]+tp[i-10]+tp[i-11]+tp[i-12]+tp[i-13]
        
            
print "TP is %f"%(TP)
print "--------------------------------------------"
sum = cm3.sum()
FP = sum-TP
print "FP is %f"%(FP)

precision = TP/(FP+TP)
print "precision is %f"%(precision)


TP is 76.000000
--------------------------------------------
FP is 77.000000
precision is 0.496732

Training(4)


In [69]:
import pickle
from pforest.master import master
m=master(dsetname='CS213')
m.reset()
m.train()
with open('CS213/out_tree4.pic', 'wb') as pickleFile:
    pickle.dump(m.root, pickleFile, pickle.HIGHEST_PROTOCOL)


master>>init() dsetname: CS213
master>> create dview
master>> init engine
Found pforest
debug:master:__init__: ['CS213/dataset00.pic', 'CS213/dataset01.pic']
master>> init local variables
master>>reset()
master>>reset() H: 2.8089
master>>reset() Q: 00306
master::train() node20*-
master::train() node19R-
master::train() node18R-
master::train() node17R-
master::train() node16RG
master::train() node16LG
master::train() node17LG
master::train() node18L-
master::train() node17R-
master::train() node16RG
master::train() node16LQ
master::train() node17L-
master::train() node16RG
master::train() node16L-
master::train() node15RG
master::train() node15LQ
master::train() node19L-
master::train() node18R-
master::train() node17R-
master::train() node16R-
master::train() node15R-
master::train() node14R-
master::train() node13RG
master::train() node13LQ
master::train() node14L-
master::train() node13RG
master::train() node13L-
master::train() node12RQ
master::train() node12L-
master::train() node11RG
master::train() node11LG
master::train() node15L-
master::train() node14R-
master::train() node13RG
master::train() node13LG
master::train() node14LG
master::train() node16L-
master::train() node15RG
master::train() node15L-
master::train() node14R-
master::train() node13RG
master::train() node13LQ
master::train() node14LG
master::train() node17L-
master::train() node16RG
master::train() node16L-
master::train() node15R-
master::train() node14RG
master::train() node14LQ
master::train() node15LG
master::train() node18L-
master::train() node17R-
master::train() node16RG
master::train() node16L-
master::train() node15RG
master::train() node15L-
master::train() node14R-
master::train() node13RG
master::train() node13L-
master::train() node12RG
master::train() node12LG
master::train() node14L-
master::train() node13R-
master::train() node12RQ
master::train() node12LG
master::train() node13L-
master::train() node12RG
master::train() node12L-
master::train() node11RG
master::train() node11LG
master::train() node17L-
master::train() node16RG
master::train() node16L-
master::train() node15R-
master::train() node14RG
master::train() node14LG
master::train() node15LG

In [70]:
from pforest.dataset import dataset
from pforest.tree import tree
with open('CS213/out_tree4.pic', 'rb') as pickleFile:
    root = pickle.load(pickleFile)
#init the test tree
t=tree()
t.settree(root)
t.show()


*- 20 H:2.809e+00,Q:000306 tau:10.0 theta:[ 22.]
L- 19 H:2.603e+00,Q:000216 tau:7.0 theta:[ 35.]
L- 18 H:2.342e+00,Q:000148 tau:7.0 theta:[ 201.]
L- 17 H:2.111e+00,Q:000028 tau:12.0 theta:[ 196.]
L- 16 H:1.708e+00,Q:000012 tau:10.0 theta:[ 162.]
LG 15 H:9.183e-01,Q:000006 (cl,P):(007,0.33) (005,0.33) (011,0.17)
R- 15 H:1.602e+00,Q:000006 tau:8.0 theta:[ 20.]
LG 14 H:9.183e-01,Q:000003 (cl,P):(009,0.67) (005,0.33) (013,0.00)
RG 14 H:6.667e-01,Q:000003 (cl,P):(010,0.33) (008,0.33) (007,0.33)
RG 16 H:1.732e+00,Q:000016 (cl,P):(008,0.38) (007,0.25) (009,0.19)
R- 17 H:2.247e+00,Q:000120 tau:11.0 theta:[ 19.]
L- 16 H:2.297e+00,Q:000105 tau:10.0 theta:[ 20.]
L- 15 H:2.181e+00,Q:000085 tau:9.0 theta:[ 98.]
L- 14 H:2.087e+00,Q:000034 tau:10.0 theta:[ 160.]
L- 13 H:1.752e+00,Q:000025 tau:12.0 theta:[ 196.]
L- 12 H:1.515e+00,Q:000013 tau:11.0 theta:[ 26.]
LG 11 H:1.200e+00,Q:000010 (cl,P):(009,0.60) (008,0.20) (005,0.20)
RG 11 H:1.585e+00,Q:000003 (cl,P):(009,0.33) (006,0.33) (005,0.33)
RG 12 H:1.366e+00,Q:000012 (cl,P):(009,0.42) (006,0.25) (008,0.17)
R- 13 H:2.183e+00,Q:000009 tau:9.0 theta:[ 125.]
LG 12 H:1.939e+00,Q:000008 (cl,P):(008,0.38) (006,0.25) (010,0.12)
RQ 12 H:1.925e-35,Q:000001 (cl,P):(009,1.00) (013,0.00) (012,0.00)
R- 14 H:2.011e+00,Q:000051 tau:9.0 theta:[ 124.]
L- 13 H:1.946e+00,Q:000044 tau:5.0 theta:[ 35.]
LG 12 H:1.867e+00,Q:000042 (cl,P):(009,0.40) (008,0.36) (007,0.10)
RG 12 H:1.000e+00,Q:000002 (cl,P):(006,0.50) (005,0.50) (013,0.00)
RG 13 H:1.265e+00,Q:000007 (cl,P):(009,0.57) (010,0.14) (008,0.14)
RG 15 H:1.970e+00,Q:000020 (cl,P):(009,0.35) (008,0.25) (011,0.10)
RG 16 H:1.209e+00,Q:000015 (cl,P):(009,0.67) (008,0.20) (013,0.07)
R- 18 H:2.504e+00,Q:000068 tau:9.0 theta:[ 154.]
L- 17 H:1.601e+00,Q:000011 tau:12.0 theta:[ 179.]
L- 16 H:7.552e-01,Q:000008 tau:11.0 theta:[ 189.]
LG 15 H:1.000e+00,Q:000002 (cl,P):(012,0.50) (010,0.50) (013,0.00)
R- 15 H:6.016e-01,Q:000006 tau:9.0 theta:[ 39.]
LQ 14 H:1.925e-35,Q:000001 (cl,P):(010,1.00) (013,0.00) (012,0.00)
RG 14 H:6.490e-01,Q:000005 (cl,P):(010,0.80) (012,0.20) (013,0.00)
RG 16 H:9.183e-01,Q:000003 (cl,P):(008,0.67) (005,0.33) (013,0.00)
R- 17 H:2.333e+00,Q:000057 tau:9.0 theta:[ 31.]
L- 16 H:1.686e+00,Q:000015 tau:11.0 theta:[ 182.]
L- 15 H:1.523e+00,Q:000011 tau:7.0 theta:[ 201.]
LG 14 H:1.252e+00,Q:000006 (cl,P):(008,0.67) (010,0.17) (006,0.17)
R- 14 H:9.510e-01,Q:000005 tau:13.0 theta:[ 26.]
LQ 13 H:1.925e-35,Q:000001 (cl,P):(009,1.00) (013,0.00) (012,0.00)
RG 13 H:1.000e+00,Q:000004 (cl,P):(009,0.50) (010,0.25) (008,0.25)
RG 15 H:6.887e-01,Q:000004 (cl,P):(008,0.50) (009,0.25) (007,0.25)
R- 16 H:2.019e+00,Q:000042 tau:9.0 theta:[ 98.]
L- 15 H:1.291e+00,Q:000018 tau:8.0 theta:[ 39.]
LG 14 H:6.824e-01,Q:000009 (cl,P):(009,0.78) (011,0.11) (010,0.11)
R- 14 H:1.428e+00,Q:000009 tau:12.0 theta:[ 7.]
LG 13 H:1.000e+00,Q:000004 (cl,P):(011,0.25) (010,0.25) (009,0.25)
RG 13 H:4.000e-01,Q:000005 (cl,P):(009,0.80) (011,0.20) (013,0.00)
R- 15 H:1.818e+00,Q:000024 tau:12.0 theta:[ 140.]
L- 14 H:1.526e+00,Q:000020 tau:10.0 theta:[ 64.]
L- 13 H:1.295e+00,Q:000018 tau:11.0 theta:[ 140.]
L- 12 H:1.115e+00,Q:000017 tau:9.0 theta:[ 34.]
LG 11 H:1.289e-35,Q:000003 (cl,P):(010,1.00) (013,0.00) (012,0.00)
RG 11 H:9.360e-01,Q:000014 (cl,P):(011,0.50) (010,0.21) (009,0.21)
RQ 12 H:1.925e-35,Q:000001 (cl,P):(012,1.00) (013,0.00) (011,0.00)
RG 13 H:9.704e-36,Q:000002 (cl,P):(013,1.00) (012,0.00) (011,0.00)
R- 14 H:1.000e+00,Q:000004 tau:7.0 theta:[ 47.]
LQ 13 H:1.925e-35,Q:000001 (cl,P):(006,1.00) (013,0.00) (012,0.00)
RG 13 H:6.667e-01,Q:000003 (cl,P):(011,0.33) (010,0.33) (009,0.33)
R- 19 H:2.485e+00,Q:000090 tau:9.0 theta:[ 201.]
L- 18 H:2.120e+00,Q:000029 tau:10.0 theta:[ 102.]
L- 17 H:1.826e+00,Q:000023 tau:7.0 theta:[ 125.]
L- 16 H:1.438e+00,Q:000012 tau:10.0 theta:[ 189.]
LQ 15 H:1.925e-35,Q:000001 (cl,P):(008,1.00) (013,0.00) (012,0.00)
RG 15 H:1.241e+00,Q:000011 (cl,P):(010,0.36) (009,0.36) (007,0.18)
RG 16 H:1.495e+00,Q:000011 (cl,P):(010,0.45) (009,0.36) (011,0.18)
R- 17 H:1.602e+00,Q:000006 tau:7.0 theta:[ 25.]
LQ 16 H:1.925e-35,Q:000001 (cl,P):(008,1.00) (013,0.00) (012,0.00)
RG 16 H:1.200e+00,Q:000005 (cl,P):(009,0.60) (013,0.20) (012,0.20)
R- 18 H:2.286e+00,Q:000061 tau:12.0 theta:[ 189.]
LG 17 H:2.138e+00,Q:000028 (cl,P):(013,0.25) (012,0.25) (009,0.25)
R- 17 H:2.199e+00,Q:000033 tau:13.0 theta:[ 196.]
LG 16 H:2.145e+00,Q:000028 (cl,P):(013,0.29) (011,0.29) (009,0.18)
RG 16 H:1.351e+00,Q:000005 (cl,P):(010,0.40) (013,0.20) (012,0.20)


In [71]:
with open('CS213/dataset04.pic', 'rb') as pickleFile:
     data = pickle.load(pickleFile)
I=data[-2];L=data[-3]

In [72]:
I


Out[72]:
array([[0, 0, 0, ..., 0, 9, 0],
       [0, 0, 0, ..., 0, 7, 0],
       [0, 0, 0, ..., 0, 7, 0],
       ..., 
       [0, 0, 0, ..., 0, 6, 0],
       [0, 0, 0, ..., 0, 6, 0],
       [0, 0, 0, ..., 0, 7, 0]], dtype=int64)

In [73]:
L


Out[73]:
array([ 9,  8,  6, 10,  6,  9,  9,  5,  8,  9,  6, 12,  8, 11,  9,  5,  7,
        9,  8,  9, 10,  9,  9,  9,  7, 10, 13,  9, 12,  9, 10, 10, 11,  5,
        8, 11,  9, 12, 12,  9, 11, 12,  9,  8, 11,  8,  9,  7, 10, 12, 13,
       13, 13,  9,  9,  9,  6,  8,  9,  8, 12,  5,  5,  6,  8, 11,  9, 13,
       11,  8, 11, 10, 10,  9, 11, 10, 10,  8,  9,  9, 13,  9,  7, 10, 12,
        9,  5,  5,  9, 11,  9,  8, 11,  8, 10,  9,  8,  9,  8,  9,  7, 10,
        7, 10, 10, 10,  9,  9,  9,  7, 11,  9,  7,  7, 10, 11, 11,  6,  9,
        9,  8,  9,  9,  8, 12,  9,  9,  9,  7,  9,  8,  8,  5,  8, 13,  8,
       11,  9,  8, 10,  8, 10,  9,  8, 11, 10,  9,  9,  9,  9,  8,  9,  8], dtype=int64)

In [74]:
i=0
actual4=np.array(L)
predict4=np.zeros(len(L))
for i in xrange(len(L)):
    Prob=t.classify(I[i])
    predict4[i]=np.argmax(Prob)
   
    print "actual grade:%d predicted grade:%d"%(actual4[i],predict4[i])


actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:6 predicted grade:6
actual grade:10 predicted grade:9
actual grade:6 predicted grade:8
actual grade:9 predicted grade:9
actual grade:9 predicted grade:5
actual grade:5 predicted grade:9
actual grade:8 predicted grade:8
actual grade:9 predicted grade:9
actual grade:6 predicted grade:9
actual grade:12 predicted grade:9
actual grade:8 predicted grade:9
actual grade:11 predicted grade:11
actual grade:9 predicted grade:9
actual grade:5 predicted grade:8
actual grade:7 predicted grade:8
actual grade:9 predicted grade:9
actual grade:8 predicted grade:8
actual grade:9 predicted grade:9
actual grade:10 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:7 predicted grade:9
actual grade:10 predicted grade:11
actual grade:13 predicted grade:9
actual grade:9 predicted grade:9
actual grade:12 predicted grade:9
actual grade:9 predicted grade:9
actual grade:10 predicted grade:9
actual grade:10 predicted grade:8
actual grade:11 predicted grade:11
actual grade:5 predicted grade:9
actual grade:8 predicted grade:8
actual grade:11 predicted grade:5
actual grade:9 predicted grade:9
actual grade:12 predicted grade:9
actual grade:12 predicted grade:11
actual grade:9 predicted grade:9
actual grade:11 predicted grade:11
actual grade:12 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:11 predicted grade:9
actual grade:8 predicted grade:9
actual grade:9 predicted grade:11
actual grade:7 predicted grade:9
actual grade:10 predicted grade:10
actual grade:12 predicted grade:9
actual grade:13 predicted grade:10
actual grade:13 predicted grade:11
actual grade:13 predicted grade:11
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:6 predicted grade:9
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:8
actual grade:12 predicted grade:10
actual grade:5 predicted grade:9
actual grade:5 predicted grade:5
actual grade:6 predicted grade:5
actual grade:8 predicted grade:9
actual grade:11 predicted grade:11
actual grade:9 predicted grade:9
actual grade:13 predicted grade:9
actual grade:11 predicted grade:11
actual grade:8 predicted grade:9
actual grade:11 predicted grade:11
actual grade:10 predicted grade:9
actual grade:10 predicted grade:7
actual grade:9 predicted grade:9
actual grade:11 predicted grade:11
actual grade:10 predicted grade:9
actual grade:10 predicted grade:9
actual grade:8 predicted grade:8
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:13 predicted grade:11
actual grade:9 predicted grade:9
actual grade:7 predicted grade:9
actual grade:10 predicted grade:10
actual grade:12 predicted grade:9
actual grade:9 predicted grade:9
actual grade:5 predicted grade:5
actual grade:5 predicted grade:5
actual grade:9 predicted grade:9
actual grade:11 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:11 predicted grade:11
actual grade:8 predicted grade:9
actual grade:10 predicted grade:10
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:7 predicted grade:7
actual grade:10 predicted grade:10
actual grade:7 predicted grade:9
actual grade:10 predicted grade:10
actual grade:10 predicted grade:9
actual grade:10 predicted grade:11
actual grade:9 predicted grade:9
actual grade:9 predicted grade:5
actual grade:9 predicted grade:9
actual grade:7 predicted grade:8
actual grade:11 predicted grade:11
actual grade:9 predicted grade:9
actual grade:7 predicted grade:9
actual grade:7 predicted grade:9
actual grade:10 predicted grade:10
actual grade:11 predicted grade:9
actual grade:11 predicted grade:11
actual grade:6 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:12 predicted grade:10
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:7 predicted grade:8
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:8 predicted grade:8
actual grade:5 predicted grade:8
actual grade:8 predicted grade:8
actual grade:13 predicted grade:9
actual grade:8 predicted grade:9
actual grade:11 predicted grade:11
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:10 predicted grade:10
actual grade:8 predicted grade:8
actual grade:10 predicted grade:5
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:11 predicted grade:11
actual grade:10 predicted grade:10
actual grade:9 predicted grade:8
actual grade:9 predicted grade:9
actual grade:9 predicted grade:8
actual grade:9 predicted grade:9
actual grade:8 predicted grade:8
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9

In [75]:
np.sqrt(np.sum((actual4-predict4)**2)/len(L))


Out[75]:
1.6329931618554521

In [76]:
from matplotlib import pyplot as plt
%pylab inline
plt.hist(actual4-predict4,50)


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['sum']
`%matplotlib` prevents importing * from pylab and numpy
Out[76]:
(array([  3.,   0.,   0.,   0.,   0.,   5.,   0.,   0.,   0.,   0.,   8.,
          0.,   0.,   0.,   0.,  23.,   0.,   0.,   0.,   0.,  79.,   0.,
          0.,   0.,   0.,  11.,   0.,   0.,   0.,   0.,   9.,   0.,   0.,
          0.,   0.,   8.,   0.,   0.,   0.,   0.,   5.,   0.,   0.,   0.,
          0.,   1.,   0.,   0.,   0.,   1.]),
 array([-4. , -3.8, -3.6, -3.4, -3.2, -3. , -2.8, -2.6, -2.4, -2.2, -2. ,
        -1.8, -1.6, -1.4, -1.2, -1. , -0.8, -0.6, -0.4, -0.2,  0. ,  0.2,
         0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8,  2. ,  2.2,  2.4,
         2.6,  2.8,  3. ,  3.2,  3.4,  3.6,  3.8,  4. ,  4.2,  4.4,  4.6,
         4.8,  5. ,  5.2,  5.4,  5.6,  5.8,  6. ]),
 <a list of 50 Patch objects>)

In [77]:
n = 14
cm4 = np.zeros((n,n))
for i in xrange(len(actual4)):
    cm4[predict4[i],actual4[i]] +=1
print cm4


[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   3.   1.   0.   0.   2.   1.   1.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   1.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   1.   0.   0.   1.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   2.   1.   3.   9.   2.   1.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   3.   3.   6.  18.  45.   7.   3.   6.   3.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   8.   0.   2.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   2.  12.   1.   3.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]]

In [78]:
#find TP
from collections import defaultdict
tp = defaultdict(list)
fp = defaultdict(list)
for i in range(0,14):
    for j in range(0,14):
        if i == j: 
            tp[i] = cm4[i,j]
            TP = tp[i]+tp[i-1]+tp[i-2]+tp[i-3]+tp[i-4]+tp[i-5]+tp[i-6]+tp[i-7]+tp[i-8]+tp[i-9]+tp[i-10]+tp[i-11]+tp[i-12]+tp[i-13]
        
            
print "TP is %f"%(TP)
print "--------------------------------------------"
sum = cm4.sum()
FP = sum-TP
print "FP is %f"%(FP)

precision = TP/(FP+TP)
print "precision is %f"%(precision)


TP is 79.000000
--------------------------------------------
FP is 74.000000
precision is 0.516340

Traing(5)


In [79]:
import pickle
from pforest.master import master
m=master(dsetname='CS213')
m.reset()
m.train()
with open('CS213/out_tree5.pic', 'wb') as pickleFile:
    pickle.dump(m.root, pickleFile, pickle.HIGHEST_PROTOCOL)


master>>init() dsetname: CS213
master>> create dview
master>> init engine
Found pforest
debug:master:__init__: ['CS213/dataset00.pic', 'CS213/dataset01.pic']
master>> init local variables
master>>reset()
master>>reset() H: 2.8737
master>>reset() Q: 00306
master::train() node20*-
master::train() node19R-
master::train() node18R-
master::train() node17RG
master::train() node17L-
master::train() node16RG
master::train() node16LQ
master::train() node18L-
master::train() node17RG
master::train() node17L-
master::train() node16RG
master::train() node16LG
master::train() node19L-
master::train() node18R-
master::train() node17R-
master::train() node16R-
master::train() node15R-
master::train() node14R-
master::train() node13R-
master::train() node12R-
master::train() node11RG
master::train() node11L-
master::train() node10RG
master::train() node10LG
master::train() node12LQ
master::train() node13L-
master::train() node12RQ
master::train() node12LG
master::train() node14LG
master::train() node15L-
master::train() node14RG
master::train() node14LQ
master::train() node16LG
master::train() node17L-
master::train() node16R-
master::train() node15R-
master::train() node14RG
master::train() node14LG
master::train() node15L-
master::train() node14R-
master::train() node13R-
master::train() node12RG
master::train() node12LG
master::train() node13LG
master::train() node14L-
master::train() node13RG
master::train() node13LQ
master::train() node16L-
master::train() node15R-
master::train() node14RQ
master::train() node14LG
master::train() node15LG
master::train() node18L-
master::train() node17R-
master::train() node16RG
master::train() node16L-
master::train() node15R-
master::train() node14R-
master::train() node13R-
master::train() node12RG
master::train() node12L-
master::train() node11R-
master::train() node10RG
master::train() node10LG
master::train() node11LG
master::train() node13LG
master::train() node14LG
master::train() node15L-
master::train() node14RG
master::train() node14LG
master::train() node17L-
master::train() node16R-
master::train() node15RG
master::train() node15LG
master::train() node16L-
master::train() node15R-
master::train() node14R-
master::train() node13RG
master::train() node13L-
master::train() node12RG
master::train() node12L-
master::train() node11R-
master::train() node10R-
master::train() node9RG
master::train() node9LG
master::train() node10LQ
master::train() node11LG
master::train() node14LG
master::train() node15L-
master::train() node14R-
master::train() node13R-
master::train() node12RG
master::train() node12LG
master::train() node13LG
master::train() node14L-
master::train() node13RG
master::train() node13LG

In [80]:
from pforest.dataset import dataset
from pforest.tree import tree
with open('CS213/out_tree5.pic', 'rb') as pickleFile:
    root = pickle.load(pickleFile)
#init the test tree
t=tree()
t.settree(root)
t.show()


*- 20 H:2.874e+00,Q:000306 tau:11.0 theta:[ 22.]
L- 19 H:2.740e+00,Q:000270 tau:9.0 theta:[ 31.]
L- 18 H:2.575e+00,Q:000198 tau:9.0 theta:[ 22.]
L- 17 H:2.399e+00,Q:000120 tau:13.0 theta:[ 178.]
L- 16 H:2.292e+00,Q:000092 tau:10.0 theta:[ 19.]
L- 15 H:2.301e+00,Q:000063 tau:9.0 theta:[ 160.]
L- 14 H:1.817e+00,Q:000021 tau:8.0 theta:[ 20.]
LG 13 H:7.763e-36,Q:000005 (cl,P):(009,0.80) (008,0.20) (013,0.00)
RG 13 H:1.601e+00,Q:000016 (cl,P):(008,0.38) (006,0.25) (007,0.19)
R- 14 H:2.256e+00,Q:000042 tau:10.0 theta:[ 160.]
LG 13 H:2.158e+00,Q:000025 (cl,P):(007,0.28) (008,0.24) (009,0.20)
R- 13 H:1.880e+00,Q:000017 tau:9.0 theta:[ 19.]
LG 12 H:1.000e+00,Q:000002 (cl,P):(007,0.50) (005,0.50) (013,0.00)
RG 12 H:1.763e+00,Q:000015 (cl,P):(008,0.40) (009,0.33) (010,0.13)
R- 15 H:1.790e+00,Q:000029 tau:7.0 theta:[ 22.]
LG 14 H:6.667e-01,Q:000003 (cl,P):(009,0.33) (007,0.33) (005,0.33)
R- 14 H:1.693e+00,Q:000026 tau:9.0 theta:[ 199.]
L- 13 H:1.308e+00,Q:000022 tau:7.0 theta:[ 125.]
L- 12 H:1.397e+00,Q:000013 tau:9.0 theta:[ 162.]
LG 11 H:6.490e-01,Q:000005 (cl,P):(009,0.60) (008,0.40) (013,0.00)
R- 11 H:1.201e+00,Q:000008 tau:9.0 theta:[ 160.]
LQ 10 H:1.925e-35,Q:000001 (cl,P):(005,1.00) (013,0.00) (012,0.00)
R- 10 H:8.571e-01,Q:000007 tau:11.0 theta:[ 196.]
LG 09 H:1.585e+00,Q:000003 (cl,P):(009,0.33) (008,0.33) (007,0.33)
RG 09 H:9.688e-36,Q:000004 (cl,P):(009,1.00) (013,0.00) (012,0.00)
RG 12 H:6.713e-01,Q:000009 (cl,P):(009,0.56) (008,0.44) (013,0.00)
RG 13 H:1.189e+00,Q:000004 (cl,P):(011,0.25) (010,0.25) (008,0.25)
R- 16 H:2.209e+00,Q:000028 tau:9.0 theta:[ 35.]
LG 15 H:2.129e+00,Q:000019 (cl,P):(005,0.37) (008,0.21) (009,0.16)
RG 15 H:1.556e+00,Q:000009 (cl,P):(007,0.44) (010,0.22) (008,0.22)
R- 17 H:2.375e+00,Q:000078 tau:9.0 theta:[ 199.]
L- 16 H:2.310e+00,Q:000065 tau:10.0 theta:[ 196.]
L- 15 H:7.616e-01,Q:000009 tau:11.0 theta:[ 160.]
LG 14 H:4.591e-01,Q:000006 (cl,P):(009,0.83) (010,0.17) (013,0.00)
RG 14 H:6.667e-01,Q:000003 (cl,P):(009,0.67) (012,0.33) (013,0.00)
R- 15 H:2.418e+00,Q:000056 tau:9.0 theta:[ 162.]
LG 14 H:6.667e-01,Q:000003 (cl,P):(012,0.33) (011,0.33) (008,0.33)
R- 14 H:2.290e+00,Q:000053 tau:9.0 theta:[ 160.]
LG 13 H:1.961e+00,Q:000009 (cl,P):(009,0.33) (010,0.22) (008,0.22)
R- 13 H:2.279e+00,Q:000044 tau:12.0 theta:[ 189.]
L- 12 H:2.276e+00,Q:000036 tau:10.0 theta:[ 189.]
LG 11 H:5.000e-01,Q:000004 (cl,P):(010,0.75) (009,0.25) (013,0.00)
R- 11 H:2.244e+00,Q:000032 tau:7.0 theta:[ 125.]
LG 10 H:2.081e+00,Q:000024 (cl,P):(009,0.46) (008,0.21) (011,0.08)
RG 10 H:1.201e+00,Q:000008 (cl,P):(010,0.38) (009,0.25) (013,0.12)
RG 12 H:1.594e+00,Q:000008 (cl,P):(009,0.50) (013,0.12) (011,0.12)
RG 16 H:2.094e+00,Q:000013 (cl,P):(009,0.31) (010,0.23) (008,0.23)
R- 18 H:2.435e+00,Q:000072 tau:10.0 theta:[ 35.]
L- 17 H:1.893e+00,Q:000030 tau:8.0 theta:[ 201.]
L- 16 H:1.251e+00,Q:000007 tau:8.0 theta:[ 35.]
LG 15 H:6.499e-36,Q:000003 (cl,P):(009,1.00) (013,0.00) (012,0.00)
R- 15 H:6.887e-01,Q:000004 tau:10.0 theta:[ 31.]
LG 14 H:6.667e-01,Q:000003 (cl,P):(010,0.67) (006,0.33) (013,0.00)
RQ 14 H:1.925e-35,Q:000001 (cl,P):(010,1.00) (013,0.00) (012,0.00)
R- 16 H:1.819e+00,Q:000023 tau:10.0 theta:[ 55.]
L- 15 H:1.826e+00,Q:000015 tau:9.0 theta:[ 32.]
L- 14 H:1.189e+00,Q:000004 tau:9.0 theta:[ 55.]
LQ 13 H:1.925e-35,Q:000002 (cl,P):(011,0.50) (009,0.50) (013,0.00)
RG 13 H:1.000e+00,Q:000002 (cl,P):(010,0.50) (009,0.50) (013,0.00)
R- 14 H:1.670e+00,Q:000011 tau:8.0 theta:[ 196.]
LG 13 H:6.667e-01,Q:000003 (cl,P):(013,0.33) (011,0.33) (009,0.33)
R- 13 H:1.201e+00,Q:000008 tau:9.0 theta:[ 154.]
LG 12 H:1.289e-35,Q:000003 (cl,P):(010,1.00) (013,0.00) (012,0.00)
RG 12 H:9.510e-01,Q:000005 (cl,P):(009,0.60) (010,0.20) (007,0.20)
R- 15 H:9.387e-01,Q:000008 tau:11.0 theta:[ 6.]
LG 14 H:5.000e-01,Q:000004 (cl,P):(009,0.50) (011,0.25) (010,0.25)
RG 14 H:4.890e-36,Q:000004 (cl,P):(011,1.00) (013,0.00) (012,0.00)
R- 17 H:2.407e+00,Q:000042 tau:9.0 theta:[ 154.]
LG 16 H:1.644e+00,Q:000007 (cl,P):(012,0.29) (010,0.29) (008,0.29)
R- 16 H:2.163e+00,Q:000035 tau:10.0 theta:[ 102.]
L- 15 H:1.333e+00,Q:000006 tau:7.0 theta:[ 47.]
LQ 14 H:1.925e-35,Q:000001 (cl,P):(010,1.00) (013,0.00) (012,0.00)
RG 14 H:1.200e+00,Q:000005 (cl,P):(011,0.40) (008,0.40) (007,0.20)
R- 15 H:2.090e+00,Q:000029 tau:9.0 theta:[ 162.]
LG 14 H:1.206e+00,Q:000008 (cl,P):(010,0.62) (008,0.25) (012,0.12)
R- 14 H:1.724e+00,Q:000021 tau:9.0 theta:[ 198.]
L- 13 H:6.793e-01,Q:000007 tau:11.0 theta:[ 6.]
LG 12 H:4.000e-01,Q:000005 (cl,P):(011,0.80) (009,0.20) (013,0.00)
RQ 12 H:1.925e-35,Q:000002 (cl,P):(011,0.50) (010,0.50) (013,0.00)
R- 13 H:1.753e+00,Q:000014 tau:8.0 theta:[ 196.]
LQ 12 H:1.925e-35,Q:000001 (cl,P):(012,1.00) (013,0.00) (011,0.00)
R- 12 H:1.728e+00,Q:000013 tau:9.0 theta:[ 51.]
L- 11 H:1.636e+00,Q:000011 tau:11.0 theta:[ 189.]
LG 10 H:1.200e+00,Q:000005 (cl,P):(011,0.60) (012,0.20) (009,0.20)
RG 10 H:1.333e+00,Q:000006 (cl,P):(011,0.33) (008,0.33) (012,0.17)
RG 11 H:9.704e-36,Q:000002 (cl,P):(009,1.00) (013,0.00) (012,0.00)
R- 19 H:2.095e+00,Q:000036 tau:13.0 theta:[ 20.]
L- 18 H:2.061e+00,Q:000021 tau:11.0 theta:[ 191.]
L- 17 H:1.828e+00,Q:000016 tau:12.0 theta:[ 196.]
LG 16 H:1.758e+00,Q:000013 (cl,P):(011,0.31) (012,0.23) (013,0.15)
RG 16 H:9.183e-01,Q:000003 (cl,P):(009,0.67) (010,0.33) (013,0.00)
RG 17 H:1.200e+00,Q:000005 (cl,P):(013,0.40) (010,0.40) (011,0.20)
R- 18 H:1.260e+00,Q:000015 tau:13.0 theta:[ 22.]
L- 17 H:6.490e-01,Q:000005 tau:8.0 theta:[ 125.]
LQ 16 H:1.925e-35,Q:000002 (cl,P):(013,0.50) (011,0.50) (012,0.00)
RG 16 H:6.499e-36,Q:000003 (cl,P):(012,1.00) (013,0.00) (011,0.00)
RG 17 H:1.200e+00,Q:000010 (cl,P):(013,0.60) (012,0.30) (011,0.10)


In [81]:
with open('CS213/dataset04.pic', 'rb') as pickleFile:
     data = pickle.load(pickleFile)
I=data[-2];L=data[-3]

In [82]:
I


Out[82]:
array([[ 0,  0,  0, ...,  0,  7,  0],
       [ 0,  0,  0, ...,  0, 13,  0],
       [ 0,  0,  0, ...,  0, 10,  0],
       ..., 
       [ 0,  0,  0, ...,  0,  8,  0],
       [ 0,  0,  0, ...,  0,  8,  0],
       [ 0,  0,  0, ...,  0,  9,  0]], dtype=int64)

In [83]:
L


Out[83]:
array([ 9, 10,  8, 10,  9,  5,  7,  9,  6,  6,  8, 11,  7, 11,  9,  8, 12,
        9,  6, 11,  9,  5,  8, 11, 13, 11,  7, 12,  7, 12, 12, 12,  8,  9,
        7,  7,  9,  9,  7, 11, 13, 13,  9,  9, 13, 10, 10, 10,  8, 11,  8,
       13,  9,  9,  9, 13, 11, 13,  8, 10,  7,  8,  9, 12, 12, 13,  9, 10,
        8,  9,  8, 12,  5,  9, 10, 12, 11, 10, 11,  9, 10,  7,  8,  9,  9,
        6, 10,  7, 10,  9, 11,  5,  7,  8, 10, 10, 11,  5,  7,  9,  8,  6,
       10,  8, 10,  8,  9,  8,  6,  9,  8, 11,  5,  7,  7,  8,  8, 11,  9,
        9, 11,  9, 10,  8, 10,  7,  5,  9,  9,  9,  5,  7,  8,  8, 10,  8,
       10,  8, 11,  5,  9, 10,  8, 10,  9,  9,  7, 10,  9, 10,  7, 11,  9], dtype=int64)

In [84]:
i=0
actual5=np.array(L)
predict5=np.zeros(len(L))
for i in xrange(len(L)):
    Prob=t.classify(I[i])
    predict5[i]=np.argmax(Prob)
   
    print "actual grade:%d predicted grade:%d"%(actual5[i],predict5[i])


actual grade:9 predicted grade:8
actual grade:10 predicted grade:11
actual grade:8 predicted grade:7
actual grade:10 predicted grade:10
actual grade:9 predicted grade:9
actual grade:5 predicted grade:8
actual grade:7 predicted grade:8
actual grade:9 predicted grade:8
actual grade:6 predicted grade:8
actual grade:6 predicted grade:8
actual grade:8 predicted grade:8
actual grade:11 predicted grade:11
actual grade:7 predicted grade:7
actual grade:11 predicted grade:11
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:12 predicted grade:12
actual grade:9 predicted grade:9
actual grade:6 predicted grade:9
actual grade:11 predicted grade:11
actual grade:9 predicted grade:9
actual grade:5 predicted grade:5
actual grade:8 predicted grade:9
actual grade:11 predicted grade:10
actual grade:13 predicted grade:13
actual grade:11 predicted grade:9
actual grade:7 predicted grade:5
actual grade:12 predicted grade:8
actual grade:7 predicted grade:9
actual grade:12 predicted grade:11
actual grade:12 predicted grade:12
actual grade:12 predicted grade:13
actual grade:8 predicted grade:7
actual grade:9 predicted grade:11
actual grade:7 predicted grade:7
actual grade:7 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:7 predicted grade:7
actual grade:11 predicted grade:11
actual grade:13 predicted grade:11
actual grade:13 predicted grade:13
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:13 predicted grade:11
actual grade:10 predicted grade:11
actual grade:10 predicted grade:9
actual grade:10 predicted grade:9
actual grade:8 predicted grade:9
actual grade:11 predicted grade:8
actual grade:8 predicted grade:7
actual grade:13 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:13 predicted grade:13
actual grade:11 predicted grade:7
actual grade:13 predicted grade:10
actual grade:8 predicted grade:9
actual grade:10 predicted grade:10
actual grade:7 predicted grade:8
actual grade:8 predicted grade:8
actual grade:9 predicted grade:9
actual grade:12 predicted grade:12
actual grade:12 predicted grade:12
actual grade:13 predicted grade:11
actual grade:9 predicted grade:9
actual grade:10 predicted grade:9
actual grade:8 predicted grade:8
actual grade:9 predicted grade:9
actual grade:8 predicted grade:9
actual grade:12 predicted grade:10
actual grade:5 predicted grade:5
actual grade:9 predicted grade:8
actual grade:10 predicted grade:9
actual grade:12 predicted grade:11
actual grade:11 predicted grade:9
actual grade:10 predicted grade:10
actual grade:11 predicted grade:11
actual grade:9 predicted grade:10
actual grade:10 predicted grade:9
actual grade:7 predicted grade:8
actual grade:8 predicted grade:8
actual grade:9 predicted grade:9
actual grade:9 predicted grade:7
actual grade:6 predicted grade:8
actual grade:10 predicted grade:8
actual grade:7 predicted grade:7
actual grade:10 predicted grade:10
actual grade:9 predicted grade:9
actual grade:11 predicted grade:11
actual grade:5 predicted grade:5
actual grade:7 predicted grade:7
actual grade:8 predicted grade:7
actual grade:10 predicted grade:7
actual grade:10 predicted grade:5
actual grade:11 predicted grade:11
actual grade:5 predicted grade:5
actual grade:7 predicted grade:7
actual grade:9 predicted grade:9
actual grade:8 predicted grade:8
actual grade:6 predicted grade:8
actual grade:10 predicted grade:8
actual grade:8 predicted grade:8
actual grade:10 predicted grade:10
actual grade:8 predicted grade:5
actual grade:9 predicted grade:5
actual grade:8 predicted grade:7
actual grade:6 predicted grade:7
actual grade:9 predicted grade:7
actual grade:8 predicted grade:8
actual grade:11 predicted grade:9
actual grade:5 predicted grade:5
actual grade:7 predicted grade:7
actual grade:7 predicted grade:8
actual grade:8 predicted grade:8
actual grade:8 predicted grade:8
actual grade:11 predicted grade:8
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:11 predicted grade:9
actual grade:9 predicted grade:9
actual grade:10 predicted grade:10
actual grade:8 predicted grade:8
actual grade:10 predicted grade:9
actual grade:7 predicted grade:7
actual grade:5 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:8
actual grade:5 predicted grade:5
actual grade:7 predicted grade:7
actual grade:8 predicted grade:10
actual grade:8 predicted grade:10
actual grade:10 predicted grade:10
actual grade:8 predicted grade:5
actual grade:10 predicted grade:7
actual grade:8 predicted grade:9
actual grade:11 predicted grade:9
actual grade:5 predicted grade:5
actual grade:9 predicted grade:7
actual grade:10 predicted grade:10
actual grade:8 predicted grade:8
actual grade:10 predicted grade:9
actual grade:9 predicted grade:9
actual grade:9 predicted grade:8
actual grade:7 predicted grade:5
actual grade:10 predicted grade:10
actual grade:9 predicted grade:8
actual grade:10 predicted grade:10
actual grade:7 predicted grade:5
actual grade:11 predicted grade:11
actual grade:9 predicted grade:9

In [85]:
np.sqrt(np.sum((actual5-predict5)**2)/len(L))


Out[85]:
1.4596982408184118

In [86]:
from matplotlib import pyplot as plt
%pylab inline
plt.hist(actual5-predict5,50)


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['sum']
`%matplotlib` prevents importing * from pylab and numpy
Out[86]:
(array([  1.,   0.,   0.,   0.,   0.,   2.,   0.,   0.,   0.,   0.,   0.,
          9.,   0.,   0.,   0.,   0.,  15.,   0.,   0.,   0.,   0.,   0.,
         76.,   0.,   0.,   0.,   0.,  21.,   0.,   0.,   0.,   0.,   0.,
         17.,   0.,   0.,   0.,   0.,   7.,   0.,   0.,   0.,   0.,   0.,
          4.,   0.,   0.,   0.,   0.,   1.]),
 array([-4.  , -3.82, -3.64, -3.46, -3.28, -3.1 , -2.92, -2.74, -2.56,
        -2.38, -2.2 , -2.02, -1.84, -1.66, -1.48, -1.3 , -1.12, -0.94,
        -0.76, -0.58, -0.4 , -0.22, -0.04,  0.14,  0.32,  0.5 ,  0.68,
         0.86,  1.04,  1.22,  1.4 ,  1.58,  1.76,  1.94,  2.12,  2.3 ,
         2.48,  2.66,  2.84,  3.02,  3.2 ,  3.38,  3.56,  3.74,  3.92,
         4.1 ,  4.28,  4.46,  4.64,  4.82,  5.  ]),
 <a list of 50 Patch objects>)

In [87]:
n = 14
cm5 = np.zeros((n,n))
for i in xrange(len(actual5)):
    cm5[predict5[i],actual5[i]] +=1
print cm5


[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   7.   0.   3.   2.   1.   1.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   1.   9.   5.   3.   2.   1.   0.   0.]
 [  0.   0.   0.   0.   0.   1.   4.   4.  11.   6.   2.   2.   1.   0.]
 [  0.   0.   0.   0.   0.   1.   1.   2.   6.  24.   7.   5.   0.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   2.   1.  10.   1.   1.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   2.   8.   2.   3.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   4.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   3.]]

In [88]:
#find TP
from collections import defaultdict
tp = defaultdict(list)
fp = defaultdict(list)
for i in range(0,14):
    for j in range(0,14):
        if i == j: 
            tp[i] = cm5[i,j]
            TP = tp[i]+tp[i-1]+tp[i-2]+tp[i-3]+tp[i-4]+tp[i-5]+tp[i-6]+tp[i-7]+tp[i-8]+tp[i-9]+tp[i-10]+tp[i-11]+tp[i-12]+tp[i-13]
        
            
print "TP is %f"%(TP)
print "--------------------------------------------"
sum = cm5.sum()
FP = sum-TP
print "FP is %f"%(FP)

precision = TP/(FP+TP)
print "precision is %f"%(precision)


TP is 76.000000
--------------------------------------------
FP is 77.000000
precision is 0.496732

Find Precision


In [92]:
print cm1


[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   1.  10.   1.   0.   1.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   4.  20.   7.   2.   1.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   3.  32.  13.  11.   6.   4.   2.   2.   5.]
 [  0.   0.   0.   0.   0.   0.   1.   0.   1.   0.   2.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   1.   0.   0.   0.   0.   0.   0.   1.]
 [  0.   0.   0.   0.   0.   0.   4.   4.   2.   3.   1.   1.   1.   6.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]]

In [93]:
print cm2


[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   2.   1.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   7.   8.   3.   6.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   1.   5.   3.   2.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   7.  10.  34.  12.   5.   1.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   6.   1.  17.   3.   1.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   0.   3.   0.   2.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   3.   3.   4.]]

In [94]:
print cm3


[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   1.   0.   1.   0.   0.   1.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   3.   1.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   1.   1.   0.   5.   0.   0.   1.   0.   1.]
 [  0.   0.   0.   0.   0.   4.  10.   6.  14.  45.  10.   6.   1.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   3.   5.  12.   2.   0.   3.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   3.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   2.   7.]]

In [95]:
print cm4


[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   3.   1.   0.   0.   2.   1.   1.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   1.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   1.   0.   0.   1.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   2.   1.   3.   9.   2.   1.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   3.   3.   6.  18.  45.   7.   3.   6.   3.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   8.   0.   2.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   2.  12.   1.   3.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]]

In [96]:
print cm5


[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   7.   0.   3.   2.   1.   1.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   1.   9.   5.   3.   2.   1.   0.   0.]
 [  0.   0.   0.   0.   0.   1.   4.   4.  11.   6.   2.   2.   1.   0.]
 [  0.   0.   0.   0.   0.   1.   1.   2.   6.  24.   7.   5.   0.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   2.   1.  10.   1.   1.   1.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   2.   8.   2.   3.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   4.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   1.   3.]]

In [97]:
cm = cm1+cm2+cm3+cm4+cm5

In [98]:
print cm


[[   0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
     0.    0.]
 [   0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
     0.    0.]
 [   0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
     0.    0.]
 [   0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
     0.    0.]
 [   0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
     0.    0.]
 [   0.    0.    0.    0.    0.   12.    2.    3.    2.    3.    2.    1.
     0.    0.]
 [   0.    0.    0.    0.    0.    0.    2.    0.    1.    0.    0.    1.
     0.    0.]
 [   0.    0.    0.    0.    0.    1.   18.   22.    9.   10.    3.    1.
     0.    0.]
 [   0.    0.    0.    0.    0.    8.   26.   15.   32.   12.    5.    3.
     1.    1.]
 [   0.    0.    0.    0.    0.   11.   46.   34.   59.  154.   40.   21.
    10.   10.]
 [   0.    0.    0.    0.    0.    0.    1.    0.   12.    7.   49.    6.
     4.    5.]
 [   0.    0.    0.    0.    0.    0.    1.    0.    0.    3.    4.   23.
     4.   10.]
 [   0.    0.    0.    0.    0.    0.    4.    4.    2.    3.    1.    2.
     9.    6.]
 [   0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    1.    3.
     6.   14.]]

Find Precision


In [99]:
#find TP
from collections import defaultdict
tp = defaultdict(list)
fp = defaultdict(list)
for i in range(0,14):
    for j in range(0,14):
        if i == j: 
            tp[i] = cm[i,j]
            TP = tp[i]+tp[i-1]+tp[i-2]+tp[i-3]+tp[i-4]+tp[i-5]+tp[i-6]+tp[i-7]+tp[i-8]+tp[i-9]+tp[i-10]+tp[i-11]+tp[i-12]+tp[i-13]
        
            
print "TP is %f"%(TP)
print "--------------------------------------------"
sum = cm.sum()
FP = sum-TP
print "FP is %f"%(FP)

precision = TP/(FP+TP)
print "precision is %f"%(precision)


TP is 317.000000
--------------------------------------------
FP is 448.000000
precision is 0.414379

In [100]:
cm.sum()


Out[100]:
765.0

In [ ]:
{
    "link": [
        {
            "source": 7,
            "target": 10,
            "type": "licensing"
        },
        {
            "source": 8,
            "target": 10,
            "type": "licensing"
        },
        
    ],
    "node": [
        {
            "name": "CS101"
        },
        {
            "name": "CS102"
        },
    ]
}

In [ ]: