题目
例:给定一组三维样本S,
S={S_1,S_2,S_3,S_4,S_5,S_6}={(1,2,0),(3,1,4),(2,1,5),(0,1,6),(2,4,3),(4,4,2)},试用代码(Matlab/Python/C等)找出距离阈值 d 大于等于4,非邻点样本的阈值部分 p 大于等于3时的噪声数据。距离用欧式距离。
个人答案(Python)
import numpy as np
from scipy.spatial import distance
# 给定样本数据
S = np.array([(1,2,0),(3,1,4),(2,1,5),(0,1,6),(2,4,3),(4,4,2)])
# 计算样本之间的欧式距离矩阵
dist_matrix = distance.cdist(S, S, 'euclidean')
# 设置阈值和条件
d = 4 # 距离阈值
p = 3 # 阈值部分
# 找出满足条件的噪声数据
noisy_points = []
for i in range(len(dist_matrix)):
for j in range(i+1, len(dist_matrix)):
if dist_matrix[i, j] >= d and np.sum(dist_matrix[i] >= d) >= p and np.sum(dist_matrix[j] >= d) >= p:
noisy_points.append(S[i])
noisy_points.append(S[j])
# 去除重复的噪声数据
noisy_points = np.unique(noisy_points, axis=0)
# 输出噪声数据
print("噪声数据:")
for point in noisy_points:
print(point)