How to save k-NN model?
3 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I couldn't use "saveCompactModel" command for k-NN model (fitcknn) in version 2017a. I think it's available for version 2019a.
But now, I using version 2017a. How to save k-NN model and then check k-NN model with a new dataset?
Could Someone help solve my problem? Pls and Thanks!
Error message!
Undefined function 'toStruct' for input arguments of type 'ClassificationKNN'.
Error in saveCompactModel (line 17)
compactStruct = toStruct(compactObj); %#ok<NASGU>
Error in simpleKNN (line 31)
saveCompactModel(classificationKNN,'mykNN');
This is my code:
clear all;
%% preparing data
load('IrisFlower.mat');
output = grp2idx(Y);
rand_num = randperm(size(Xnew,1));
% training 70% testing 30%
X_train = Xnew(rand_num(1:round(0.7*length(rand_num))),:);
Y_train = output(rand_num(1:round(0.7*length(rand_num))),:);
X_test = Xnew(rand_num(round(0.7*length(rand_num))+1:end),:);
y_test = output(rand_num(round(0.7*length(rand_num))+1:end),:);
%% Train a classifier
% This code specifies all the classifier options and trains the classifier.
classificationKNN = fitcknn(...
X_train, ...
Y_train, ...
'Distance', 'Euclidean', ...
'Exponent', [], ...
'NumNeighbors', 3, ...
'DistanceWeight', 'Equal', ...
'Standardize', true);
%% Compute validation accuracy
% Perform cross-validation
partitionedModel = crossval(classificationKNN, 'KFold', 10);
% Compute validation predictions
[validationPredictions, validationScores] = kfoldPredict(partitionedModel);
% Compute validation accuracy
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');
%% save model and check model with new dataset
saveCompactModel(classificationKNN,'mykNN');
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!