def __init__(self, trainingSet, anomalyMethod = "KNN", h = None ): self.method = anomalyMethod if self.method == "online": self.h = h if self.method == "centroid": self.h = Util.centroid( trainingSet ) if self.method == "medoid": self.h = Util.medoid( trainingSet ) if self.method == "IGNG": self.h = IGNG( radius = PARAMS["R"] ) # IGNG.estimate_radius( trainingSet ) self.h.train( trainingSet ) # print len( self.h.get_nodes_positions() ), len(trainingSet) if self.method == "GNG": self.h = GNG(period = 50) self.h.train( trainingSet ) if self.method == "KNN": self.h = NearestNeighbors(algorithm='ball_tree', metric='euclidean').fit(trainingSet) if self.method == "RNN": self.h = NearestNeighbors(algorithm='ball_tree', metric='euclidean').fit(trainingSet) if self.method == "SVM": self.h = svm.OneClassSVM(nu=PARAMS["NU"], kernel="rbf", gamma=PARAMS["GAMMA"]).fit(trainingSet)
def get_templates(self): # maxval = max([ max(self.edgeHist(e)) for e in self.graph.edges ] )*1. # minval = min([ min(self.edgeHist(e)) for e in self.graph.edges ] )*1. maxval = 1. minval = 0. bins = len(self.graph.edges[0].data.hisv2) hs = [ self.edgeHist(e) for e in self.graph.edges ] H_centroid = Util.centroid( hs ) H_medoid = Util.medoid( hs ) H_stable1 = [ maxval for v in range(bins) ] H_stable2 = [ maxval/2. for v in range(bins) ] H_pyramid = self.inc_vals( bins/2, maxval, minval, 1 ) + self.dec_vals( bins/2, maxval, minval, 1 ) H_gap = self.dec_vals( bins/2, maxval, minval, 1 ) + self.inc_vals( bins/2, maxval, minval, 1 ) H_gap2 = self.dec_vals( bins/2, maxval, minval, 2 ) + self.inc_vals( bins/2, maxval, minval, 2 ) H_half_dec = self.dec_vals( bins/2, maxval, minval, 1 ) + [0.]*(bins/2) H_half_dec2 = self.dec_vals( bins/2, maxval, minval, 2 ) + [0.]*(bins/2) H_half_inc = [0.]*(bins/2) + self.inc_vals( bins/2, maxval, minval, 1 ) H_half_inc2 = [0.]*(bins/2) + self.inc_vals( bins/2, maxval, minval, 2 ) H_dec = self.dec_vals( bins/4, maxval, minval, 1 ) + [0.]*(bins-bins/4) H_dec2 = self.dec_vals( bins/4, maxval, minval, 2 ) + [0.]*(bins-bins/4) H_inc = [0.]*(bins-bins/4) + self.inc_vals( bins/4, maxval, minval, 1 ) H_inc2 = [0.]*(bins-bins/4) + self.inc_vals( bins/4, maxval, minval, 2 ) H = [ H_centroid, H_medoid, H_pyramid, H_stable1, H_stable2, H_gap, H_gap2, H_half_dec, H_half_dec2, H_half_inc, H_half_inc2, H_dec, H_dec2, H_inc, H_inc2 ]; H = [ h[:bins] for h in H ] C = ['r', 'r', 'r', 'r', 'r', 'y', 'y', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b'] return H, C
from sklearn.neighbors import NearestNeighbors #----------------------------------- if __name__ == "__main__": random.seed( 12345 ) # como_extract.computeHistogramsAllBuses(); exit(0) (all_buses, periods_all_buses) = Util.pickleLoad(DATA_FILE_NAME+"_"+SIGNAL_CODE+".txt") dates_all_buses = [ [ Util.getDate(tm) for tm in times ] for times in periods_all_buses ] # como_ploting.Ploting.vizualize_buses(all_buses, dates_all_buses, m = '.'); exit(0) #----------------------------------- for id_bus in range( len(all_buses) ): # for each test bus h = IGNG( radius = 2*PARAMS["R"] ); h.train( [Util.centroid( Util.flatList(all_buses) )] ); dir_imgs = "cosmo_IGNG_deriv_mature/" # h = GNG(period = 200); h.train( [Util.centroid( Util.flatList(all_buses) )] ); dir_imgs = "cosmo_GNG/" own_test = all_buses[id_bus] fleet_test = all_buses[:id_bus] + all_buses[id_bus+1 :] filename = DBFILES[id_bus] busname = filename.split("_")[0] dates = dates_all_buses[id_bus] Z1 = []; Z2 = []; S1 = []; S2 = [] #-------------------------- for i, his_test in enumerate( own_test ): # for each day sys.stdout.write( "\r%s" % "---------------------------- progress = " + str(i*100./len(own_test)) + " " + DBFILES[id_bus] + " " ); sys.stdout.flush()
for i, his_test in enumerate(own_test): # for each day sys.stdout.write( "\r%s" % "---------------------------- progress = " + str(i * 100.0 / len(own_test)) + " " + DBFILES[id_bus] + " " ) sys.stdout.flush() own_test_ = Util.shrink(i, own_test, TH1) fleet_test_ = [Util.shrink(i, bus, TH1) for bus in fleet_test] flat_fleet_test_ = Util.flatList(fleet_test_) # ------ center = Util.centroid(own_test_) score1 = Util.dist(his_test, center) pvalue1 = 0.5 # pvalue1, score1 = como_anomaly.normalityProba_V3( "centroid", own_test_, his_test, all_buses, id_bus, i ) # pvalue1, score1 = como_anomaly.normalityProba_V3( "medoid", own_test_, his_test, all_buses, id_bus, i ) # ------ # pvalue1, score1 = como_anomaly.normalityProba_V1( "online", flat_fleet_test_, his_test, all_buses, id_bus, i, h ) # pvalue1, score1 = como_anomaly.normalityProba_V1( "IGNG", flat_fleet_test_, his_test, all_buses, id_bus, i ) # pvalue1, score1 = como_anomaly.normalityProba_V1( "GNG", flat_fleet_test_, his_test, all_buses, id_bus, i ) # pvalue1, score1 = como_anomaly.normalityProba_V1( "KNN", flat_fleet_test_, his_test, all_buses, id_bus, i ) # pvalue2, score2 = como_anomaly.normalityProba_V2( "RNN", own_test_, his_test, all_buses, id_bus, i ) pvalue2, score2 = 0.5, 0.5 # h.train( [ bus[0] for bus in fleet_test_ ] )# ; print "nb_nodes", h.nb_nodes