Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 import roslib; roslib.load_manifest('trf_learn')
00031 import rospy
00032 import trf_learn.recognize_3d as r3d
00033 import trf_learn.locations_manager as lcm
00034 import sys
00035 import optparse
00036 import pdb
00037 import numpy as np
00038 import ml_lib.dataset as ds
00039
00040 class LeaveOneOut:
00041
00042 def __init__(self, filename, dataset_name):
00043 self.rec_params = r3d.Recognize3DParam()
00044 self.locations_man = lcm.LocationsManager(filename, rec_params=self.rec_params, train=True)
00045 self.dataset_name = dataset_name
00046 pdb.set_trace()
00047 print 'The following datasets are available:', self.locations_man.data.keys()
00048
00049 def leave_one_out(self):
00050
00051 dataset = self.locations_man.data[self.dataset_name]['dataset']
00052 num_datapoints = dataset.inputs.shape[1]
00053
00054
00055 predicted_values = []
00056 correct = 0
00057 incorrect = 0
00058 confusion = np.matrix([[0,0], [0,0.]])
00059 num_pos = np.sum(dataset.outputs)
00060 num_neg = num_datapoints-num_pos
00061
00062 for i in range(num_datapoints):
00063 loo_dataset, left_out_input, left_out_output = ds.leave_one_out(dataset, i)
00064 self.locations_man.data[self.dataset_name]['dataset'] = loo_dataset
00065 self.locations_man.train(self.dataset_name, save_pca_images=False)
00066 learner = self.locations_man.learners[self.dataset_name]
00067 predicted = learner.classify(left_out_input)
00068 predicted_values += predicted
00069 if predicted[0] == 0:
00070 if left_out_output[0,0] == 0:
00071 confusion[0,0] += 1
00072 else:
00073 confusion[0,1] += 1
00074 else:
00075
00076 if left_out_output[0,0] == 0:
00077 confusion[1,0] += 1
00078 else:
00079 confusion[1,1] += 1
00080
00081 if predicted[0] == left_out_output[0,0]:
00082 correct += 1
00083 else:
00084 incorrect += 1
00085
00086 print '============================================'
00087 print 'dataset', self.dataset_name
00088 print 'confusion matrix\n', confusion
00089 confusion[:,0] = confusion[:,0] / num_neg
00090 confusion[:,1] = confusion[:,1] / num_pos
00091 print 'correct', correct, '\nincorrect', incorrect, '\npercentage', 100.* (correct/float(num_datapoints))
00092 print predicted_values
00093 print '============================================'
00094
00095
00096
00097
00098
00099
00100 if __name__ == '__main__':
00101
00102
00103
00104 if len(sys.argv) > 1:
00105 name = sys.argv[1]
00106 else:
00107 name = 'locations_narrow_v11.pkl'
00108
00109 loo = LeaveOneOut(name, sys.argv[2])
00110 loo.leave_one_out()
00111 print 'end!'
00112
00113