matrix 1x3 as input for testing data after train the model in classification learner

I have done with classification learner for training algorithm, i have explor to model to workspace for testing the data, my input data in classification learner are import from excel (table) but for testing i want to use user input, i come out with this code. (one more thing if i import data from excel file, the testing data can classifiy but i dont want this way)
H2_percentage=15.217
CH4_percentage=53.261
C2H6_percentage= 31.522
testing=[H2_percentage CH4_percentage C2H6_percentage];
yfit = FineKNN.predictFcn('testing');
but when i run that code come out with this error....

Answers (1)

yfit = FineKNN.predictFcn(testing);
You should use this instead.

7 Comments

this code can use if the testing data is from excel file
how about if the testing data is not from excel
It would work as long as the input is given in the format of [number number number].
The input data for training in table format but the testing data in variable fromat(user input the value)
Is there any other way to change the training input data as varible to classification learner (what I mean is the input data to classification learner is not come from excel)
Why don't you write a code by yourself?
It it is hard, then
  1. press small triangle under the 'export' button in classification learner and choose "function" or something
  2. You'll get a MATLAB code to train your model and then you now know where to make a small change to use your set of variables as training/testing data.
Note that if you train your model with a table, then you need to feed your test data in the same format.
function [trainedClassifier, validationAccuracy] = trainClassifier(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 table containing the same predictor and response
% columns as those 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 table containing at least the same predictor columns as used
% during training. For details, enter:
% trainedClassifier.HowToPredict
% Auto-generated by MATLAB on 10-Dec-2020 13:21:14
% Extract predictors and response
% This code processes the data into the right shape for training the
% model.
inputTable = trainingData;
predictorNames = {'H2_percentage', 'CH4_percentage', 'C2H6_percentage'};
predictors = inputTable(:, predictorNames);
response = inputTable.Classification;
isCategoricalPredictor = [false, false, false];
% Train a classifier
% This code specifies all the classifier options and trains the classifier.
classificationKNN = fitcknn(...
predictors, ...
response, ...
'Distance', 'Euclidean', ...
'Exponent', [], ...
'NumNeighbors', 1, ...
'DistanceWeight', 'Equal', ...
'Standardize', true, ...
'ClassNames', categorical({'Fault'; 'N/D'; 'Stray'}));
% Create the result struct with predict function
predictorExtractionFcn = @(t) t(:, predictorNames);
knnPredictFcn = @(x) predict(classificationKNN, x);
trainedClassifier.predictFcn = @(x) knnPredictFcn(predictorExtractionFcn(x));
% Add additional fields to the result struct
trainedClassifier.RequiredVariables = {'C2H6_percentage', 'CH4_percentage', 'H2_percentage'};
trainedClassifier.ClassificationKNN = classificationKNN;
trainedClassifier.About = 'This struct is a trained model exported from Classification Learner R2020a.';
trainedClassifier.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 = {'H2_percentage', 'CH4_percentage', 'C2H6_percentage'};
predictors = inputTable(:, predictorNames);
response = inputTable.Classification;
isCategoricalPredictor = [false, false, false];
% Perform cross-validation
partitionedModel = crossval(trainedClassifier.ClassificationKNN, 'KFold', 5);
% Compute validation predictions
[validationPredictions, validationScores] = kfoldPredict(partitionedModel);
% Compute validation accuracy
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');
here is the code can someone help because i am not good in coding...
How about convert test data into the right format?
H2_percentage=15.217 CH4_percentage=53.261 C2H6_percentage= 31.522 testing=[H2_percentage CH4_percentage C2H6_percentage];
%the format of testing data will be like the above code which asking user to enter data for testing

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!