knn classifier

I havea segmented image of a brain,i have extracted the features for that image and have stored it in stats,now i want to classify that image using knn classifier,wheter it is starting stage or middle level stage or the image is normal
in knn
c = knnclassify(sample, training, group);
what i should give in place of sample, training, group
please help

Answers (3)

Tom Lane
Tom Lane on 7 Apr 2012

0 votes

You would give "sample" as the data you want to classify, "training" as the training data having known groups, and "group" as the known groups for the training data.
If you don't have training data with known groups, you could try cluster analysis instead of knn classification.

4 Comments

kash
kash on 10 Apr 2012
Tom can u give an example please
kash
kash on 10 Apr 2012
Tom i have features with 15 rows and 2 columns,pleas etell how t do
Tom Lane
Tom Lane on 10 Apr 2012
"help knnclassify" gives an example. If that's not helpful, you'll need to ask a more specific question.
kash
kash on 11 Apr 2012
Tom please tell how to classify based on features,i have extracted features of the segmented images and have 20 features.say for example
stats
mean:[0.6 0.5];
entropy[.9 0.23];
energy:[0.025 0.36];
like these i have 20 features from sgldm,now i wnt to classify image if(tumour) it in normal stage ,starting stage ,middle stage
please help

Sign in to comment.

Tom Lane
Tom Lane on 11 Apr 2012

0 votes

Sorry, I just don't understand what you have and what you want. If you had an n-by-20 matrix X containing features from pre-classified tumors, a vector G of size n-by-1 giving the classification of those tumors, and another m-by-20 matrix Y containing the same features measured on new tumors, you would use knnclassify(Y,X,G). The result would be a vector of size m-by-1 containing the classifications of the tumors in Y.

5 Comments

