www.gusucode.com > stats 源码程序 matlab案例代码 > stats/TrainAKNNClassifierExample.m
%% Train a _k_-Nearest Neighbor Classifier % Construct a _k_-nearest neighbor classifier for Fisher's iris data, where % _k_, the number of nearest neighbors in the predictors, is 5. %% % Load Fisher's iris data. load fisheriris X = meas; Y = species; %% % |X| is a numeric matrix that contains four petal measurements for 150 % irises. |Y| is a cell array of character vectors that contains the corresponding % iris species. %% % Train a 5-nearest neighbors classifier. It is good practice to % standardize noncategorical predictor data. Mdl = fitcknn(X,Y,'NumNeighbors',5,'Standardize',1) %% % |Mdl| is a trained |ClassificationKNN| classifier, and some of its % properties display in the Command Window. %% % To access the properties of |Mdl|, use dot notation. Mdl.ClassNames Mdl.Prior %% % |Mdl.Prior| contains the class prior probabilities, which are settable using % the name-value pair argument |'Prior'| in |fitcknn|. The order of the % class prior probabilities corresponds to the order of the classes in % |Mdl.ClassNames|. By default, the prior probabilities are the % respective relative frequencies of the classes in the data. %% % You can also reset the prior probabilities after training. For example, % set the prior probabilities to 0.5, 0.2, and 0.3 respectively. Mdl.Prior = [0.5 0.2 0.3]; %% % You can pass |Mdl| to, for example, <docid:stats_ug.bs85nou> to label new measurements, or % <docid:stats_ug.bs85m95> to cross validate the classifier.