Untitled

mail@pastecode.io avatar
unknown
matlab
2 years ago
1.3 kB
4
Indexable
Never
% Splitti i dati in training and test sets
[train_ind, test_ind] = holdout(numel(images), 0.6); %Scegli tu il valore del bilanciamento) 
train_features = features(train_ind, :);
train_labels = labels(train_ind);
test_features = features(test_ind, :);
test_labels = labels(test_ind);

% Training del classificatore SVM
svm_model = fitcsvm(train_features, train_labels);

% Training di un ensemble tree classifier
ensemble_model = fitensemble(train_features, train_labels, 'AdaBoostM2', 100, 'Tree');

% Classifico il test set utilizzando i classificatori SVM e ensemble tree
svm_predictions = predict(svm_model, test_features);
ensemble_predictions = predict(ensemble_model, test_features);

% Calcolo la confusion matrix per ciascun classificatore
svm_confusion = confusionmat(test_labels, svm_predictions);
ensemble_confusion = confusionmat(test_labels, ensemble_predictions);

% Visualizzo la confusion matrix per ciascun classificatore

subplot(1, 2, 1);
plotconfusion(test_labels, svm_predictions);
title('SVM Confusion Matrix');
subplot(1, 2, 2);
plotconfusion(test_labels, ensemble_predictions);
title('Ensemble Confusion Matrix');

% Calcolo le performance metrics per ogni classificatore
svm_report = classification_report(test_labels, svm_predictions);
ensemble_report = classification_report(test_labels, ensemble_predictions);