knn-matlab achieve


All Close;
CLC;

%% algorithm
% Step1, initialization training set and test set, K value
% create a three-dimensional matrix, two-dimensional representations of two-dimensional coordinate point in the same class, a third dimension represents the category

trainData1 = [0 0; 0.1 0.3; 0.2 0.1; 0.2 0.2];% first class training data
trainData2 = [1 0; 1.1 0.3 ; 1.2 0.1; 1.2 0.2];% second type training data
trainData3 = [0 1; 0.1 1.3 ; 0.2 1.1; 0.2 1.2]; The third training data%

trainData (:,:, 1) = trainData1;% provided a first type of test data
trainData (:,:, 2) = trainData2;% a second type of test data
trainData (:,:, 3) = trainData3;% disposed of three types of test data

trainDim = size (trainData);% acquiring training set number of dimensions
% spatial three-dimensional matrix rows 3 2 4
% trainDim =
%
% 2. 4 3

testData = [1.6 0.3];% provided a test point

K =. 7;

%% respective points are calculated in the test set and the focus point of each of the training Euclidean distance
% extended test points in a matrix
testData_rep = repmat (testData, 4 ,1)

% Initialization value
% repmat A = (10,3,2)
% = A × 2. 3
%
% 10 10
% 10 10
% 10 10
%

% testData_rep =
%
% 1.6000 0.3000
% 1.6000 0.3000
% 1.6000 0.3000
% 1.6000 0.3000


% Sum of squared differences three dimensional matrix is ​​provided to store the test set and test points spreading matrix


= I. 1 for: trainDim (. 3)
DIFF1 = (trainData (:,:,. 1) -testData_rep) ^ 2;.
diff2 = (trainData (:,:, 2) -testData_rep) ^ 2;.
diff3 = (trainData ( :,:,. 3) -testData_rep) ^ 2;.
End

% is provided a three-dimensional array stored Euclidean distances
distance1 = (diff1 (:,. 1) + diff1 (:., 2)) ^ 0.5;
% taken cross diff1 vertical coordinates, taking the calculated level diff1 set point and test point distance
of distance2 to = (diff2 (:,. 1) diff2 + (:, 2)) ^ 0.5;.
distance3 = (diff3 (:,. 1) diff3 + . (:, 2)) ^ 0.5;

% the synthesis of a three-dimensional array, a two-dimensional matrix
temp = [distance1 distance2 distance3]% distances are listed all together size (temp) = 4 * 3 three sample points set test point distance matrix composed
% this two-dimensional matrix is converted into a one-dimensional array
distance = the RESHAPE (TEMP, l, 3 *. 4);
% distance sort
distance_sort = sort (distance)% from the 12 Columbia ordering From small to large


num1 = 0;% frequency and a first type appears
number% of the second type appears; num2 = 0
the number of the third type appears%; num3 = 0
SUM = 0;% SUM1, SUM2, sum3 and
for i = 1: % K taken until all the smallest distance K
for j = 1: 4% for each category training set of sample points, only 4 brother

if distance1 (j) == distance_sort ( i)% sequentially to each sample point to test point distance and turn all distances from small to large to compare
% i does not move, j becomes four times if you can find the same, indicating that these points are relatively close, and if there is no point distance appears, it means this point too far
num1 = num1 + 1;% if found
% DISP ( '*****')
distance1 (J);
num1% latest highest priority, so the first comparison, but a point not explain the problem, recently put the kind total points came to discover which category
End
IF of distance2 to (J) == distance_sort (I)
num2 = num2 +. 1;
% DISP ( '////')
of distance2 to (J); at the minimum point% sequentially accumulating the number of second-class num_i
num2;
End
IF distance3 (J) == distance_sort (I)
num3 = num3 +. 1;
% DISP ( '-----')
distance3 (J);
sum3
end
end
sum=num1+num2+num3;
if sum>=K
break;
end
end

class=[num1 num2 num3];

className = Find% (class (. 1,:) == max (class));
max (class)% Maximum The second category is the kind, sum3 equal SUM1 =. 3 0
class (. 1, :)

classname = find (class (1, :) == max (class))% is the maximum of the kind of the second type, so that the most recently classified as the second type

fprintf ( 'test points (% f% f) belonging to the class% d ', testData (1), testData (2), classname);% most important word

%% the training set using the drawing point and drawing point test set out
Figure (. 1);
HOLD oN;
for I =. 1:. 4
Plot (trainData1 (I,. 1), trainData1 (I, 2), '*');
Plot (trainData2 (I,. 1), trainData2 (I, 2), 'O');
Plot (trainData3 ( I,. 1), trainData3 (I, 2), '>');
End

 


plot(testData(1),testData(2),'x');

text (0.1,0.1, 'first class');
text (1.1,0.1, 'second category');
text (0.1, 1, 'the third category');

Guess you like

Origin www.cnblogs.com/china520/p/11615624.html