Ejemplo n.º 1
0
	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)
Ejemplo n.º 2
0
	def query_sufficient_distance(self):
		ids, _ = self.query_margin()
		scores = []
		
		for ix, x in enumerate(self.Ux):
			if ix in ids[:self.optimize]:
				y1, y2, p1, p2 = self.clf.getMarginInfo(x)
				C = [dp for idp, dp in enumerate(self.Lx) if self.Ly[idp] == y2 ]
				CDx = [Util.dist(dp, x) for idp, dp in enumerate(self.Lx) if self.Ly[idp] == y2 ]
				idsC = (np.array(CDx)).argsort(); xx = Util.medoid( [ C[idp] for idp in idsC[:1] ] )
				
				step = 0.01; lower = 0.; upper = 1.
				while (upper - lower > step):
					w = (upper + lower) / 2.
					px = np.array(x) + w * ( np.array(xx) - np.array(x) )
					
					if self.clf.predict_label(px) != y1: upper = w
					else: lower = w
					
				informativeness = 1. - w
			else: informativeness = 0.
			scores.append( informativeness )
		
		return self.sort_scores(scores)
Ejemplo n.º 3
0
	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
Ejemplo n.º 4
0
	
	#-----------------------------------
	data = Data("datasets\\data_MSL.mat", "array_slip_ratio", randomize = False)
	
	# data.rescale()
	# data.standardize()
	
	print "nb points in data  :", len(data.X)
	print "nb features in data:", data.nb_features
	
	# Util.pickleSave("MIT_hists.txt", data.X)
	#-----------------------------------
	viz = Visualize()
	
	# med = Util.centroid(data.X)
	med = Util.medoid(data.X)
	
	clrs_true = [ 1. if v > 60 else 0. for v in data.Y ]
	viz.PCA_Plot(data.X_transpose, dim = 3, fig = "_PCA_true.png", color = clrs_true)
	
	dists = [ Util.dist(x, med) for x in data.X ]; mean = np.mean(dists); std = np.std(dists)
	for alpha in np.arange(0, 2, 0.1):
		clrs = [ 1. if v - mean > alpha*std else 0. for v in dists ]
		print "alpha", alpha, "Acc", 100.*len([1. for i, j in zip(clrs, clrs_true) if i == j]) / len(data.Y)
		viz.PCA_Plot(data.X_transpose, dim = 3, fig = "_PCA_"+str(alpha)+".png", color = clrs)
	
	# viz.plot(data.X_transpose[:3], data.features_name[:3], fig = "vizu.png", color = data.X_transpose[0])
	
	#-----------------------------------
	# gng = GNG(period = 50)
	# gng.train(data.X, step = 1000, directory = "graph_plots_GNG\\50\\")