我觉得找离群点有点类似KMEAN算法,考完后做出来,但不确定对不对

from numpy import * def distEclud(vecA, vecB):
    x = sqrt(pow(vecA[0]-vecB[0],2)+pow(vecA[1]-vecB[1],2)) return x def Kmeans_b(dataSet,N):
    clusterAssment = [[0,0] for i in range(N)] # 创建一个簇分配结果矩阵,第0列记录输入哪一个簇,第1列记录存储误差  # 使用第一个作为质心  centroids = dataSet[0]
    clusterChanged = True  K = int(N * 0.3)
    past_centroids = centroids while clusterChanged:
        clusterChanged = False  for i in range(N):
            clusterAssment[i][1] = distEclud(dataSet[i],centroids)
        arg_clusterAssment = argsort(clusterAssment,axis=0) # 修改属于哪一类  for x in range(N): if x<K:
                clusterAssment[arg_clusterAssment[x][1]][0] = 1  else:
                clusterAssment[arg_clusterAssment[x][1]][0] = 0  # 更新质心  temp = [] for x in clusterAssment: if x[0]==1:
                temp.append(x)
        centroids = sum(temp,axis=0)/K if distEclud(centroids,past_centroids) <= 0.001:
            clusterChanged = True  past_centroids = centroids for i in clusterAssment: print(i[0])
N = 20 data =[[1, 1], [0, 0], [0, 1], [-200, -100], [-200, 0], [-200, 100], [-100, -100], [-100, 0], [-100, 100], [0, -100], [0, 100], [100, -100], [100, 0], [100, 100], [200, -100], [200, 0], [200, 100], [2, 1], [2, 0], [1, 0]] # N = int(input()) # data = [] # for i in range(N): #     data.append(list(map(int,input().split()))) Kmeans_b(data,N)