Plotting Classification Loss Functions

1 visualización (últimos 30 días)
johnny2
johnny2 el 12 de Jul. de 2019
Respondida: Meet el 1 de Ag. de 2024
How can I plot the logisitic loss function, MathWorks says that is supports loss functions that you can specify by using the 'LossFun' name-value pair argument. The value for this function is 'logit'. So is there a way that MatLab can automically generate this function for me?

Respuestas (1)

Meet
Meet el 1 de Ag. de 2024
Hi Johnny,
MATLAB provides a function "fitclinear" that you can use to fit a logistic regression model. You can also specify the loss function, in your case, "logit".
Here is an example code for the same, where I have generated 100 samples for each class and labeled the data as "+1" and "1" for the two classes:
The "Learner", "logistic" argument specifies that we are using a logistic regression model.
The "LossFun", "logit" argument specifies that we are using the logistic loss function.
numSamples = 100;
X = [randn(numSamples, 2) + 1; randn(numSamples, 2) - 1]; % Features
y = [ones(numSamples, 1); -ones(numSamples, 1)]; % Output Binary labels: +1 and -1
% Plot the synthetic data
figure;
gscatter(X(:,1), X(:,2), y, 'rb', 'xo');
xlabel('Feature 1');
ylabel('Feature 2');
title('Synthetic Data');
legend('Class +1', 'Class -1');
grid on;
% Fit a Model using Logistic Loss
mdl = fitclinear(X, y, 'Learner', 'logistic', 'LossFun', 'logit');
disp(mdl);
For more information, you can refer to the following documentation link:

Categorías

Más información sobre Statistics and Machine Learning Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by