Take the data out after using Classification Learner App

Hi, I separated my data into training and testing data. I trained using the training data, then I tested using the testing data with the code below. It said "Unrecognized variable name 'Character'." And how can I take the data out after classification? Please help me. Thank guys.
testdata=readtable("ClassificationTestData.xlsx")
predictions = char(trainedModel.predictFcn(testdata))
% accuracy
iscorrect=predictions==cell2mat(string((testdata.Character)));
iscorrect=iscorrect(:,2);
accuracy = sum(iscorrect)*100/20;

 Accepted Answer

Huy, here is a complete demo along with instructions in the comments
%==============================================================================
% TRAINING
trainingData=readtable("ClassificationData.xlsx")
% The first 4 columns are the inputs.
tPredictors = trainingData(:, 1:4);
% The last column is the "answer/ground truth".
tResponse = trainingData{:, end};
% Now run Classification Learner app. Start a new session from workspace, and
% Select tPredictors as the "Data set variable" and
% select tResponse as the response.
% I did "All quick to train" and found the Fine Tree model was the best.
% I highlighted it and clicked the Export button to export trainingModel to the workspace.
% Then save it to a .mat file from the command line like this:
% save('trainedModel.mat', 'trainedModel')
%==============================================================================
% TESTING
% Now run it on some test data once you have the trainedModel.mat file created.
% First read in the model from the .mat file
fileName = fullfile(pwd, 'trainedModel.mat');
if ~isfile(fileName)
message = sprintf('Model file not found:\n%s\n\nAre you sure you exported it from Classification Learner', fileName);
uiwait(errordlg(message));
return;
end
s = load(fileName)
trainedModel = s.trainedModel;
% Read in test data
testData=readtable("ClassificationTestData.xlsx")
testPredictors = testData(:, 1:4); % First 4 columns are the model inputs.
testResponse = testData{:, end}; % Last column is the ground truth (correct values).
% Predict the values.
numTestValues = size(predictions, 1)
% Compare predictions to ground truth.
itsCorrect = zeros(numTestValues, 1);
for k = 1 : numTestValues
if isequal(predictions{k}, testResponse{k})
itsCorrect(k) = 1;
end
end
% accuracy
accuracy = sum(itsCorrect)*100/numTestValues

20 Comments

