Matlab2016b版本代码:
class = knnclassify(X_test, X_train, Y_train,k); % [X_train,X_test,Y_train, Y_test],X样本,Y标签,train训练,test测试
%k可以为1,2,3,4.....。X_test为测试数据,X_train,Y_train为训练集的样本和标签,k为设置的k临近值的k值
Matlab2019b对应的代码为:
mdl = fitcknn(X_train, Y_train,'NumNeighbors',k); %k为对应的1,2,3,4.....
class = predict(mdl,X_test);
网友评论