필터 지우기
필터 지우기

How to save k-NN model?

조회 수: 4 (최근 30일)
Le Truong An
Le Truong An 2019년 7월 16일
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개)

카테고리

Help CenterFile 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!

Translated by