Hi I got the error like this please help meeee @Image Analyst
Unrecognized function or variable 'predictions'.
Error in testclassi (line 17)
numTestValues = size(predictions, 1)
You need to call the predictfcn() function
% ONLY DO THE FOLLOWING AFTER A MODEL HAS BEEN BUILT, EXPORTED, AND SAVED.
% After training and exporting the model, get the predictions.
matFileName = whateverItIs;
if ~isfile(matFileName)
message = sprintf('Did not find model mat file:\n%s', matFileName);
msgboxwarn(message);
return;
end
s = load(matFileName)
trainedModel = s.trainedModel
fn = fieldnames(trainedModel);
c = contains(fn, 'Regression');
regressionModelName = fn{find(c)};
% Predict the values from the data in the tPredictors table.
yfit = trainedModel.predictFcn(tPredictors);
Unrecognized function or variable 'soilToUse'.
Error in Untitled3 (line 3)
matFileName = sprintf('TrainedModel%s.mat', soilToUse);
still got this problem :(
Sorry, I just copied and pasted from my app. just replace that line with the name of your trainedModel mat file that you saved/exported when you were in Regression Learner.
I think this is Classification Learner right?
It's pretty similar no matter is you're doing Classification Learner or Regression Learning. I use both. Just make the obvious modifications for whichever one you're using. You can export "trainedModel" from both of them like from the command window (after you've exported):
save('trainedModel.mat', 'trainedModel');
Should I replace 'whateveritis' or another one? I just try to use matlab recently. I saved trainedModel like u told me
Yes, of course. What filename did you save trainedModel as? Just use that.
You assigned the filename to the model instead of a string that says where the model lives. You need to do
matFileName = fullfilw(pwd, 'trainedModel.mat');
if ~isfile(matFileName)
warningMessage = sprintf('Model file not found:\n', matFileName);
uiwait(errordlg(warningMessage));
return;
end
Is this a correct answer?
I still got the ame error when i added testing code @Image Analyst
yFit should be called predictions.
Can you tell more detail please :(
Just to get me up to date, can you attach your training m-file, your testing/prediction m-file, and your data again?
OK, that script is just for running test data through and required trainedModel.mat, so attach that since I'm not sure if it's the same one as I originally attached.
Try this:
% TRAINING
trainingData=readtable("ClassificationData.xlsx")
% The first 4 columns are the inputs.
tPredictors = trainingData(:, 1:4);
% The last column is the "answer/ground truth".
tResponse = trainingData{:, end};
matFileName = fullfile(pwd, 'trainedModel.mat');
if ~isfile(matFileName)
warningMessage = sprintf('Model file not found:\n', matFileName);
uiwait(errordlg(warningMessage));
return;
end;
s = load(matFileName)
trainedModel = s.trainedModel
fn = fieldnames(trainedModel);
c = contains(fn, 'Classification');
regressionModelName = fn{find(c)}
% Read test data in to a table.
testData=readtable("ClassificationTestData.xlsx")
testPredictors = testData(:, 1:4); % First 4 columns are the model inputs.
testResponse = testData{:, end}; % Last column is the ground truth (correct values).
% Predict the values from the data in the tPredictors table.
predictions = trainedModel.predictFcn(testPredictors);
% Predict the values.
numTestValues = size(predictions, 1)
% Compare predictions to ground truth.
itsCorrect = zeros(numTestValues, 1);
for k = 1 : numTestValues
if isequal(predictions{k}, testResponse{k})
itsCorrect(k) = 1;
end
end
% accuracy
accuracy = sum(itsCorrect)*100/numTestValues
Everything is being classified as Cluster5. Is that what you expect?
Hi but acctually i have to use the test data from the classification learner app but i can import the testing data into the data set. can you help me thankss

Sign in to comment.

More Answers (3)

You appear to be trying to use a variable name (Character) that does not exist in your test data file. To me, it appears the available variable names are Power, WingSpeed, WindDirect, WindSpeed, and Cluster.

2 Comments

So you have any comment on that error please help me
I did. You need to use one of the actual variable names in your table, which appear to be Power, WingSpeed, WindDirect, WindSpeed, and Cluster.

Sign in to comment.

You train using Power, WingSpeed, WindDirect, WindSpeed as the predictors, and Cluster as the response (ground truth).
Now when you go to use the trainedModel, you need to pass in only the predictors (columns 1-4 which are Power, WingSpeed, WindDirect, WindSpeed). Don't pass in Cluster as a predictor since the trained model won't be expecting that.
Your predictions are trainedModel.predictFcn(testdata). That's how you "can take the data out after classification". I'm not sure you need to cast it to char. And there is no column in your testdata table called "Character". Again, it needs to be Cluster since that's the column where the ground truth response for your test data is stored.
Attach trainedModel.mat if you need more help.

3 Comments

Hi I think I didnt get the point. So I have to delete the column Cluster in the Test data right?
And the error Unrecognized variable name 'Character' how can i fix that :(, thank youu
And in the WorkSpace I just got trainedModel 1x1 Struct
Save the model to a .mat file
save('trainedModel.mat', 'trainedModel');
then attach trainedModel.mat with the paper clip icon.
Character is not a field of the table so you need to extract the ground truth labels like this
testGroundTruth = testdata.Cluster;
iscorrect= predictions == testGroundTruth;
That may not work. I won't know unless I get the model. Or I'd have to train the model myself, but I don't know which model you chose.
Also make sure you didn't train your model with Cluster being one of the predictors as well as the response. Otherwise it will ignore your other 4 inputs since Cluster is, obviously, a perfect predictor of the response.

Sign in to comment.

Hi thank you alot but when I added tPredictors into the data variables I couldn't add tResponse as a Response

8 Comments

Run this code:
%==============================================================================
% TRAINING
trainingData=readtable("ClassificationData.xlsx");
% The first 4 columns are the inputs.
tPredictors = trainingData(:, 1:4);
% The last column is the "answer/ground truth".
tResponse = trainingData{:, end};
% Now run Classification Learner app. Start a new session from workspace, and
% Select tPredictors as the "Data set variable" and
% select tResponse as the response.
% I did "All quick to train" and found the Fine Tree model was the best.
% I highlighted it and clicked the Export button to export trainingModel to the workspace.
% Then save it to a .mat file from the command line like this:
% save('trainedModel.mat', 'trainedModel')
Now start a new session from workspace. You should see this:
Your screenshot doesn't have New Session from Workspace in it so I think you did something different. Once you click the drop down list for Data Set Variable and pick tPredictors, then it should populate the Response. Click From Workspace and you should see tResponse.
Hi what is the version of your matlab i click on the new session from workspace and didnt see that @@
I have r2021b. But tResponse should be in your response drop down list.
i use R2018a but the response I can choose is one of the column in the data set variable
Can you call tech support on that?
Anyway, I uploaded the model for you so you should be able to use that.
oh thank you i think i will upgrdae my version for sure ^^
I think for earlier versions it expects the response variable to be one of the columns in your predictors table. I was just working with a guy yesterday who had r2019b and we figured out that's what it was wanting.
@Image Analyst i mean I cant use the test data function in classification app. I use your code to import the data into workspace. Can you help me please
% TRAINING
trainingData=readtable("ClassificationData2.xlsx")
% The first 4 columns are the inputs.
tPredictors = trainingData(:, 1:4);
% The last column is the "answer/ground truth".
tResponse = trainingData{:, end};
testingData=readtable("ClassificationTestData2.xlsx")
tTesting=testingData(:,1:4);
ttestResponse=testingData{:,end};

Sign in to comment.

Products

Release

R2018a

Asked:

on 15 Dec 2021

Commented:

on 12 Jan 2022

Community Treasure Hunt

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

Start Hunting!