本文整理汇总了Python中sklearn.neighbors.NearestNeighbors.train方法的典型用法代码示例。如果您正苦于以下问题:Python NearestNeighbors.train方法的具体用法?Python NearestNeighbors.train怎么用?Python NearestNeighbors.train使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.neighbors.NearestNeighbors
的用法示例。
在下文中一共展示了NearestNeighbors.train方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import train [as 别名]
class AnomalyModel:
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 getAnomalyScore(self, x, inversed = False):
if self.method == "online":
alpha_m = self.h.getNearestDist(x) # alpha_m = self.h.getNearestDistToMature(x)
if inversed == True: alpha_m = 1. / alpha_m
if self.method == "centroid":
alpha_m = Util.dist(x, self.h)
if inversed == True: alpha_m = 1. / alpha_m
if self.method == "medoid":
alpha_m = Util.dist(x, self.h)
if inversed == True: alpha_m = 1. / alpha_m
if self.method == "IGNG":
alpha_m = self.h.getNearestDist(x)
if inversed == True: alpha_m = 1. / alpha_m
if self.method == "GNG":
alpha_m = self.h.getNearestDist(x)
if inversed == True: alpha_m = 1. / alpha_m
if self.method == "KNN":
distances, indices = self.h.kneighbors( x, n_neighbors = PARAMS["K"] )
alpha_m = sum( distances[0] )
if inversed == True: alpha_m = 1. / alpha_m
if self.method == "RNN":
distances, indices = self.h.radius_neighbors(x, radius = PARAMS["R"])
alpha_m = 1. / ( 1. + sum( [ 1./di for di in distances[0] if di != 0 ] ) )
if inversed == True: alpha_m = 1. / alpha_m
if self.method == "SVM":
alpha_m = -1. * self.h.decision_function(x)[0][0]
if inversed == True: alpha_m = -1. * alpha_m
return alpha_m