kash
kash on 12 Apr 2012
Tom i could not understand ,can u provide me an example plz.i need the output as these
whether the tumour is in starting stage ,or in middle stage ,normal stage
for example let us assume the fever of a person
if the bodt temp is 80deg it is starting stage ,110 deg high,65deg ,he is normal ,like these i want to classify,here temperature is an feature
kash
kash on 12 Apr 2012
Tom i used
Y=[0.5 0.3 ;0.8 0.9;0.1 0.3;0.6 0.9];
X=[0.2 0.5;02 0.5 ;0.1 0.3;0.2 0.6]
G=[0; 1;1;2]
class=knnclassify(Y,X,G)
for class i get answer as
class =
0
2
1
2
from these how to classify to which class it belongs
0-normal .1 is starting stage ,2 is middle stage tumours
here is an example, I cannot interpret the mu and sigma, CX1 and CX2
%% Section I: Forming and plotting the dataset
mu = [1.5,1.5]; sigma = [1,1.5;1.5,3];
sampleNo = 3000;
testNo = 1000;
C1X = (mvnrnd(mu,sigma,sampleNo));
mu = [4,1];sigma = [1,1.5;1.5,3];
C2X = (mvnrnd(mu,sigma,sampleNo));
% mean computation
x = [C1X,C2X];
mu = [1.5,1.5]; sigma = [1,1.5;1.5,3];
That would be for a multivariate normal random distribution with two variables, each of which had mean 1.5, and sigma describes the covariances.
mu = [4,1];sigma = [1,1.5;1.5,3];
Similar to the above but the mean for the first variable is 4 and the mean for the second variable is 1
% mean computation
x = [C1X,C2X];
That just concatenates the results of the random generation together and does not calculate any means.
merlin toche
merlin toche on 22 Nov 2022
thank'you sir ! please i want to know how to built dataset of knn for fault detection.
i get for example x=[4 6 7 5 8] and y=[3 7 8 5 8]; respectively the craracteristics of current and voltage as income data and take outcome(pass or fail) in one array and z=(x,y)=(2,4) as data test
c=['open,'short','short','openl','open'];
how to buit this dataset and train it. thanks for your help

Sign in to comment.

Tom Lane
Tom Lane on 12 Apr 2012
In your X,G values you specified that certain rows of X were in the 0, 1, or 2 stage. The "class" output is that same classification for the rows of Y. Try this:
gscatter(X(:,1),X(:,2),G,'rgb','o')
hold on; gscatter(Y(:,1),Y(:,2),class,'rgb','x'); hold off
axis equal
You can see that the Y values ('x') are the same color as the closest X values, indicating that they were classified to the same groups.
I'm not sure we are communicating properly, since either your "class" values are the ones you asked for, or I'm just not understanding. If this is not enough information for you, you'll need to describe specifically what data you have, what you want to do, what you tried, how you interpret it, what is missing, etc.

18 Comments

kash
kash on 16 Apr 2012
Tom please tell from the segmented image tumour part ,please tell how to classify to which class it belongs to such as malignin,Glioma, TlTc-SPECT etc
kash
kash on 16 Apr 2012
i have extracted all the features of tumours imagges such as malignin, Glioma, TlTc-SPECT etc an have stored in variable X,
next i have segmented image say ,have extracted features and stored in variable Y,now i want to compare these two and want to see to whixh class it belongs to for ex
X=[0.3 0.1 0;0.6 0.9 0;0.23 0.36 1;0.5 0.2 1]
Y=[0.23 0.36 ;0.5 0.2]
in Xthe first 2 columns are features extracted he 3rd is class,
in Y it is the features extraced from image fromthese two i have to compare and say to which class it belongs is it 0 or 1,its very urgent please help tom
Walter Roberson
Walter Roberson on 16 Apr 2012
http://www.mathworks.com/matlabcentral/answers/29922-why-your-question-is-not-urgent-or-an-emergency
Tom Lane
Tom Lane on 17 Apr 2012
We are not communicating. Please read my earlier answers and suggestions. In two of your most recent postings you have described 0/1/2 as normal/start/middle, then later described the classes as malignant/glioma/TITc-SPECT and requested classification as 0 or 1. I don't think I can help you.
Hello
Please I have a serious problem on the knn algorithm. I have a set of data (characteristics) to train in order to classify if there is a defect
For example:
A=[2.5, 3, 6 ,0.5,6.2;8.1, 5, 6, 10.5,2,3.6] % characteristic
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
B=['C','S','F','G','M','Q'] % out
Ny problem is how to build this give set and how to train it.
Please help me
Image Analyst
Image Analyst on 17 Nov 2022
@merlin toche what is your reference set (values with their corresponding classes), and what is the test set you want to classify into those classes based on the training set? Your example does not make sense.
Start a new discussion thread. Don't continue here.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
merlin toche
merlin toche on 17 Nov 2022
Thank'you my dear
i sign y for which social media?
Image Analyst is not available through social media (though he might possibly be willing to make an exception if you know some really really good BBQ recipes.)
merlin toche
merlin toche on 21 Nov 2022
Hello! how to train your data with knn? and build it? excuse me for my question, I am new to maching learning
best regards
Image Analyst
Image Analyst on 22 Nov 2022
@merlin toche see attached KNN demo
merlin toche
merlin toche on 22 Nov 2022
thanks you sir, i'm very satisfy with your explain very pedagogic .really very satisfied with your explanation with step-by-step example. if there was anything else beyond thank you I did. thank you very much sir. like who says thank you still asks, please how to determine the detection threshold. for example x=[4 6 7 5 8] and y=[3 7 8 5 8]; respectively the characteristics of current and voltage as income data and take outcome(pass or fail) in one array and z=(x,y)=(2,4) as data test
c=['open,'short','short','open','open']; I want to determine the threshold distance in order to detect if there is a defect or not. note that if there is a fault the distance (between the test datum and the learning data set) is greater than the threshold otherwise no fault. thank you. I really apologize for the inconvenience, I don't know much about programming and I have no choice but you. best regards
Image Analyst
Image Analyst on 22 Nov 2022
@merlin toche you have to give knn some examples. So if you have a set of ground truth values of current vs c (state or status) then you can then pass in some test data and use knn and the example data to classify the test data. For a simple thresholding situation you would probably not want to, or need to, use KNN. You could simply threshold and be done with it.
merlin toche
merlin toche on 23 Nov 2022
thank's you sir!
please can I have a small code (detect fault) of this example.for example x=[4 6 7 5 8] and y=[3 7 8 5 8]; respectively the characteristics of current and voltage as income data and take outcome(open or short) in one array and z=(x,y)=(2,4) as data test
c=['open,'short','short','open','open']
best regards
Image Analyst
Image Analyst on 23 Nov 2022
Yes, you said that already. I think you don't really understand the concept of K nearest neighbors. I think you need to study up on it first.
merlin toche
merlin toche on 23 Nov 2022
thanks you sir for your reply!
indeed, I understood the concept of kNN my problem lies in the writing of the detection code and construction of the circle which surrounds the new data and its k neighbors. sorry don't get upset. for 4 months I fight as I can, I only had the chance to fall on someone nice like you to help me implement. please i need it thank you
I'm willing to help but we need to explain and understand things first. Let's say, as you did, that your data is
xTraining = [4 6 7 5 8];
yTraining = [3 7 8 5 8];
% Now you say your classes are c=['open,'short','short','open','open']
% so let's make those class numbers.
trainingClasses = [1,2,2,1,1];
% Now let's plot them. First plot class 1 in blue.
plot(xTraining(trainingClasses == 1), yTraining(trainingClasses == 1), 'b.', 'MarkerSize', 30);
hold on
% Next plot class 2 in cyan.
plot(xTraining(trainingClasses == 2), yTraining(trainingClasses == 2), 'c.', 'MarkerSize', 30);
grid on;
xlabel('x');
ylabel('y');
% Now let's plot your unknown data point that we want to classify
zx = 2;
zy = 4;
plot(zx, zy, 'rs', 'LineWidth', 3, 'MarkerSize', 15);
legend('Training Class 1', 'Training Class 2', 'Unknown', 'Location', 'Northwest')
xy = [xTraining(:), yTraining(:)];
% Make model.
MdlKDT = KDTreeSearcher(xy)
MdlKDT =
KDTreeSearcher with properties: BucketSize: 50 Distance: 'euclidean' DistParameter: [] X: [5×2 double]
% Use model to predict unknown data.
closestIndex = knnsearch(MdlKDT, [zx, zy])
closestIndex = 1
estimatedClassNumber = trainingClasses(closestIndex)
estimatedClassNumber = 1
OK, look at the plot, at the blue and cyan dots. Those are your training points and if you're going to use KNN you're going to have to have at least two classes, in other words two clusters of points. You don't have any. There is nothing that really groups those 4 dots into two classes/clusters. I mean you did, but your classifications don't seem to make sense since your dark blue dots are not really near each other. That's OK though and that is one of the reasons to use KNN -- it doesn't require your clusters to be grouped nearby each other. It will work even for seemingly crazy, nonsensical classifications. Ok now look at your unknown point (the red square) - it doesn't really look like it belongs to either cluster since it's not really near the training points. That's why I say you should have hundreds of training points, and you should have some apriori knowledge of what cluster/class they truly are. But knn will say that it's closer to class 1 (dark blue spots) than class 2 (cyan spots) and assign it to class 1. I hope that explains it better.
merlin toche
merlin toche on 28 Nov 2022
Hi mister!
I come back to you for a detail. please let me calculate euclidean distance from several test data. how to proceed?
example, for a set of data split into training and test data
x1_train=[4 7 5 6 2;3 1 2.5 8.1 9;7.2 4.5 4 7 3]
y1_train=[1 4 5 3.2 1.5;6 1 5.3 4.7 8;0.9 11 15 3.6 9.8]
x1_test=[10 11.5 10.2 12 13.1; 1.3 2.4 5.9 3.4 6.4; 7.2 16 19 17 3.8 ]
y1_test=[1.6 8.2 4 6.2 10;6.1 2 6 5.3 9.4;1.9 0.5 13.5 8.4 14]
I want to calculate euclidean distance.
Another question, I wrote a code to partition my data using cvpartition, but in maltlab, I don't know how to train it in classifierlearner. sorry for my questions, I'm still learning please.
thank you sir for your help.
Cordially!

Sign in to comment.

Asked:

on 7 Apr 2012

Commented:

on 28 Nov 2022

Community Treasure Hunt

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

Start Hunting!