|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
# 读取数据文件
data = pd.read_csv('data.csv', encoding='GBK')
def euclidean_distance(x1, x2):
"""欧氏距离计算函数"""
return np.sqrt(np.sum((x1 - x2) ** 2))
def get_k_neighbors(x, k, data):
"""获取 x 的 K 近邻"""
distances = []
for i in range(len(data)):
if not np.all(data.iloc[i] == x):
distances.append((i, euclidean_distance(data.iloc[i], x)))
distances.sort(key=lambda x: x[1])
return [distances[i][0] for i in range(k)]
def compute_OF1(x, k, data):
"""计算 x 的离群因子 OF1"""
k_neighbors = get_k_neighbors(x, k, data)
dist_x_k = euclidean_distance(x, data.iloc[k_neighbors[-1]])
dist_x_k_minus1 = euclidean_distance(x, data.iloc[k_neighbors[-2]])
return dist_x_k / dist_x_k_minus1
def detect_outliers(k, data):
"""离群点检测"""
OF1_values = []
for i in range(len(data)):
OF1_values.append(compute_OF1(data.iloc[i], k, data))
# 确定分割点
OF1_values_sorted = sorted(OF1_values, reverse=True)
threshold_index = 0
for i in range(1, len(OF1_values_sorted)):
if (OF1_values_sorted[i] - OF1_values_sorted[i - 1]) / OF1_values_sorted[i] > 0.1:
threshold_index = i
break
threshold = OF1_values_sorted[threshold_index]
# 输出离群点
outliers = data[OF1_values >= threshold]
print('当 k =', k, '时,检测到的离群点:\n', outliers)
# k = 2
detect_outliers(2, data)
# k = 3
detect_outliers(3, data)
# k = 4
detect_outliers(4, data)
帮我优化这段代码
非常抱歉给你带来困扰,以下是经过修正和完善的代码: # -*- coding: utf-8 -*-import numpy as npimport pandas as pd# 读取数据文件data = pd.read_csv('data.csv', encoding='GBK')def euclidean_distance(x1, x2): """欧氏距离计算函数""" return np.sqrt(np.sum((x1 - x2) ** 2))def get_k_neighbors(x, k, data): """获取 x 的 K 近邻和 K-1 近邻""" distances = [(i, euclidean_distance(x, data.iloc[i])) for i in range(len(data)) if not (data.iloc[i] == x).all()] distances.sort(key=lambda x: x[1]) return [distances[i][0] for i in range(k)], [distances[i][0] for i in range(k-1)]def compute_OF1(x, k, data): """计算 x 的离群因子 OF1""" k_neighbors, k_minus1_neighbors = get_k_neighbors(x, k, data) dist_x_k = euclidean_distance(x, data.iloc[k_neighbors]) dist_x_k_minus1 = euclidean_distance(x, data.iloc[k_minus1_neighbors]) return dist_x_k / dist_x_k_minus1def detect_outliers(k, data): """离群点检测""" OF1_values = [] for i in range(len(data)): OF1_values.append(compute_OF1(data.iloc[i], k, data)) # 确定分割点 OF1_values_sorted = sorted(OF1_values, reverse=True) threshold_index = 0 for i in range(1, len(OF1_values_sorted)): if (OF1_values_sorted[i-1] - OF1_values_sorted[i]) / OF1_values_sorted[i-1] > 0.1: threshold_index = i break threshold = OF1_values_sorted[threshold_index] # 输出离群点 outliers = data.iloc[[i for i in range(len(data)) if OF1_values[i] > threshold]] print('当 k =', k, '时,检测到的离群点:\n', outliers)# k = 2detect_outliers(2, data)# k = 3detect_outliers(3, data)# k = 4detect_outliers(4, data)
希望这次的代码能够正常运行。如果还有其他问题,请随时告诉我。非常感谢你的耐心!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|