def plot_graph(self, data = None, iter = None, directory = "graph_plots\\"): # TODO: this should be generalized and added to Vizualize.py viz = Visualize() if data is not None: viz.do_plot( zip( *data[:iter] )[:3], color = 'y', marker = '.') # viz.do_plot( zip( *data[:iter] )[:3], color = self.data.Y[:iter], marker = '.') viz.do_plot( zip( *self.get_nodes_positions() ), color = 'r', marker = 'o') dis_avg = np.mean([ distance.euclidean(edg.head.data.pos, edg.tail.data.pos) for edg in self.gng.graph.edges ]) dis_std = np.std([ distance.euclidean(edg.head.data.pos, edg.tail.data.pos) for edg in self.gng.graph.edges ]) for e in self.gng.graph.edges: if distance.euclidean(e.head.data.pos, e.tail.data.pos) - dis_avg > 1. * dis_std: viz.do_plot( zip(* [e.head.data.pos, e.tail.data.pos] ) , color = 'y', marker='-') else: viz.do_plot( zip(* [e.head.data.pos, e.tail.data.pos] ) , color = 'r', marker='-') if not os.path.exists(directory): os.makedirs(directory) filename = str(time.time()) + '.png' if iter is None: viz.end_plot(fig = directory+'_'+filename) else: viz.end_plot(fig = directory+filename)
def plot_graph(self, data = None, iter = None, directory = "graph_plots\\"): # TODO: this should be generalized and added to Vizualize.py viz = Visualize() colors = ['r', 'b', 'k', 'g', 'm', 'c']*1000 # FIXME if data is not None: viz.do_plot( zip( *data[:iter] ), color = 'y', marker = '.') # viz.do_plot( zip( *data[:iter] ), color = self.data.Y[:iter], marker = '.') labels=set([n.data.label for n in self.graph.nodes]) d = {l: [n for n in self.graph.nodes if n.data.label == l] for l in labels} for ico,label in enumerate(d): viz.do_plot( zip( *[n.data.pos for n in d[label]] ), color = colors[ico], marker = 'o') for node in d[label]: node_links = [[node.data.pos, n.data.pos] for n in node.neighbors()] for nl in node_links: viz.do_plot( zip( *nl ), color = colors[ico], marker = '-') dis_avg = np.mean([ distance.euclidean(edg.head.data.pos, edg.tail.data.pos) for edg in self.graph.edges ]) dis_std = np.std([ distance.euclidean(edg.head.data.pos, edg.tail.data.pos) for edg in self.graph.edges ]) for e in self.graph.edges: if e.head.data.label != e.tail.data.label: if distance.euclidean(e.head.data.pos, e.tail.data.pos) - dis_avg > 1. * dis_std: viz.do_plot( zip(* [e.head.data.pos, e.tail.data.pos] ) , color = 'w', marker='-') if not os.path.exists(directory): os.makedirs(directory) filename = str(time.time()) + '.png' if iter is None: viz.end_plot(fig = directory+'_'+filename) else: viz.end_plot(fig = directory+filename)
def plot_graph(self, data = None, iter = None, directory = "graph_plots\\"): # TODO: this should be generalized and added to Vizualize.py if len(self.graph.edges) < 1: return viz = Visualize() if data is not None: viz.do_plot( zip( *data[:iter] ), color = 'y', marker = '.') viz.do_plot( zip( *data[max(0,iter-400):iter] ), color = 'y', marker = '.') matures = [node.data.pos for node in self.graph.nodes if node.data.age > self.mature_age ] embryon = [node.data.pos for node in self.graph.nodes if node.data.age <= self.mature_age ] if len(matures) > 0: viz.do_plot( zip( *matures ), color = 'r', marker = 'o') if len(embryon) > 0: viz.do_plot( zip( *embryon ), color = 'g', marker = 'o') # dis_avg = np.mean([ distance.euclidean(edg.head.data.pos, edg.tail.data.pos) for edg in self.graph.edges ]) # dis_std = np.std([ distance.euclidean(edg.head.data.pos, edg.tail.data.pos) for edg in self.graph.edges ]) for e in self.graph.edges: # if e.head.data.age > self.mature_age and e.tail.data.age > self.mature_age: viz.do_plot( zip(* [e.head.data.pos, e.tail.data.pos] ) , color = 'r', marker='-') if not os.path.exists(directory): os.makedirs(directory) filename = str(time.time()) + '.png' if iter is None: viz.end_plot(fig = directory+'_'+filename) else: viz.end_plot(fig = directory+filename)
def plot_graph(self, data = None, iter = None, dir = "graph_plots\\"): if len(self.graph.edges) < 1: return if not os.path.exists(dir): os.makedirs(dir) viz = Visualize(); viz2 = Visualize() self.separate() H, C = self.get_templates() hs = [ self.edgeHist(e) for e in self.graph.edges ] COLOR = self.group_by_ref(hs, H, C) carac_edges = [ self.edge_features(e) for i, e in enumerate(self.graph.edges) ] # carac_edges = [ self.edge_features(e) + [1. if COLOR[i]=='r' else 0.] for i, e in enumerate(self.graph.edges) ] # viz2.MDS_Plot( zip(*hs), dim=2, fig=dir+str(time.time())+'--.png', marker='o', color=COLOR ) # viz2.MDS_Plot( zip(*carac_edges), dim=2, fig=dir+str(time.time())+'__.png', marker='o', color=COLOR ) # viz2.plot( zip(*carac_edges), fig=dir+str(time.time())+'__.png', marker='o', color=COLOR ) ''' for ih, h in enumerate(H): plt.bar(range(len(h)), h, color=C[ih]); plt.savefig(dir+str(time.time())+'.png'); plt.close() for ie, e in enumerate(self.graph.edges): c = COLOR[ie] plt.title('-'.join(str(e) for e in e.head.data.pos)); plt.bar(range(len(hs[ie])), hs[ie], color=c); plt.savefig(dir+str(time.time())+'.png'); plt.close() ''' # ''' if data is not None: viz.do_plot( zip( *data[:iter] ), color = 'y', marker = '.') matures = [node.data.pos for node in self.graph.nodes if node.data.age > self.mature_age ] if len(matures) > 0: viz.do_plot( zip( *matures ), color = 'r', marker = 'o') embryon = [node.data.pos for node in self.graph.nodes if node.data.age <= self.mature_age ] if len(embryon) > 0: viz.do_plot( zip( *embryon ), color = 'g', marker = 'o') low_dens = [ node.data.pos for node in self.graph.nodes if not self.isDenseNode(node) ] if len(low_dens) > 0: viz.do_plot( zip( *low_dens ), color = 'b', marker = 'o') for ie, e in enumerate(self.graph.edges): # if e.head.data.age > self.mature_age and e.tail.data.age > self.mature_age: # if self.isDenseNode(e.head) and self.isDenseNode(e.tail): c = COLOR[ie] # c = 'r' if self.isDenseEdgeMean(e) else 'y' # c = 'r' if self.isDenseEdgeMean(e) else COLOR[ie] # c = 'r' if self.isDenseEdgeMean(e) or e.head.data.label == e.tail.data.label else COLOR[ie] viz.do_plot( zip(* [e.head.data.pos, e.tail.data.pos] ) , color = c, marker='-', lw = 1) filename = str(time.time()) + '.png' if iter is None: viz.end_plot(fig = dir+'_'+filename) else: viz.end_plot(fig = dir+filename)
def plot(self, fig=None): if not self.done(): return viz = Visualize() if len(self.X[0]) > 3: X = viz.PCA_Transform( list(zip(*self.X)) ) else: X = self.X unique_labels = np.unique(self.Y) clusters = { ul:[] for ul in unique_labels } for i in range( len(X) ): clusters[ self.Y[i] ].append( X[i] ) centers_for_plot = [] # Not the real centers because dimension was reduced using PCA for label in clusters: centers_for_plot.append( [np.mean(col) for col in list(zip(* clusters[label] )) ] ) viz.do_plot(list(zip(*centers_for_plot)), marker='o', color='m') viz.plot_groups(clusters, fig)
def plot_graph(self, data = None, iter = None, directory = "graph_plots\\"): # TODO: this should be generalized and added to Vizualize.py viz = Visualize() if data is not None: viz.do_plot( zip( *data[:iter] ), color = 'y', marker = '.') # viz.do_plot( zip( *data[:iter] ), color = self.data.Y[:iter], marker = '.') matures = [node.data.pos for node in self.graph.nodes if node.data.age > self.mature_age ] embryon = [node.data.pos for node in self.graph.nodes if node.data.age <= self.mature_age ] if len(matures) > 0: viz.do_plot( zip( *matures ), color = 'r', marker = 'o') if len(embryon) > 0: viz.do_plot( zip( *embryon ), color = 'y', marker = 'o') for e in self.graph.edges: pos_head = e.head.data.pos pos_tail = e.tail.data.pos viz.do_plot( zip(* [pos_head, pos_tail] ) , color = 'r', marker='-') if not os.path.exists(directory): os.makedirs(directory) filename = str(time.time()) + '.png' if iter is None: viz.end_plot(fig = directory+'_'+filename) else: viz.end_plot(fig = directory+filename)
def vizualize_buses( all_buses, dates_all_buses, dim = 2, path = "buses_viz/", m = '.' ): Util.mkdir(path) # ''' viz0 = Visualize(); viz1 = Visualize(); viz2 = Visualize(); viz3 = Visualize() c = Visualize.colors( len(all_buses) ) D = Util.flatList(all_buses) viz1.PCA_Plot( zip(*D), dim = dim, fig=path+"_Buses_All.png", color='b', marker = m ) # DD = Util.shuffle_list(D) # for i in xrange(0, len(DD)-1, 1000): viz1.MDS_Plot( zip(*DD[i:i+1000]), dim = dim, fig=path+"_"+str(i)+"_Buses_All.png", color='b', marker = m ) X = viz1.PCA_Transform( zip(*D), dim = dim ) all_buses_transformed = [] for ib in range( len(all_buses) ): print ib+1, Xb = [ x for i,x in enumerate(X) if D[i] in all_buses[ib] ] all_buses_transformed.append( Xb ) viz0.do_plot( zip(*Xb), color = c[ib], marker = m ) viz1.plot( zip(*Xb), fig=path+"Bus"+str(ib)+".png", color = c[ib], marker = m ) viz0.end_plot(fig=path+"_Buses_All_c.png") # ''' window = 30; step = 10; window_t = datetime.timedelta(days = window); step_t = datetime.timedelta(days = step) t = datetime.datetime(year=2011, month=6, day=1) while t <= datetime.datetime(year=2015, month=9, day=1): viz2.do_plot( [[-0.39, 0.39], [-0.39, 0.39]], color='w' ) for ib, bus in enumerate(all_buses_transformed): # bus_tt = [x for ix,x in enumerate(bus) if ix < len(dates_all_buses[ib]) and dates_all_buses[ib][ix] > t and dates_all_buses[ib][ix] <= t+window_t] bus_tt = [x for ix,x in enumerate(bus) if ix < len(dates_all_buses[ib]) and dates_all_buses[ib][ix] <= t+window_t] if len( bus_tt ) > 0: viz2.do_plot( zip(* bus_tt ), color = c[ib], marker = m ) viz3.do_plot( [[-0.39, 0.39], [-0.39, 0.39]], color='w' ); viz3.do_plot( zip(* bus_tt ), color = c[ib], marker = m ); viz3.end_plot(fig=path+"Bus"+str(ib)+"_"+Util.date2str(t+window_t)+".png") viz2.end_plot(fig=path+"_Buses_"+Util.date2str(t+window_t)+".png") t += step_t '''
def plot(self, fig=None): if not self.done(): return viz = Visualize() if len(self.X[0]) > 3: X = viz.PCA_Transform(list(zip(*self.X))) else: X = self.X unique_labels = np.unique(self.Y) clusters = {ul: [] for ul in unique_labels} for i in range(len(X)): clusters[self.Y[i]].append(X[i]) centers_for_plot = [ ] # Not the real centers because dimension was reduced using PCA for label in clusters: centers_for_plot.append( [np.mean(col) for col in list(zip(*clusters[label]))]) viz.do_plot(list(zip(*centers_for_plot)), marker='o', color='m') viz.plot_groups(clusters, fig)
def plot_graph(self, data = None, iter = None, directory = "graph_plots\\"): # TODO: this should be generalized and added to Vizualize.py viz = Visualize() if data is not None: viz.do_plot( zip( *data[:iter] ), color = 'y', marker = '.') # viz.do_plot( zip( *data[:iter] ), color = self.data.Y[:iter], marker = '.') viz.do_plot( zip( *self.get_nodes_positions() ), color = 'r', marker = 'o') for e in self.graph.edges: pos_head = e.head.data.pos pos_tail = e.tail.data.pos viz.do_plot( zip(* [pos_head, pos_tail] ) , color = 'r', marker='-') if not os.path.exists(directory): os.makedirs(directory) filename = str(time.time()) + '.png' if iter is None: viz.end_plot(fig = directory+'_'+filename) else: viz.end_plot(fig = directory+filename)
s=20) plt2.text(start_plt, 120, "clustering") cl_ssp = [MODE_NAMES[xx] for xx in labels_ssp] plt2.scatter(TIME[start_plt:end_plt], [110] * len(TIME[start_plt:end_plt]), color=cl_ssp[start_plt:end_plt], marker="|", s=20) plt2.text(start_plt, 100, "tracking") fname = "plots/rs" + str(id_combin) + ".png" plt2.savefig(fname) plt2.close() viz.do_plot( [vals, FSP_ari], axs_labels=['Parameter ' + variable, 'Adjusted Random Index'], marker="-", color="red", label="FSP_ari") viz.do_plot( [vals, SSP_ari], axs_labels=['Parameter ' + variable, 'Adjusted Random Index'], marker="-", color="blue", label="SSP_ari") viz.end_plot(fig=gb.PLOT_PATH + "/ARI_" + str(id_combin) + "---" + str(time.time()) + ".png") viz.do_plot([vals, FSP_ami], axs_labels=[ 'Parameter ' + variable, 'Adjusted Mutual Information' ],
def sortForInformativeness(self, mtd): if mtd in ["etc", "etc_", "expectedErrorReduction", "weight", "optimal", "test", "intuition"] : ids, scores = self.sortForInformativeness(self.optimization_method) scores = [] for ix, x in enumerate(self.Ux): y1, y2, p1, p2 = self.clf.getMarginInfo(x) if mtd == "intuitionM": if ix in ids[:self.optimization_limit]: informativeness = self.clf.uncertainty_margin(x) else: informativeness = 0. #--------------------------------------------------------- if mtd == "margin": informativeness = self.clf.uncertainty_margin(x) #--------------------------------------------------------- elif mtd == "proba": informativeness = self.clf.uncertainty_prediction(x) #--------------------------------------------------------- elif mtd == "entropy": informativeness = self.clf.uncertainty_entropy(x) #--------------------------------------------------------- elif mtd == "random": informativeness = random.uniform(0., 1.) #--------------------------------------------------------- elif mtd == "weight": if ix in ids[:self.optimization_limit]: informativeness = self.clf.uncertainty_weight(x, self.Lx, self.Ly) else: informativeness = 0. #--------------------------------------------------------- elif mtd == "expectedErrorReduction": if ix in ids[:self.optimization_limit]: sums = 0. YP = self.clf.predict(x, all = True) YP.sort(key=operator.itemgetter(1), reverse=True) for ir, (yy, proba) in enumerate(YP): if ir == 5: break temp_clf = Classification(self.Lx + [x], self.Ly + [yy], method = self.clf.method); temp_clf.GAMMA, temp_clf.C = self.clf.GAMMA, self.clf.C # TODO FIXME: do it in general not specifically for svm temp_clf.train() e_h1 = sum( [ temp_clf.uncertainty_entropy(dp) for dp in self.Ux if dp != x ] ) sums += (proba) * e_h1 informativeness = 1. / sums else: informativeness = 0. #--------------------------------------------------------- elif mtd == "etc": if ix in ids[:self.optimization_limit]: temp_clf1 = Classification(self.Lx + [x], self.Ly + [y1], method = self.clf.method); temp_clf1.GAMMA, temp_clf1.C = self.clf.GAMMA, self.clf.C temp_clf1.train() diff1 = sum( [ abs(temp_clf1.getPredictProba(1,dp) - self.clf.getPredictProba(1,dp)) if temp_clf1.predict_label(dp) != self.clf.predict_label(dp) else 0. for dp in self.Ux if dp != x ] ) / (len(self.Ux) - 1.) temp_clf2 = Classification(self.Lx + [x], self.Ly + [y2], method = self.clf.method); temp_clf2.GAMMA, temp_clf2.C = self.clf.GAMMA, self.clf.C temp_clf2.train() diff2 = sum( [ abs(temp_clf2.getPredictProba(1,dp) - self.clf.getPredictProba(1,dp)) if temp_clf2.predict_label(dp) != self.clf.predict_label(dp) else 0. for dp in self.Ux if dp != x ] ) / (len(self.Ux) - 1.) informativeness = diff1 # this one is particularly good for rejection (to be confirmed) informativeness = diff1 if p1/(1+diff1) >= p2/(1+diff2) else diff2 informativeness = p1*diff1 + p2*diff2 + 1. else: informativeness = 0. #--------------------------------------------------------- elif mtd == "etc_": if ix in ids[:self.optimization_limit]: temp_clf1 = Classification(self.Lx + [x], self.Ly + [y1], method = self.clf.method); temp_clf1.GAMMA, temp_clf1.C = self.clf.GAMMA, self.clf.C temp_clf1.train() diff1 = sum( [ 1. if temp_clf1.predict_label(dp) != self.clf.predict_label(dp) else 0. for dp in self.Ux if dp != x ] ) / (len(self.Ux) - 1.) temp_clf2 = Classification(self.Lx + [x], self.Ly + [y2], method = self.clf.method); temp_clf2.GAMMA, temp_clf2.C = self.clf.GAMMA, self.clf.C temp_clf2.train() diff2 = sum( [ 1. if temp_clf2.predict_label(dp) != self.clf.predict_label(dp) else 0. for dp in self.Ux if dp != x ] ) / (len(self.Ux) - 1.) informativeness = diff1 # this one is particularly good for rejection (to be confirmed) informativeness = diff1 if p1/(1+diff1) >= p2/(1+diff2) else diff2 informativeness = p1*diff1 + p2*diff2 + 1. else: informativeness = 0. #--------------------------------------------------------- elif mtd == "test": if ix in ids[:self.optimization_limit]: temp_clf1 = Classification(self.Lx + [x], self.Ly + [y1], method = self.clf.method); temp_clf1.GAMMA, temp_clf1.C = self.clf.GAMMA, self.clf.C temp_clf1.train() diff1 = np.mean( [0.]+[ abs(temp_clf1.getPredictProba(1,dp) - self.clf.getPredictProba(1,dp)) for dp in self.Ux if temp_clf1.predict_label(dp) != self.clf.predict_label(dp) and dp != x ] ) temp_clf2 = Classification(self.Lx + [x], self.Ly + [y2], method = self.clf.method); temp_clf2.GAMMA, temp_clf2.C = self.clf.GAMMA, self.clf.C temp_clf2.train() diff2 = np.mean( [0.]+[ abs(temp_clf2.getPredictProba(1,dp) - self.clf.getPredictProba(1,dp)) for dp in self.Ux if temp_clf2.predict_label(dp) != self.clf.predict_label(dp) and dp != x ] ) informativeness = diff1 # this one is particularly good for rejection (to be confirmed) informativeness = diff1 if p1/(1+diff1) >= p2/(1+diff2) else diff2 informativeness = p1*diff1 + p2*diff2 + 1. else: informativeness = 0. #--------------------------------------------------------- elif mtd == "intuition": if ix in ids[:self.optimization_limit]: true_y = self.Uy[ self.Ux.index(x) ] temp_clf = Classification(self.Lx + [x], self.Ly + [true_y], method = self.clf.method) temp_clf.GAMMA, temp_clf.C = self.clf.GAMMA, self.clf.C; temp_clf.train() ucts = [ temp_clf.getPredictProba(1,dp) - self.clf.getPredictProba(1,dp) for dp in self.Tx ] ids_ucts = (-np.array(ucts)).argsort()[:50] # diff = np.mean( [ 1. if temp_clf.predict_label(dp) != self.clf.predict_label(dp) else 0. for dp in self.Tx ] ) diff = np.mean([1. if temp_clf.predict_label(dp) != self.clf.predict_label(dp) and idp in ids_ucts else 0. for idp,dp in enumerate(self.Tx) ]) informativeness = diff else: informativeness = 0. #--------------------------------------------------------- elif mtd == "intuition": if ix in ids[:self.optimization_limit]: true_y = self.Uy[ self.Ux.index(x) ] temp_clf = Classification(self.Lx + [x], self.Ly + [true_y], method = self.clf.method) temp_clf.GAMMA, temp_clf.C = self.clf.GAMMA, self.clf.C; temp_clf.train() # --------------------- imp_x = [ xdp for xdp in self.Tx if temp_clf.predict_label(xdp) != self.clf.predict_label(xdp) ] imp_y_hh = [ temp_clf.predict_label(xdp) for xdp in self.Tx if temp_clf.predict_label(xdp) != self.clf.predict_label(xdp) ] if len( set(imp_y_hh) ) > 1: # hh = Classification(imp_x, imp_y_hh, method = self.clf.method) hh = Classification(imp_x + [x], imp_y_hh + [true_y], method = self.clf.method, tuning = False) hh.GAMMA, hh.C = self.clf.GAMMA, self.clf.C; hh.train() else: hh = self.clf # --------------------- h_inconsistant_truth = 0; hh_inconsistant_truth = 0; hh_inconsistant_h = 0; h_consistency = []; hh_consistency = [] for ilx, lx in enumerate(self.Lx): h_consistency.append( self.clf.getProbaOf( self.Ly[ilx], lx ) ) # hh_consistency.append( hh.getProbaOf( self.Ly[ilx], lx ) ) hh_consistency.append( hh.getProbaOf( self.Ly[ilx], lx ) if hh.predict_label(lx) == self.Ly[ilx] else 0. ) if self.clf.predict_label(lx) != self.Ly[ilx]: h_inconsistant_truth += 1. if hh.predict_label(lx) != self.Ly[ilx]: hh_inconsistant_truth += 1. if hh.predict_label(lx) != self.clf.predict_label(lx): hh_inconsistant_h += 1. h_consistency = np.mean(h_consistency) hh_consistency = np.mean(hh_consistency) if len( set(imp_y_hh) ) > 1 else 0. consistency_dif = hh_consistency - h_consistency # --------------------- diff = []; errors = 0.; trues = 0.; impacted = 0; impacted_probs = []; for idp, dp in enumerate(self.Tx): if temp_clf.predict_label(dp) != self.clf.predict_label(dp): ################## impacted += 1. impacted_probs.append( abs( temp_clf.getPredictProba(1,dp) - self.clf.getPredictProba(1,dp) ) ) if self.Ty[idp]!=temp_clf.predict_label(dp): errors += 1. else: trues += 1. # if temp_clf.predict_label(dp) != self.clf.predict_label(dp) and self.Ty[idp]==temp_clf.predict_label(dp): diff.append( 1. ) # if temp_clf.predict_label(dp) != self.clf.predict_label(dp) and trues - errors > 0: diff.append( 1. ) # if temp_clf.predict_label(dp) != self.clf.predict_label(dp): diff.append( 1. ) if temp_clf.predict_label(dp) != self.clf.predict_label(dp): diff.append( 1. ) else: diff.append( 0. ) diff = np.mean( diff ) # diff = diff * np.mean(impacted_probs) # seems to be working ... # --------------------- # self.viz_A.append( consistency_dif ) self.viz_A.append( hh_consistency ) self.viz_B.append( errors ) self.viz_C.append( trues ) self.viz_D.append( trues - errors ); posI = [inb for inb,nbD in enumerate(self.viz_D) if nbD >= 0.] self.viz_E.append( impacted ) self.viz_F.append( np.mean(impacted_probs) ) viz = Visualize(); viz.plot( [self.viz_A, self.viz_B], fig = "test_errors.png", color = 'r', marker = 'o' ) vizu = Visualize(); vizu.plot( [self.viz_A, self.viz_C], fig = "test_trues.png", color = 'r', marker = 'o' ) vizuu = Visualize(); vizuu.plot( [self.viz_A, self.viz_D], fig = "test_trues_errors.png", color = 'r', marker = 'o' ) vizuuu = Visualize(); vizuuu.do_plot( [self.viz_A, self.viz_E], color = 'r', marker = 'o' ) vizuuu.do_plot( [[self.viz_A[inb] for inb in posI], [self.viz_E[inb] for inb in posI]], color = 'b', marker = 'o' ) vizuuu.end_plot(fig = "impacted.png") print hh_consistency, hh_inconsistant_truth, "---", len(imp_x), len( set(imp_y_hh) ), "============>", impacted, trues - errors informativeness = diff else: informativeness = 0. #--------------------------------------------------------- scores.append( informativeness ) ids = (-np.array(scores)).argsort() sorted_scores = [ scores[id] for id in ids ] # sorted_scores = [ 1.*scores[id] / sum(scores) for id in ids ] return ids, sorted_scores
#umode = set(modes) cl=[MODE_NAMES[xx] for xx in modes] start_plt = sss end_plt = 3*sss plt2.scatter(TIME[start_plt:end_plt],signalsValues[0][start_plt:end_plt], color = cl[start_plt:end_plt]) cl_fsp = [MODE_NAMES[xx] for xx in labels_fsp] plt2.scatter(TIME[start_plt:end_plt],[130]*len(TIME[start_plt:end_plt]),color=cl_fsp[start_plt:end_plt],marker="|",s=20) plt2.text(start_plt,120,"clustering") cl_ssp = [MODE_NAMES[xx] for xx in labels_ssp] plt2.scatter(TIME[start_plt:end_plt],[110]*len(TIME[start_plt:end_plt]),color=cl_ssp[start_plt:end_plt],marker="|",s=20) plt2.text(start_plt,100,"tracking") fname = "plots/rs"+ str(id_combin) + ".png" plt2.savefig(fname) plt2.close() viz.do_plot( [vals, FSP_ari], axs_labels=['Parameter '+variable, 'Adjusted Random Index'], marker="-", color="red", label="FSP_ari" ) viz.do_plot( [vals, SSP_ari], axs_labels=['Parameter '+variable, 'Adjusted Random Index'], marker="-", color="blue", label="SSP_ari" ) viz.end_plot( fig=gb.PLOT_PATH+"/ARI_"+str(id_combin)+"---"+str(time.time())+".png" ) viz.do_plot( [vals, FSP_ami], axs_labels=['Parameter '+variable, 'Adjusted Mutual Information'], marker="-", color="red", label="FSP_ami" ) viz.do_plot( [vals, SSP_ami], axs_labels=['Parameter '+variable, 'Adjusted Mutual Information'], marker="-", color="blue", label="SSP_ami" ) viz.end_plot( fig=gb.PLOT_PATH+"/AMI_"+str(id_combin)+"---"+str(time.time())+".png" ) viz.do_plot( [vals, FSP_ho], axs_labels=['Parameter '+variable, 'Homogeneity'], marker="-", color="red", label="FSP_ho" ) viz.do_plot( [vals, SSP_ho], axs_labels=['Parameter '+variable, 'Homogeneity'], marker="-", color="blue", label="SSP_ho" ) viz.end_plot( fig=gb.PLOT_PATH+"/HO_"+str(id_combin)+"---"+str(time.time())+".png" ) viz.do_plot( [vals, FSP_com], axs_labels=['Parameter '+variable, 'Completeness'], marker="-", color="red", label="FSP_com" ) viz.do_plot( [vals, SSP_com], axs_labels=['Parameter '+variable, 'Completeness'], marker="-", color="blue", label="SSP_com" ) viz.end_plot( fig=gb.PLOT_PATH+"/COM_"+str(id_combin)+"---"+str(time.time())+".png" )
def train(self, mtd = "margin", backupfile = "backupfile.txt"): for i, x in enumerate(self.Ux): y1 = self.clf.predict_label(x) if mtd == "supervised": informativeness = sys.float_info.max if mtd == "margin": informativeness = self.clf.uncertainty_margin(x) # =============================== id_th = self.mab.choose() self.th = self.mab.algos[ id_th ] print "Choosen =", self.th, "nb_choices =", self.mab.nb_choices, ("avg rwd=", [ np.mean(L) for L in self.mab.rewards ] if self.mab.rewards[0]!=[] else " "), "expected=", sum([ a*l for a,l in zip(self.mab.algos,self.mab.nb_choices) ]) / sum(self.mab.nb_choices) prev_clf = Classification(self.Lx, self.Ly, method = self.clf.method) prev_clf.GAMMA, prev_clf.C = self.clf.GAMMA, self.clf.C; prev_clf.train() # =============================== # avg_rewards = [ np.mean(L[:-20]) if len(L)>0 else 1. for L in self.mab.rewards ] # self.th = sum([ a*l for a,l in zip(self.mab.algos,avg_rewards) ]) / sum(avg_rewards) # print "Choosen =", self.th, "avg rwd=", avg_rewards # =============================== if informativeness > self.th: qx = x qy = self.Uy[i] self.Lx.append(qx) self.Ly.append(qy) self.queried += 1 self.clf.X = self.Lx; self.clf.Y = self.Ly; self.clf.train() # =============================== reward = 1. - abs( 0.1 - self.queried / (i+1.) ) self.mab.update(id_th, reward) # =============================== # for idt in range(len(self.mab.algos)): # reward = 1. - abs( 0.3 - (self.queried-1+1) / (i+1.) ) if informativeness > self.mab.algos[idt] else 1. - abs( 0.4 - (self.queried-1) / (i+1.) ) # self.mab.update(idt, reward) # =============================== self.ths.append( self.th ) self.infos.append( informativeness ) self.accuracys.append( self.clf.getTestAccuracy( self.Tx, self.Ty ) ) self.queries.append( self.queried ) self.sup_infos.append( self.sup_clf.uncertainty_margin(x) ) # TODO should not be here self.sup_clf.X = self.Lx0+self.Ux[:i+1]; self.sup_clf.Y = self.Ly0+self.Uy[:i+1]; self.sup_clf.train() # TODO should not be here self.sup_accuracys.append( self.sup_clf.getTestAccuracy( self.Tx, self.Ty ) ) # TODO should not be here ''' if i>10: # last_infos = self.infos[-100:] if len(self.infos) > 100 else self.infos[:] # self.th = np.mean( last_infos ) if informativeness > self.th: # queried if y1 == qy: # but was correctly predicted self.th = self.th + 0.1 * (informativeness - self.th) else: if y1 != qy: self.th = self.th - 0.1 * (self.th - informativeness ) ''' print "i=", i+1, self.queried, self.queried / (i+1.), "-- acc=%.4f"%(self.accuracys[-1]*100), "%.4f"%(self.sup_accuracys[-1]*100), "-- %.4f"%(np.mean(self.accuracys)*100), "%.4f"%(np.average(self.accuracys, weights = range(1,1+len(self.accuracys)))*100), "--", informativeness if (i+1)%10 == 0: Util.pickleSave(backupfile, self); viz = Visualize() viz.do_plot( [range(len(self.infos)), self.ths], color = 'b', marker = '-' ) viz.do_plot( [range(len(self.infos)), self.infos], color = 'r', marker = '-' ) viz.do_plot( [range(len(self.sup_infos)), self.sup_infos], color = 'y', marker = '-' ) viz.end_plot( fig = backupfile+"_stream_inf.png" ) viz.do_plot( [range(len(self.accuracys)), self.accuracys], color = 'r', marker = '-' ) viz.do_plot( [range(len(self.sup_accuracys)), self.sup_accuracys], color = 'y', marker = '-' ) viz.end_plot( fig = backupfile+"_stream_acc.png" ) viz.do_plot( [range(len(self.queries)), self.queries], color = 'r', marker = '-' ) viz.do_plot( [range(len(self.queries)), range(len(self.queries))], color = 'y', marker = '-' ) viz.end_plot( fig = backupfile+"_stream_lab.png" ) '''