Not enough input arguments.

1 view (last 30 days)
hi there, I really need help, can someone tell me what is wrong with this code that shows the error "Not enough input arguments"
I should say my MATLAB version is 2018b.
function [trainedClassifier, validationAccuracy] = svm(trainingData)
% [trainedClassifier, 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 matrix with the same number of columns and data type
% as imported into the app.
%
% Output:
% trainedClassifier: a struct containing the trained classifier. The
% struct contains various fields with information about the trained
% classifier.
%
% trainedClassifier.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:
% [trainedClassifier, validationAccuracy] = trainClassifier(T)
%
% To make predictions with the returned 'trainedClassifier' on new data T2,
% use
% yfit = trainedClassifier.predictFcn(T2)
%
% T2 must be a matrix containing only the predictor columns used for
% training. For details, enter:
% trainedClassifier.HowToPredict
% Auto-generated by MATLAB on 27-Nov-2022 19:35:46
% Extract predictors and response
% This code processes the data into the right shape for training the
% model.
% Convert input to table
inputTable = array2table(trainingData, 'VariableNames', {'column_1', 'column_2'});
predictorNames = {'column_1'};
predictors = inputTable(:, predictorNames);
response = inputTable.column_2;
isCategoricalPredictor = [false];
% Train a classifier
% This code specifies all the classifier options and trains the classifier.
template = templateSVM(...
'KernelFunction', 'linear', ...
'PolynomialOrder', [], ...
'KernelScale', 'auto', ...
'BoxConstraint', 1, ...
'Standardize', true);
classificationsvm = fitcecoc(...
predictors, ...
response, ...
'Learners', template, ...
'Coding', 'onevsone', ...
'ClassNames', [0; 10; 15; 20; 25; 27; 30; 32]);
% Create the result struct with predict function
predictorExtractionFcn = @(x) array2table(x, 'VariableNames', predictorNames);
svmPredictFcn = @(x) predict(classificationsvm, x);
trainedClassifier.predictFcn = @(x) svmPredictFcn(predictorExtractionFcn(x));
% Add additional fields to the result struct
trainedClassifier.ClassificationSVM = classificationsvm;
trainedClassifier.About = 'This struct is a trained model exported from Classification Learner R2017b.';
trainedClassifier.HowToPredict = sprintf('To make predictions on a new predictor column matrix, X, use: \n yfit = c.predictFcn(X) \nreplacing ''c'' with the name of the variable that is this struct, e.g. ''trainedModel''. \n \nX must contain exactly 1 columns because this model was trained using 1 predictors. \nX must contain only predictor columns in exactly the same order and format as your training \ndata. Do not include the response column or any columns you did not import into the app. \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.
% Convert input to table
inputTable = array2table(trainingData, 'VariableNames', {'column_1', 'column_2'});
predictorNames = {'column_1'};
predictors = inputTable(:, predictorNames);
response = inputTable.column_2;
isCategoricalPredictor = [false];
% Perform cross-validation
partitionedModel = crossval(trainedClassifier.ClassificationSVM, 'KFold', 2);
% Compute validation predictions
[validationPredictions, validationScores] = kfoldPredict(partitionedModel);
% Compute validation accuracy
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 31 Dec 2022
Here is the corrected code ('end' was missing and it looks like that it was not executed correctly with a necessary input data):
T = [randi([-100, 100], 100,1), randi([0, 255], 100,1)];
[trainedClassifier, validationAccuracy] = svm(T)
Warning: Unable to fit learner 14 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 16 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 17 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 18 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 20 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 21 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 22 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 26 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 27 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 28 (SVM) because: No class names are found in input labels.
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.
Warning: Unable to fit learner 8 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 9 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 11 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 12 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 13 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 14 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 16 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 17 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 18 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 20 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 21 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 22 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 26 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 27 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 28 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 2 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 3 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 4 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 5 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 6 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 7 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 14 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 15 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 16 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 17 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 18 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 19 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 20 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 21 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 22 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 23 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 24 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 25 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 26 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 27 (SVM) because: No class names are found in input labels.
Warning: Unable to fit learner 28 (SVM) because: No class names are found in input labels.
trainedClassifier = struct with fields:
predictFcn: @(x)svmPredictFcn(predictorExtractionFcn(x)) ClassificationSVM: [1×1 ClassificationECOC] About: 'This struct is a trained model exported from Classification Learner R2017b.' HowToPredict: 'To make predictions on a new predictor column matrix, X, use: ↵ yfit = c.predictFcn(X) ↵replacing 'c' with the name of the variable that is this struct, e.g. 'trainedModel'. ↵ ↵X must contain exactly 1 columns because this model was trained using 1 predictors. ↵X must contain only predictor columns in exactly the same order and format as your training ↵data. Do not include the response column or any columns you did not import into the app. ↵ ↵For more information, see How to predict using an exported model.'
validationAccuracy = 0.3333
function [trainedClassifier, validationAccuracy] = svm(trainingData)
% [trainedClassifier, 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 matrix with the same number of columns and data type
% as imported into the app.
%
% Output:
% trainedClassifier: a struct containing the trained classifier. The
% struct contains various fields with information about the trained
% classifier.
%
% trainedClassifier.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:
% [trainedClassifier, validationAccuracy] = trainClassifier(T)
%
% To make predictions with the returned 'trainedClassifier' on new data T2,
% use
% yfit = trainedClassifier.predictFcn(T2)
%
% T2 must be a matrix containing only the predictor columns used for
% training. For details, enter:
% trainedClassifier.HowToPredict
% Auto-generated by MATLAB on 27-Nov-2022 19:35:46
% Extract predictors and response
% This code processes the data into the right shape for training the
% model.
% Convert input to table
inputTable = array2table(trainingData, 'VariableNames', {'column_1', 'column_2'});
predictorNames = {'column_1'};
predictors = inputTable(:, predictorNames);
response = inputTable.column_2;
isCategoricalPredictor = [false];
% Train a classifier
% This code specifies all the classifier options and trains the classifier.
template = templateSVM(...
'KernelFunction', 'linear', ...
'PolynomialOrder', [], ...
'KernelScale', 'auto', ...
'BoxConstraint', 1, ...
'Standardize', true);
classificationsvm = fitcecoc(...
predictors, ...
response, ...
'Learners', template, ...
'Coding', 'onevsone', ...
'ClassNames', [0; 10; 15; 20; 25; 27; 30; 32]);
% Create the result struct with predict function
predictorExtractionFcn = @(x) array2table(x, 'VariableNames', predictorNames);
svmPredictFcn = @(x) predict(classificationsvm, x);
trainedClassifier.predictFcn = @(x) svmPredictFcn(predictorExtractionFcn(x));
% Add additional fields to the result struct
trainedClassifier.ClassificationSVM = classificationsvm;
trainedClassifier.About = 'This struct is a trained model exported from Classification Learner R2017b.';
trainedClassifier.HowToPredict = sprintf('To make predictions on a new predictor column matrix, X, use: \n yfit = c.predictFcn(X) \nreplacing ''c'' with the name of the variable that is this struct, e.g. ''trainedModel''. \n \nX must contain exactly 1 columns because this model was trained using 1 predictors. \nX must contain only predictor columns in exactly the same order and format as your training \ndata. Do not include the response column or any columns you did not import into the app. \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.
% Convert input to table
inputTable = array2table(trainingData, 'VariableNames', {'column_1', 'column_2'});
predictorNames = {'column_1'};
predictors = inputTable(:, predictorNames);
response = inputTable.column_2;
isCategoricalPredictor = [false];
% Perform cross-validation
partitionedModel = crossval(trainedClassifier.ClassificationSVM, 'KFold', 2);
% Compute validation predictions
[validationPredictions, validationScores] = kfoldPredict(partitionedModel);
% Compute validation accuracy
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');
end
  1 Comment
Elnaz Yousefi
Elnaz Yousefi on 4 Jan 2023
predictFcn: [function_handle]
ClassificationSVM: [1×1 ClassificationECOC]
About: 'This struct is a trained model exported from Classification Learner R2017b.'
HowToPredict: 'To make predictions on a new predictor column matrix, X, use: ↵ yfit = c.predictFcn(X) ↵replacing 'c' with the name of the variable that is this struct, e.g. 'trainedModel'. ↵ ↵X must contain exactly 1 columns because this model was trained using 1 predictors. ↵X must contain only predictor columns in exactly the same order and format as your training ↵data. Do not include the response column or any columns you did not import into the app. ↵ ↵For more information, see How to predict using an exported model.'
validationAccuracy =
0
why it happened? and one more question, what exactly should I insert in the section of 'ClassNames', [0; 10; 15; 20; 25; 27; 30; 32]);

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 31 Dec 2022
How are you calling this function? Did you do
trainingData = % however you assigned it.
% Now pass training data into the svm function?
[trainedClassifier, validationAccuracy] = svm(trainingData)
or did you do
[trainedClassifier, validationAccuracy] = svm()
or did you simply click the green run triangle, in which case it won't automatically pass in anything for trainingData? You can't simply click the run button. You must assign trainingData to something then call svm from the command line, or another m-file, or even the same m-file if you call it before you have that "function" line of code.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!