how to use knn algorithem ?

1 view (last 30 days)
mari ahmad
mari ahmad on 25 Jan 2016
i have features of 15 images that 11 of them are "ms" mri features and 4 of them are health mri images. i want to classify them to two grouph: "patien person" & "ms person". but when i want to test it, the knn algorithem predict uncorrect( it predict all the features in input matrix classify in second grouph:"ma person"). i also want to test it with the "test matrix":rows of 2 & 5 are "health person" and the other are "ms person". my code is:
close all;
clear;
clc;
load obzervation;% load input include:11 rows for ms person & 4 rows for health person
%load test;% test data include two healthy & three patient
X=obzervation;
trArray = java_array('java.lang.String', 15);
strArray(1) = java.lang.String('MS person');
strArray(2) = java.lang.String('MS person');
strArray(3) = java.lang.String('MS person');
strArray(4) = java.lang.String('MS person');
strArray(5) = java.lang.String('MS person');
strArray(6) = java.lang.String('MS person');
strArray(7) = java.lang.String('MS person');
strArray(8) = java.lang.String('MS person');
strArray(9) = java.lang.String('MS person');
strArray(10) = java.lang.String('MS person');
strArray(11) = java.lang.String('MS person');
strArray(12) = java.lang.String('health person');
strArray(13) = java.lang.String('health person');
strArray(14) = java.lang.String('health person');
strArray(15) = java.lang.String('health person');
cellArray = cell(strArray);
Y=cellArray;
%%Analysis
kmax=7;
knnc=cell(kmax,1);
cvm=cell(kmax,1);
ResubLoss=zeros(kmax,1);
KFoldLoss=zeros(kmax,1);
for k=1:kmax
knnc{k}=ClassificationKNN.fit(X,Y,'NumNeighbors',k);
cvm{k}=crossval(knnc{k});
ResubLoss(k)=resubLoss(knnc{k});
KFoldLoss(k)=kfoldLoss(cvm{k});
end
%%Plot Results
figure;
plot(ResubLoss,'r','LineWidth',2);
hold on;
plot(KFoldLoss,'b','LineWidth',2);
legend('Resub. Loss','k-Fold Loss');
xlabel('Number of Neighbors (k)');
ylabel('Loss');

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!