how to predict test data from trained model

7 visualizaciones (últimos 30 días)
Hira Abid
Hira Abid el 10 de Jul. de 2018
Comentada: Warid Islam el 20 de Jun. de 2020
i'm making a project in which i have to classify hand gestures. i have a feature file(made in excel). i have trained a model using classification learner app. now prediction is supposed to be done by getting a new image of hand gesture and calculating all those features in an array. now i want that array as testing data. i am unable to predict that testing data from already trained model.i have changed array to matrix as well as table but failed. please help me out.
  2 comentarios
Sammit Jain
Sammit Jain el 10 de Jul. de 2018
Hi, I think it'd help a lot if you could share some information about the dimensions of your dataset. Given that, the usual way to go about it would be to feed your features into the predict function in the same way as you put your training data.
Hira Abid
Hira Abid el 10 de Jul. de 2018
Editada: Hira Abid el 10 de Jul. de 2018
this is auto generated code for training
function [trainedClassifier, resubstitutionAccuracy] = trainClassifier(trainingData)
% trainClassifier(trainingData)
% returns a trained classifier and its accuracy.
% This code recreates the classification model trained in
% Classification Learner app.
%
% Input:
% trainingData: the training data of same data type as imported
% in the app (table or matrix).
%
% 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. It takes an input of the same form as this training
% code (table or matrix) and returns predictions for the response.
% If you supply a matrix, include only the predictors columns (or
% rows).
%
% resubstitutionAccuracy: 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, resubstitutionAccuracy] = trainClassifier(T)
%
% To make predictions with the returned 'trainedClassifier' on new data T,
% use
% yfit = trainedClassifier.predictFcn(T)
%
% To automate training the same classifier with new data, or to learn how
% to programmatically train classifiers, examine the generated code.
% Auto-generated by MATLAB on 10-Jul-2018 09:47:17
% Extract predictors and response
% This code processes the data into the right shape for training the
% classifier.
inputTable = trainingData;
predictorNames = {'acpect_ratio', 'rectangularity', 'convex_area_ratio', 'eccenticity', 'diameter', 'form_factor', 'narrow_factor', 'perimeter_ratio_length_width', 'solidity', 'circularity', 'irregularity', 'm1', 'a1', 'm2', 'a2', 'm3', 'a3', 'm4', 'a4', 'm5', 'a5', 'm6', 'a6', 'm7', 'a7', 'm8', 'a8', 'm9', 'a9', 'cm1', 'cm2', 'cm3', 'cm4', 'cm5', 'cm6', 'cm7'};
predictors = inputTable(:, predictorNames);
response = inputTable.CLASS;
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];
% Train a classifier
% This code specifies all the classifier options and trains the classifier.
template = templateSVM(...
'KernelFunction', 'gaussian', ...
'PolynomialOrder', [], ...
'KernelScale', 1.5, ...
'BoxConstraint', 1, ...
'Standardize', true);
classificationSVM = fitcecoc(...
predictors, ...
response, ...
'Learners', template, ...
'Coding', 'onevsone', ...
'ClassNames', [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24]);
% Create the result struct with predict function
predictorExtractionFcn = @(t) t(:, predictorNames);
svmPredictFcn = @(x) predict(classificationSVM, x);
trainedClassifier.predictFcn = @(x) svmPredictFcn(predictorExtractionFcn(x));
% Add additional fields to the result struct
trainedClassifier.RequiredVariables = {'acpect_ratio', 'rectangularity', 'convex_area_ratio', 'eccenticity', 'diameter', 'form_factor', 'narrow_factor', 'perimeter_ratio_length_width', 'solidity', 'circularity', 'irregularity', 'm1', 'a1', 'm2', 'a2', 'm3', 'a3', 'm4', 'a4', 'm5', 'a5', 'm6', 'a6', 'm7', 'a7', 'm8', 'a8', 'm9', 'a9', 'cm1', 'cm2', 'cm3', 'cm4', 'cm5', 'cm6', 'cm7'};
trainedClassifier.ClassificationSVM = classificationSVM;
trainedClassifier.About = 'This struct is a trained classifier exported from Classification Learner R2016a.';
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. ''trainedClassifier''. \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
% classifier.
inputTable = trainingData;
predictorNames = {'acpect_ratio', 'rectangularity', 'convex_area_ratio', 'eccenticity', 'diameter', 'form_factor', 'narrow_factor', 'perimeter_ratio_length_width', 'solidity', 'circularity', 'irregularity', 'm1', 'a1', 'm2', 'a2', 'm3', 'a3', 'm4', 'a4', 'm5', 'a5', 'm6', 'a6', 'm7', 'a7', 'm8', 'a8', 'm9', 'a9', 'cm1', 'cm2', 'cm3', 'cm4', 'cm5', 'cm6', 'cm7'};
predictors = inputTable(:, predictorNames);
response = inputTable.CLASS;
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];
% Compute resubstitution accuracy
resubstitutionAccuracy = 1 - resubLoss(trainedClassifier.ClassificationSVM, 'LossFun', 'ClassifError');
% Compute resubstitution predictions and scores
[resubstitutionPredictions, resubstitutionScores] = predict(trainedClassifier.ClassificationSVM, predictors);
and calling this fucnction as
T = xlsread('traindata.xlsx', 1,'B1:AK385');
[trainedClassifier, resubstitutionAccuracy] = trainClassifier(T)
where the first row of excel contains name of feature. and getting this error Function 'subsindex' is not defined for values of class 'cell'. in line: predictors = inputTable(:, predictorNames);

Iniciar sesión para comentar.

Respuestas (1)

Rahul Madan Raju
Rahul Madan Raju el 9 de En. de 2019
predictors = inputTable(:, predictorNames{:})
You can try this code at the place of 'predictors = inputTable(:, predictorNames)'
  2 comentarios
ferkous ahmed
ferkous ahmed el 17 de En. de 2019
Try to make sure that all the data contains a comma instead of a dot
Thanks
Warid Islam
Warid Islam el 20 de Jun. de 2020
Hi guys,
I am having a smimilar problem where I want to test the model on a test image. I used the following line of code.
signalTemp2 = trainedModel.predictFcn(statsArray1);
But I get the following error.
Unable to use a value of type 'cell' as an index.
Error in mlearnapp.internal.model.DatasetSpecification>@(t)t(:,predictorNames) (line 156)
extractPredictorsFromTableFcn = @(t) t(:,predictorNames);
Error in mlearnapp.internal.model.DatasetSpecification>@(x)extractPredictorsFromTableFcn(x) (line 161)
predictorExtractionFcn = @(x) extractPredictorsFromTableFcn(x);
Error in mlearnapp.internal.model.DatasetSpecification>@(x)exportableModel.predictFcn(predictorExtractionFcn(x)) (line 165)
newExportableModel.predictFcn = @(x) exportableModel.predictFcn(predictorExtractionFcn(x));

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by