predict
Predict labels for Gaussian kernel classification model
Description
Examples
Predict Training Set Labels
Predict the training set labels using a binary kernel classification model, and display the confusion matrix for the resulting classification.
Load theionosphere
data set. This data set has 34 predictors and 351 binary responses for radar returns, either bad ('b'
) or good ('g'
).
loadionosphere
Train a binary kernel classification model that identifies whether the radar return is bad ('b'
) or good ('g'
).
rng('default')% For reproducibilityMdl = fitckernel(X,Y);
Mdl
is aClassificationKernel
model.
Predict the training set, or resubstitution, labels.
label = predict(Mdl,X);
Construct a confusion matrix.
ConfusionTrain = confusionchart(Y,label);
The model misclassifies one radar return for each class.
Predict Test Set Labels
Predict the test set labels using a binary kernel classification model, and display the confusion matrix for the resulting classification.
Load theionosphere
data set. This data set has 34 predictors and 351 binary responses for radar returns, either bad ('b'
) or good ('g'
).
loadionosphere
Partition the data set into training and test sets. Specify a 15% holdout sample for the test set.
rng('default')% For reproducibilityPartition = cvpartition(Y,'Holdout',0.15); trainingInds = training(Partition);% Indices for the training settestInds = test(Partition);% Indices for the test set
Train a binary kernel classification model using the training set. A good practice is to define the class order.
Mdl = fitckernel(X(trainingInds,:),Y(trainingInds),'ClassNames',{'b','g'});
Predict the training-set labels and the test set labels.
labelTrain = predict(Mdl,X(trainingInds,:)); labelTest = predict(Mdl,X(testInds,:));
Construct a confusion matrix for the training set.
ConfusionTrain = confusionchart(Y(trainingInds),labelTrain);
The model misclassifies only one radar return for each class.
Construct a confusion matrix for the test set.
ConfusionTest = confusionchart(Y(testInds),labelTest);
The model misclassifies one bad radar return as being a good return, and five good radar returns as being bad returns.
Estimate Posterior Class Probabilities
Estimate posterior class probabilities for a test set, and determine the quality of the model by plotting a receiver operating characteristic (ROC) curve. Kernel classification models return posterior probabilities for logistic regression learners only.
Load theionosphere
data set. This data set has 34 predictors and 351 binary responses for radar returns, either bad ('b'
) or good ('g'
).
loadionosphere
Partition the data set into training and test sets. Specify a 30% holdout sample for the test set.
rng('default')% For reproducibilityPartition = cvpartition(Y,'Holdout',0.30); trainingInds = training(Partition);% Indices for the training settestInds = test(Partition);% Indices for the test set
Train a binary kernel classification model. Fit logistic regression learners.
Mdl = fitckernel(X(trainingInds,:),Y(trainingInds),...'ClassNames',{'b','g'},'Learner','logistic');
Predict the posterior class probabilities for the test set.
[~,posterior] = predict(Mdl,X(testInds,:));
BecauseMdl
has one regularization strength, the outputposterior
is a matrix with two columns and rows equal to the number of test-set observations. Columni
contains posterior probabilities ofMdl.ClassNames(i)
given a particular observation.
计算性能指标(真正的positive rates and false positive rates) for a ROC curve and find the area under the ROC curve (AUC) value by creating arocmetrics
object.
rocObj = rocmetrics(Y(testInds),posterior,Mdl.ClassNames);
Plot the ROC curve for the second class by using theplot
function ofrocmetrics
.
plot(rocObj,ClassNames=Mdl.ClassNames(2))
The AUC is close to1
, which indicates that the model predicts labels well.
Input Arguments
Mdl
—Binary kernel classification model
ClassificationKernel
model object
Binary kernel classification model, specified as aClassificationKernel
model object. You can create aClassificationKernel
model object usingfitckernel
.
X
—Predictor data to be classified
numeric matrix|table
Predictor data to be classified, specified as a numeric matrix or table.
Each row ofX
corresponds to one observation, and each column corresponds to one variable.
For a numeric matrix:
The variables in the columns of
X
must have the same order as the predictor variables that trainedMdl
.If you trained
Mdl
using a table (for example,Tbl
) andTbl
contains all numeric predictor variables, thenX
can be a numeric matrix. To treat numeric predictors inTbl
as categorical during training, identify categorical predictors by using theCategoricalPredictors
name-value pair argument offitckernel
. IfTbl
contains heterogeneous predictor variables (for example, numeric and categorical data types) andX
is a numeric matrix, thenpredict
throws an error.
For a table:
predict
does not support multicolumn variables or cell arrays other than cell arrays of character vectors.If you trained
Mdl
using a table (for example,Tbl
), then all predictor variables inX
must have the same variable names and data types as those that trainedMdl
(stored inMdl.PredictorNames
). However, the column order ofX
does not need to correspond to the column order ofTbl
. Also,Tbl
andX
can contain additional variables (response variables, observation weights, and so on), butpredict
ignores them.If you trained
Mdl
using a numeric matrix, then the predictor names inMdl.PredictorNames
and corresponding predictor variable names inX
must be the same. To specify predictor names during training, see thePredictorNames
name-value pair argument offitckernel
. All predictor variables inX
must be numeric vectors.X
can contain additional variables (response variables, observation weights, and so on), butpredict
ignores them.
Data Types:table
|double
|single
Output Arguments
Label
— Predicted class labels
categorical array | character array | logical matrix | numeric matrix | cell array of character vectors
预测类标签,returned as a categorical or character array, logical or numeric matrix, or cell array of character vectors.
Label
hasnrows, wherenis the number of observations inX
, and has the same data type as the observed class labels (Y
) used to trainMdl
.(The software treats string arrays as cell arrays of character vectors.)
predict
classifies observations into the class yielding the highest score.
Score
— Classification scores
numeric array
Classification scores, returned as ann-by-2 numeric array, wherenis the number of observations inX
.Score(
is the score for classifying observationi
,j
)i
into classj
.Mdl.ClassNames
stores the order of the classes.
IfMdl.Learner
is'logistic'
, then classification scores are posterior probabilities.
More About
Classification Score
For kernel classification models, the rawclassification scorefor classifying the observationx, a row vector, into the positive class is defined by
is a transformation of an observation for feature expansion.
βis the estimated column vector of coefficients.
bis the estimated scalar bias.
The raw classification score for classifyingxinto the negative class is−f(x). The software classifies observations into the class that yields a positive score.
If the kernel classification model consists of logistic regression learners, then the software applies the'logit'
score transformation to the raw classification scores (seeScoreTransform
).
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
Usage notes and limitations:
predict
does not support talltable
data.
For more information, seeTall Arrays.
Version History
Abrir ejemplo
Tiene una versión modificada de este ejemplo. ¿Desea abrir este ejemplo con sus modificaciones?
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(西班牙语)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(西班牙语)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)