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
回答(3 个)
Tom Lane
2012-4-7
0 个投票
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.
Tom Lane
2012-4-11
0 个投票
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 个评论
kash
2012-4-12
kash
2012-4-12
merlin toche
2022-11-16
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];
Walter Roberson
2022-11-17
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
2022-11-22
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
Tom Lane
2012-4-12
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 个评论
kash
2012-4-16
kash
2012-4-16
Walter Roberson
2012-4-16
http://www.mathworks.com/matlabcentral/answers/29922-why-your-question-is-not-urgent-or-an-emergency
Tom Lane
2012-4-17
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
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
2022-11-17
@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
2022-11-17
Thank'you my dear
i sign y for which social media?
Walter Roberson
2022-11-17
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
2022-11-21
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
2022-11-22
@merlin toche see attached KNN demo

merlin toche
2022-11-22
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
2022-11-22
@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
2022-11-23
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
2022-11-23
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
2022-11-23
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)
% Use model to predict unknown data.
closestIndex = knnsearch(MdlKDT, [zx, zy])
estimatedClassNumber = trainingClasses(closestIndex)
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
2022-11-28
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!
类别
在 帮助中心 和 File Exchange 中查找有关 Nearest Neighbors 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
