Finding k-nearest neighbors to get outliers?
5 次查看(过去 30 天)
显示 更早的评论
i want to implement k-nearest neighbors to get outliers with better time complexity
steps:
1-skip first distance for knn(equal zero).
2-Get mean of every distance point in query .
3-Decceding all mean- distance points to get top n (20) .
4- Get size of real outlier from top n to calculate outlier detection rate.
[Idx,Dist] = knnsearch(Data,query,'k',10,'Distance','cityblock','NSMethod','kdtree','IncludeTies',true);
t=mean(Dist(:,2:end),2);
[distance, index] = sort( t, 'descend');
out=(query(index(1:20, 1),:));
[~,ia,~] = intersect(datasetoutlier(:,:),query(:,:),'rows');
nreal-outlier=size(ia);
nout-dataset = size(datasetout(:,:), 1);
detection_rate=nreal-outlier/nout-dataset
1- I want to know if my code is correct to use for big dataset ?.
I found this code in matlab center but i don't know much about parfor .
2-Does this code will be better performance(run time) because of parfor?
function [id,dist] = matlabKNN(data,k,disMetric)
if strcmp(disMetric,'euclidean')
disMetric = 'euclidean';
else
error("δ֪µÄ¾àÀë²ÎÊý");
end
k = k + 1;
[n,~] = size(data);
id = zeros(n,k);
dist = zeros(n,k);
parfor i = 1:1:n
datum = data(i,:);
[aID,aDist] = knnsearch(data,datum,'K',k,...
'Distance',disMetric,'IncludeTies',true);
aID = aID{1}(1:(k));
aDist = aDist{1}(1:(k));
id(i,:) = aID;
dist(i,:) = aDist;
end
id1 = id(:,1);
dist1 = dist(:,1);
id = id(:,2:end);
dist = dist(:,2:end);
for i = 1:1:n
mySelf = find(id(i,:) == i);
if(~isempty(mySelf))
id(i,mySelf) = id1(i);
dist(i,mySelf) = dist1(i);
end
end
end
thanks
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!