www.gusucode.com > stats 源码程序 matlab案例代码 > stats/SearchForKNearestNeighborsExample.m
%% Search for _k_-Nearest Neighbors % Find _k_-nearest neighbors of query data given training data using % |knnserach| on a |KDTreeSearcher| model. %% % Load Fisher's iris data. Focus on the petal dimensions. % Copyright 2015 The MathWorks, Inc. load fisheriris X = meas(:,3:4); % Predictors Y = species; % Response %% % Train a _k_-nearest neighbors searcher using the predictors. Specify to % use the Minkowski distance with exponent 5. KDTreeMdl = KDTreeSearcher(X,'Distance','minkowski','P',5) %% % |KDTreeMdl| is a |KDTreeSearcher| model. You can access its properties % using dot notation. %% % Find the 10 nearest neighbors from |X| to a query point (|newpoint|), using % first Minkowski then Chebychev distance metrics. The query point must % have the same column dimension as the data used to train the model. newpoint = [5 1.45]; [IdxMk,DMk] = knnsearch(KDTreeMdl,newpoint,'k',10); [IdxCb,DCb] = knnsearch(KDTreeMdl,newpoint,'k',10,... 'Distance','chebychev'); %% % |IdxMk| and |IdxCb| are 1-by-10 matrices containing the row indices of % |X| corresponding to the nearest neighbors to |newpoint| using Minkowski % and then Chebychev distances, respectively. Element (1,1) is the nearest, % element (1,2) is the next nearest, and so on. %% % Plot the training data. figure; gscatter(X(:,1),X(:,2),Y); title('Fisher''s Iris Data -- Nearest Neighbors'); xlabel('Petal length (cm)'); ylabel('Petal width (cm)'); hold on %% % Zoom in on the points of interest. h = gca; % Get current axis handle. h.XLim = [4.5 5.5]; h.YLim = [1 2]; axis square; %% % Plot a cross for the query point. plot(newpoint(1),newpoint(2),'kx','MarkerSize',10,... 'LineWidth',2); %% % Plot circles to identify the Minkowski nearest neighbors. plot(X(IdxMk,1),X(IdxMk,2),'o','Color',[.5 .5 .5],'MarkerSize',10); %% % Plot pentagrams to identify the Chebychev nearest neighbors. plot(X(IdxCb,1),X(IdxCb,2),'p','Color',[.5 .5 .5],'MarkerSize',10); legend('setosa','versicolor','virginica','query point',... 'minkowski','chebychev','Location','Best'); hold off; %% % Several observations are equal, which is why only eight nearest neighbors are % identified in the plot.