How to save k-NN model?
1 view (last 30 days)
Show older comments
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 Comments
Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!