How to solve an error using a function generated by Classification Learner
1 回表示 (過去 30 日間)
古いコメントを表示
I am using a function that was generated in the Classification Learner App:
function [trainedClassifierLinDisLinDis, validationAccuracy] = trainClassifierLinDis(trainingData)
% [trainedClassifierLinDis, validationAccuracy] = trainClassifier(trainingData)
% Returns a trained classifier and its accuracy. This code recreates the
% classification model trained in Classification Learner app. Use the
% generated code to automate training the same model with new data, or to
% learn how to programmatically train models.
%
% Input:
% trainingData: A table containing the same predictor and response
% columns as those imported into the app.
%
% Output:
% trainedClassifierLinDis: A struct containing the trained classifier. The
% struct contains various fields with information about the trained
% classifier.
%
% trainedClassifierLinDis.predictFcn: A function to make predictions on new
% data.
%
% validationAccuracy: A double containing the accuracy in percent. In
% the app, the History list displays this overall accuracy score for
% each model.
%
% Use the code to train the model with new data. To retrain your
% classifier, call the function from the command line with your original
% data or new data as the input argument trainingData.
%
% For example, to retrain a classifier trained with the original data set
% T, enter:
% [trainedClassifierLinDis, validationAccuracy] = trainClassifierLinDis(T)
%
% To make predictions with the returned 'trainedClassifierLinDis' on new data T2,
% use
% yfit = trainedClassifierLinDis.predictFcn(T2)
%
% T2 must be a table containing at least the same predictor columns as used
% during training. For details, enter:
% trainedClassifierLinDis.HowToPredict
% Auto-generated by MATLAB on 30-Aug-2021 16:11:50
% Extract predictors and response
% This code processes the data into the right shape for training the
% model.
inputTable = trainingData;
predictorNames = {'scenedata1', 'scenedata2', 'scenedata3', 'scenedata4', 'scenedata5', 'scenedata6', 'scenedata7', 'scenedata8', 'scenedata9', 'scenedata10', 'scenedata11', 'scenedata12', 'scenedata13', 'scenedata14', 'scenedata15', 'scenedata16', 'scenedata17', 'scenedata18', 'scenedata19', 'scenedata20', 'scenedata21', 'scenedata22', 'scenedata23', 'scenedata24', 'scenedata25', 'scenedata26', 'scenedata27', 'scenedata28', 'scenedata29', 'scenedata30', 'scenedata31', 'scenedata32', 'scenedata33', 'scenedata34', 'scenedata35', 'scenedata36', 'scenedata37', 'scenedata38', 'scenedata39', 'scenedata40', 'scenedata41', 'scenedata42', 'scenedata43', 'scenedata44', 'scenedata45', 'scenedata46', 'scenedata47', 'scenedata48', 'scenedata49', 'scenedata50', 'scenedata51', 'scenedata52', 'scenedata53', 'scenedata54', 'scenedata55', 'scenedata56', 'scenedata57', 'scenedata58', 'scenedata59', 'scenedata60', 'scenedata61', 'scenedata62', 'scenedata63', 'scenedata64', 'scenedata65', 'scenedata66', 'scenedata67', 'scenedata68', 'scenedata69', 'scenedata70', 'scenedata71', 'scenedata72', 'scenedata73', 'scenedata74', 'scenedata75', 'scenedata76', 'scenedata77', 'scenedata78', 'scenedata79', 'scenedata80', 'scenedata81', 'scenedata82', 'scenedata83', 'scenedata84', 'scenedata85', 'scenedata86', 'scenedata87', 'scenedata88', 'scenedata89', 'scenedata90', 'scenedata91', 'scenedata92', 'scenedata93', 'scenedata94', 'scenedata95', 'scenedata96', 'scenedata97', 'scenedata98', 'scenedata99', 'scenedata100', 'scenedata101', 'scenedata102', 'scenedata103', 'scenedata104', 'scenedata105', 'scenedata106', 'scenedata107', 'scenedata108', 'scenedata109', 'scenedata110', 'scenedata111', 'scenedata112', 'scenedata113', 'scenedata114', 'scenedata115', 'scenedata116', 'scenedata117', 'scenedata118', 'scenedata119', 'scenedata120', 'scenedata121', 'scenedata122', 'scenedata123', 'scenedata124', 'scenedata125', 'scenedata126', 'scenedata127', 'scenedata128', 'scenedata129', 'scenedata130', 'scenedata131', 'scenedata132', 'scenedata133', 'scenedata134', 'scenedata135', 'scenedata136', 'scenedata137', 'scenedata138', 'scenedata139', 'scenedata140', 'scenedata141', 'scenedata142', 'scenedata143', 'scenedata144', 'scenedata145', 'scenedata146', 'scenedata147', 'scenedata148', 'scenedata149', 'scenedata150', 'scenedata151', 'scenedata152', 'scenedata153', 'scenedata154', 'scenedata155', 'scenedata156', 'scenedata157', 'scenedata158', 'scenedata159', 'scenedata160', 'scenedata161', 'scenedata162', 'scenedata163', 'scenedata164', 'scenedata165', 'scenedata166', 'scenedata167', 'scenedata168', 'scenedata169', 'scenedata170', 'scenedata171', 'scenedata172', 'scenedata173', 'scenedata174', 'scenedata175', 'scenedata176', 'scenedata177', 'scenedata178', 'scenedata179', 'scenedata180', 'scenedata181', 'scenedata182', 'scenedata183', 'scenedata184', 'scenedata185', 'scenedata186', 'scenedata187', 'scenedata188', 'scenedata189', 'scenedata190', 'scenedata191', 'scenedata192', 'scenedata193', 'scenedata194', 'scenedata195', 'scenedata196', 'scenedata197', 'scenedata198', 'scenedata199', 'scenedata200', 'scenedata201', 'scenedata202', 'scenedata203', 'scenedata204', 'scenedata205', 'scenedata206', 'scenedata207', 'scenedata208', 'scenedata209', 'scenedata210', 'scenedata211', 'scenedata212', 'scenedata213', 'scenedata214', 'scenedata215', 'scenedata216', 'scenedata217', 'scenedata218', 'scenedata219', 'scenedata220', 'scenedata221', 'scenedata222', 'scenedata223', 'scenedata224', 'scenedata225', 'scenedata226', 'scenedata227', 'scenedata228', 'scenedata229', 'scenedata230', 'scenedata231', 'scenedata232', 'scenedata233', 'scenedata234', 'scenedata235', 'scenedata236', 'scenedata237', 'scenedata238', 'scenedata239', 'scenedata240', 'scenedata241', 'scenedata242', 'scenedata243', 'scenedata244', 'scenedata245', 'scenedata246', 'scenedata247', 'scenedata248', 'scenedata249', 'scenedata250'};
predictors = inputTable(:, predictorNames);
response = inputTable.sceneType;
isCategoricalPredictor = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false];
% Train a classifier
% This code specifies all the classifier options and trains the classifier.
classificationDiscriminant = fitcdiscr(...
predictors, ...
response, ...
'DiscrimType', 'linear', ...
'Gamma', 0, ...
'FillCoeffs', 'off', ...
'ClassNames', categorical({'MS T1 No Contrast'; 'MS T1 w: contrast'; 'Normal T1 No Contrast'; 'Normal T1 w:contrast'; 'T2 MS No Contrast'; 'T2 Normal No Contrast'}));
% Create the result struct with predict function
predictorExtractionFcn = @(t) t(:, predictorNames);
discriminantPredictFcn = @(x) predict(classificationDiscriminant, x);
trainedClassifierLinDis.predictFcn = @(x) discriminantPredictFcn(predictorExtractionFcn(x));
% Add additional fields to the result struct
trainedClassifierLinDis.RequiredVariables = {'scenedata1', 'scenedata10', 'scenedata100', 'scenedata101', 'scenedata102', 'scenedata103', 'scenedata104', 'scenedata105', 'scenedata106', 'scenedata107', 'scenedata108', 'scenedata109', 'scenedata11', 'scenedata110', 'scenedata111', 'scenedata112', 'scenedata113', 'scenedata114', 'scenedata115', 'scenedata116', 'scenedata117', 'scenedata118', 'scenedata119', 'scenedata12', 'scenedata120', 'scenedata121', 'scenedata122', 'scenedata123', 'scenedata124', 'scenedata125', 'scenedata126', 'scenedata127', 'scenedata128', 'scenedata129', 'scenedata13', 'scenedata130', 'scenedata131', 'scenedata132', 'scenedata133', 'scenedata134', 'scenedata135', 'scenedata136', 'scenedata137', 'scenedata138', 'scenedata139', 'scenedata14', 'scenedata140', 'scenedata141', 'scenedata142', 'scenedata143', 'scenedata144', 'scenedata145', 'scenedata146', 'scenedata147', 'scenedata148', 'scenedata149', 'scenedata15', 'scenedata150', 'scenedata151', 'scenedata152', 'scenedata153', 'scenedata154', 'scenedata155', 'scenedata156', 'scenedata157', 'scenedata158', 'scenedata159', 'scenedata16', 'scenedata160', 'scenedata161', 'scenedata162', 'scenedata163', 'scenedata164', 'scenedata165', 'scenedata166', 'scenedata167', 'scenedata168', 'scenedata169', 'scenedata17', 'scenedata170', 'scenedata171', 'scenedata172', 'scenedata173', 'scenedata174', 'scenedata175', 'scenedata176', 'scenedata177', 'scenedata178', 'scenedata179', 'scenedata18', 'scenedata180', 'scenedata181', 'scenedata182', 'scenedata183', 'scenedata184', 'scenedata185', 'scenedata186', 'scenedata187', 'scenedata188', 'scenedata189', 'scenedata19', 'scenedata190', 'scenedata191', 'scenedata192', 'scenedata193', 'scenedata194', 'scenedata195', 'scenedata196', 'scenedata197', 'scenedata198', 'scenedata199', 'scenedata2', 'scenedata20', 'scenedata200', 'scenedata201', 'scenedata202', 'scenedata203', 'scenedata204', 'scenedata205', 'scenedata206', 'scenedata207', 'scenedata208', 'scenedata209', 'scenedata21', 'scenedata210', 'scenedata211', 'scenedata212', 'scenedata213', 'scenedata214', 'scenedata215', 'scenedata216', 'scenedata217', 'scenedata218', 'scenedata219', 'scenedata22', 'scenedata220', 'scenedata221', 'scenedata222', 'scenedata223', 'scenedata224', 'scenedata225', 'scenedata226', 'scenedata227', 'scenedata228', 'scenedata229', 'scenedata23', 'scenedata230', 'scenedata231', 'scenedata232', 'scenedata233', 'scenedata234', 'scenedata235', 'scenedata236', 'scenedata237', 'scenedata238', 'scenedata239', 'scenedata24', 'scenedata240', 'scenedata241', 'scenedata242', 'scenedata243', 'scenedata244', 'scenedata245', 'scenedata246', 'scenedata247', 'scenedata248', 'scenedata249', 'scenedata25', 'scenedata250', 'scenedata26', 'scenedata27', 'scenedata28', 'scenedata29', 'scenedata3', 'scenedata30', 'scenedata31', 'scenedata32', 'scenedata33', 'scenedata34', 'scenedata35', 'scenedata36', 'scenedata37', 'scenedata38', 'scenedata39', 'scenedata4', 'scenedata40', 'scenedata41', 'scenedata42', 'scenedata43', 'scenedata44', 'scenedata45', 'scenedata46', 'scenedata47', 'scenedata48', 'scenedata49', 'scenedata5', 'scenedata50', 'scenedata51', 'scenedata52', 'scenedata53', 'scenedata54', 'scenedata55', 'scenedata56', 'scenedata57', 'scenedata58', 'scenedata59', 'scenedata6', 'scenedata60', 'scenedata61', 'scenedata62', 'scenedata63', 'scenedata64', 'scenedata65', 'scenedata66', 'scenedata67', 'scenedata68', 'scenedata69', 'scenedata7', 'scenedata70', 'scenedata71', 'scenedata72', 'scenedata73', 'scenedata74', 'scenedata75', 'scenedata76', 'scenedata77', 'scenedata78', 'scenedata79', 'scenedata8', 'scenedata80', 'scenedata81', 'scenedata82', 'scenedata83', 'scenedata84', 'scenedata85', 'scenedata86', 'scenedata87', 'scenedata88', 'scenedata89', 'scenedata9', 'scenedata90', 'scenedata91', 'scenedata92', 'scenedata93', 'scenedata94', 'scenedata95', 'scenedata96', 'scenedata97', 'scenedata98', 'scenedata99'};
trainedClassifierLinDis.ClassificationDiscriminant = classificationDiscriminant;
trainedClassifierLinDis.About = 'This struct is a trained model exported from Classification Learner R2020a.';
trainedClassifierLinDis.HowToPredict = sprintf('To make predictions on a new table, T, use: \n yfit = c.predictFcn(T) \nreplacing ''c'' with the name of the variable that is this struct, e.g. ''trainedModel''. \n \nThe table, T, must contain the variables returned by: \n c.RequiredVariables \nVariable formats (e.g. matrix/vector, datatype) must match the original training data. \nAdditional variables are ignored. \n \nFor more information, see <a href="matlab:helpview(fullfile(docroot, ''stats'', ''stats.map''), ''appclassification_exportmodeltoworkspace'')">How to predict using an exported model</a>.');
% Extract predictors and response
% This code processes the data into the right shape for training the
% model.
inputTable = trainingData;
predictorNames = {'scenedata1', 'scenedata2', 'scenedata3', 'scenedata4', 'scenedata5', 'scenedata6', 'scenedata7', 'scenedata8', 'scenedata9', 'scenedata10', 'scenedata11', 'scenedata12', 'scenedata13', 'scenedata14', 'scenedata15', 'scenedata16', 'scenedata17', 'scenedata18', 'scenedata19', 'scenedata20', 'scenedata21', 'scenedata22', 'scenedata23', 'scenedata24', 'scenedata25', 'scenedata26', 'scenedata27', 'scenedata28', 'scenedata29', 'scenedata30', 'scenedata31', 'scenedata32', 'scenedata33', 'scenedata34', 'scenedata35', 'scenedata36', 'scenedata37', 'scenedata38', 'scenedata39', 'scenedata40', 'scenedata41', 'scenedata42', 'scenedata43', 'scenedata44', 'scenedata45', 'scenedata46', 'scenedata47', 'scenedata48', 'scenedata49', 'scenedata50', 'scenedata51', 'scenedata52', 'scenedata53', 'scenedata54', 'scenedata55', 'scenedata56', 'scenedata57', 'scenedata58', 'scenedata59', 'scenedata60', 'scenedata61', 'scenedata62', 'scenedata63', 'scenedata64', 'scenedata65', 'scenedata66', 'scenedata67', 'scenedata68', 'scenedata69', 'scenedata70', 'scenedata71', 'scenedata72', 'scenedata73', 'scenedata74', 'scenedata75', 'scenedata76', 'scenedata77', 'scenedata78', 'scenedata79', 'scenedata80', 'scenedata81', 'scenedata82', 'scenedata83', 'scenedata84', 'scenedata85', 'scenedata86', 'scenedata87', 'scenedata88', 'scenedata89', 'scenedata90', 'scenedata91', 'scenedata92', 'scenedata93', 'scenedata94', 'scenedata95', 'scenedata96', 'scenedata97', 'scenedata98', 'scenedata99', 'scenedata100', 'scenedata101', 'scenedata102', 'scenedata103', 'scenedata104', 'scenedata105', 'scenedata106', 'scenedata107', 'scenedata108', 'scenedata109', 'scenedata110', 'scenedata111', 'scenedata112', 'scenedata113', 'scenedata114', 'scenedata115', 'scenedata116', 'scenedata117', 'scenedata118', 'scenedata119', 'scenedata120', 'scenedata121', 'scenedata122', 'scenedata123', 'scenedata124', 'scenedata125', 'scenedata126', 'scenedata127', 'scenedata128', 'scenedata129', 'scenedata130', 'scenedata131', 'scenedata132', 'scenedata133', 'scenedata134', 'scenedata135', 'scenedata136', 'scenedata137', 'scenedata138', 'scenedata139', 'scenedata140', 'scenedata141', 'scenedata142', 'scenedata143', 'scenedata144', 'scenedata145', 'scenedata146', 'scenedata147', 'scenedata148', 'scenedata149', 'scenedata150', 'scenedata151', 'scenedata152', 'scenedata153', 'scenedata154', 'scenedata155', 'scenedata156', 'scenedata157', 'scenedata158', 'scenedata159', 'scenedata160', 'scenedata161', 'scenedata162', 'scenedata163', 'scenedata164', 'scenedata165', 'scenedata166', 'scenedata167', 'scenedata168', 'scenedata169', 'scenedata170', 'scenedata171', 'scenedata172', 'scenedata173', 'scenedata174', 'scenedata175', 'scenedata176', 'scenedata177', 'scenedata178', 'scenedata179', 'scenedata180', 'scenedata181', 'scenedata182', 'scenedata183', 'scenedata184', 'scenedata185', 'scenedata186', 'scenedata187', 'scenedata188', 'scenedata189', 'scenedata190', 'scenedata191', 'scenedata192', 'scenedata193', 'scenedata194', 'scenedata195', 'scenedata196', 'scenedata197', 'scenedata198', 'scenedata199', 'scenedata200', 'scenedata201', 'scenedata202', 'scenedata203', 'scenedata204', 'scenedata205', 'scenedata206', 'scenedata207', 'scenedata208', 'scenedata209', 'scenedata210', 'scenedata211', 'scenedata212', 'scenedata213', 'scenedata214', 'scenedata215', 'scenedata216', 'scenedata217', 'scenedata218', 'scenedata219', 'scenedata220', 'scenedata221', 'scenedata222', 'scenedata223', 'scenedata224', 'scenedata225', 'scenedata226', 'scenedata227', 'scenedata228', 'scenedata229', 'scenedata230', 'scenedata231', 'scenedata232', 'scenedata233', 'scenedata234', 'scenedata235', 'scenedata236', 'scenedata237', 'scenedata238', 'scenedata239', 'scenedata240', 'scenedata241', 'scenedata242', 'scenedata243', 'scenedata244', 'scenedata245', 'scenedata246', 'scenedata247', 'scenedata248', 'scenedata249', 'scenedata250'};
predictors = inputTable(:, predictorNames);
response = inputTable.sceneType;
isCategoricalPredictor = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false];
% Perform cross-validation
partitionedModel = crossval(trainedClassifierLinDis.ClassificationDiscriminant, 'KFold', 5);
% Compute validation predictions
[validationPredictions, validationScores] = kfoldPredict(partitionedModel);
% Compute validation accuracy
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');
I am trying to retrain this classifier. When I type in
trainClassifierLinDis(SceneImageData)
which was my original data set, I get this error:
Warning: One or more of the unique class values in GROUP is not present in one or
more folds. For classification problems, either remove this class from the data or
use N instead of GROUP to obtain nonstratified partitions. For regression problems
with continuous response, use N.
> In internal.stats.cvpartitionImpl>stra_kfoldcv (line 364)
In internal.stats/cvpartitionImpl/rerandom (line 315)
In internal.stats.cvpartitionInMemoryImpl (line 166)
In cvpartition (line 175)
In classreg.learning.generator.Partitioner (line 54)
In classreg.learning.modelparams/EnsembleParams/makeGenerator (line 374)
In classreg.learning.modelparams/EnsembleParams/fillDefaultParams (line 628)
In classreg.learning.modelparams/ModelParams/fillIfNeeded (line 93)
In classreg.learning.classif/FullClassificationModel (line 35)
In classreg.learning.classif/ClassificationEnsemble (line 70)
In classreg.learning.partition.ClassificationPartitionedModel (line 161)
In classreg.learning/FitTemplate/fit (line 263)
In ClassificationDiscriminant/crossval (line 656)
In trainClassifierLinDis (line 83)
Warning: Predictor scenedata28 has zero within-class variance. Either exclude this
predictor or set 'discrimType' to 'pseudoLinear' or 'diagLinear'.
> In classreg.learning.ensemble/Ensemble/fitWeakLearners (line 184)
In classreg.learning.classif/ClassificationEnsemble/fitEnsemble (line 369)
In classreg.learning.classif/ClassificationEnsemble (line 79)
In classreg.learning.partition.ClassificationPartitionedModel (line 161)
In classreg.learning/FitTemplate/fit (line 263)
In ClassificationDiscriminant/crossval (line 656)
In trainClassifierLinDis (line 83)
Can someone please clarify the issue? Thank you so much!
0 件のコメント
回答 (1 件)
Drew
2022 年 10 月 20 日
Based on the warning messages, it looks like there are a couple of issues in your dataset.
(1) The first warning sounds like you have an output class label listed at categorical({'MS T1 No Contrast'; 'MS T1 w: contrast'; 'Normal T1 No Contrast'; 'Normal T1 w:contrast'; 'T2 MS No Contrast'; 'T2 Normal No Contrast'}))
but which never occurs in the dataset.
(2) The second warning sounds like predictor scendata28 is a constant, since it has "zero within-class variance"
So, the solution is to:
(1) Remove output class for which there is no data, or get data for it
(2) Remove the predictor scenedata28, or get real (non-constant) data for it.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Classification Learner App についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!