Misclassification cost matrix error

1 visualizzazione (ultimi 30 giorni)
Mohammad Zakaie Far
Mohammad Zakaie Far il 7 Giu 2022
Hello,
I have an error in my matlab machin learning code
"Error using classreg.learning.classif.FullClassificationModel.processCost (line 638)
Misclassification cost matrix must be 1-by-1."
I will apriciate if some one solve it for me.
I have attached the code and its data.
TNX

Risposte (1)

Mohammad Zakaie Far
Mohammad Zakaie Far il 7 Giu 2022
close all;
clear all;
%----------------------------
%Step 1: Prepare to read the data into memory & Create a table with filenames and labels
% expecting the training data in subfolders of 'Data\training\*': "training-a", etc
training_fds = fileDatastore(fullfile(pwd, 'Data', 'training'), 'ReadFcn', @importMeazzaFile, 'FileExtensions', '.mat','IncludeSubfolders',true);
data_dir = fullfile(pwd, 'Data', 'training');
folder_list = dir([data_dir filesep 'training*']);
reference_table = table();
for ifolder = 1:length(folder_list)
disp(['Processing files from folder: ' folder_list(ifolder).name])
current_folder = [data_dir filesep folder_list(ifolder).name];
% Import ground truth labels (1, -1) from reference. 1 = Stand 2041, -1 = Stand 2051
reference_table = [reference_table; importReferencefile([current_folder filesep 'REFERENCE.csv'])];
end
% ----------------------------
% Step 2: Extract features from raw Meazza Stadium signals
runExtraction = false; % control whether to run feature extraction (will take several minutes)
% Note: be sure to have the training data downloaded before executing
% this section!
if runExtraction | ~exist('FeatureTable.mat')%#ok
% Window length for feature extraction in seconds
win_len = 0.1;
% Specify the overlap between adjacent windows for feature extraction in percentage
win_overlap = 0;
% Initialize feature table to accumulate observations
feature_table = table();
% Use Parallel Computing Toobox to speed up feature extraction by distributing computation across available processors
% Create partitions of the fileDatastore object based on the number of processors
n_parts = numpartitions(training_fds, gcp);
% Note: You could distribute computation across available processors by using
% parfor instead of "for" below, but you'll need to omit keeping track
% of signal lengths
parfor ipart = 1:n_parts
% Get partition ipart of the datastore.
subds = partition(training_fds, n_parts, ipart);
% Extract features for the sub datastore
[feature_win,sampleN] = extractFeatures2(subds, win_len, win_overlap, reference_table);
% and append that to the overall feature table we're building up
feature_table = [feature_table; feature_win];
% Display progress
disp(['Part ' num2str(ipart) ' done.'])
end
save('FeatureTable', 'feature_table');
else % simply load the precomputed features
load('FeatureTable.mat');
end
%Take a look at the feature table
disp(feature_table(1:5,:))
% %----------------------------
% %Step 3: Develope Predictive Model|Train, compare and select classifier
% classificationLearner
% ----------------------------
% Step 5: Develope Predictive Model|Split data into training and testing sets
% using split function defined at end of script to divide feature table
% into training and test set, holding out 30%
[training_set, test_set] = splitDataSets(feature_table,0.3);
% Assign higher cost for misclassification of abnormal heart sounds
C = [0 5; 1 0];
% Create a random sub sample (to speed up training) of 1/4 of the training set
% subsample = randi([1 height(training_set)], round(height(training_set)/4), 1);
% OR train on the whole training set
subsample = 1:height(training_set);
rng(1);
% Create a 5-fold cross-validation set from training data
cvp = cvpartition(length(subsample),'KFold',5);
% Step 2: train the model with hyperparameter tuning (unless you simply
% load an existing pre-trained model)
% train ensemble of decision trees (random forest)
disp("Training Ensemble classifier...")
% bayesian optimization parameters (stop after 15 iterations)
opts = struct('Optimizer','bayesopt','ShowPlots',true,'CVPartition',cvp,...
'AcquisitionFunctionName','expected-improvement-plus','MaxObjectiveEvaluations',5);
trained_model = fitcensemble(training_set(subsample,:),'class','Cost',C,...
'OptimizeHyperparameters',{'Method','NumLearningCycles','LearnRate'},...
'HyperparameterOptimizationOptions',opts)
% Step 3: evaluate accuracy on held-out test set
% Predict class labels for the validation set using trained model
% NOTE: if training ensemble without optimization, need to use trained_model.Trained{idx} to predict
predicted_class = predict(trained_model, test_set);
conf_mat = confusionmat(test_set.class, predicted_class);
conf_mat_per = conf_mat*100./sum(conf_mat, 2);
% Visualize model performance in heatmap
labels = {'Abnormal', 'Normal'};
heatmap(labels, labels, conf_mat_per, 'Colormap', winter, 'ColorbarVisible','off');

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by