ClassificationPartitionedKernel - Cross-validated, binary kernel classification model - MATLAB (original) (raw)
Cross-validated, binary kernel classification model
Description
ClassificationPartitionedKernel
is a binary kernel classification model, trained on cross-validated folds. You can estimate the quality of classification, or how well the kernel classification model generalizes, using one or more “kfold” functions: kfoldPredict,kfoldLoss,kfoldMargin, andkfoldEdge.
Every “kfold” method uses models trained on training-fold (in-fold) observations to predict the response for validation-fold (out-of-fold) observations. For example, suppose that you cross-validate using five folds. In this case, the software randomly assigns each observation into five groups of equal size (roughly). The training fold contains four of the groups (that is, roughly 4/5 of the data) and the_validation fold_ contains the other group (that is, roughly 1/5 of the data). In this case, cross-validation proceeds as follows:
- The software trains the first model (stored in
CVMdl.Trained{1}
) by using the observations in the last four groups and reserves the observations in the first group for validation. - The software trains the second model (stored in
CVMdl.Trained{2}
) using the observations in the first group and the last three groups. The software reserves the observations in the second group for validation. - The software proceeds in a similar fashion for the third, fourth, and fifth models.
If you validate by using kfoldPredict, the software computes predictions for the observations in group i by using the_i_th model. In short, the software estimates a response for every observation by using the model trained without that observation.
Note
ClassificationPartitionedKernel
model objects do not store the predictor data set.
Creation
You can create a ClassificationPartitionedKernel
model by training a classification kernel model using fitckernel and specifying one of these name-value pair arguments: 'Crossval'
,'CVPartition'
, 'Holdout'
, 'KFold'
, or 'Leaveout'
.
Properties
Cross-Validation Properties
This property is read-only.
Cross-validated model name, specified as a character vector.
For example, 'Kernel'
specifies a cross-validated kernel model.
Data Types: char
This property is read-only.
Number of cross-validated folds, specified as a positive integer scalar.
Data Types: double
This property is read-only.
Cross-validation parameter values, specified as an object. The parameter values correspond to the name-value pair argument values used to cross-validate the kernel classifier. ModelParameters
does not contain estimated parameters.
You can access the properties of ModelParameters
using dot notation.
This property is read-only.
Number of observations in the training data, specified as a positive numeric scalar.
Data Types: double
This property is read-only.
Data partition indicating how the software splits the data into cross-validation folds, specified as a cvpartition model.
This property is read-only.
Kernel classifiers trained on cross-validation folds, specified as a cell array ofClassificationKernel models. Trained
has_k_ cells, where k is the number of folds.
Data Types: cell
This property is read-only.
Observation weights used to cross-validate the model, specified as a numeric vector. W
has NumObservations
elements.
The software normalizes the weights used for training so thatsum(W,'omitnan')
is 1
.
Data Types: single
| double
This property is read-only.
Observed class labels used to cross-validate the model, specified as a categorical or character array, logical or numeric vector, or cell array of character vectors.Y
has NumObservations
elements and has the same data type as the input argument Y
that you pass to fitckernel to cross-validate the model. (The software treats string arrays as cell arrays of character vectors.)
Each row of Y
represents the observed classification of the corresponding row of X
.
Data Types: categorical
| char
| logical
| single
| double
| cell
Other Classification Properties
This property is read-only.
Categorical predictor indices, specified as a vector of positive integers. CategoricalPredictors
contains index values indicating that the corresponding predictors are categorical. The index values are between 1 and p
, where p
is the number of predictors used to train the model. If none of the predictors are categorical, then this property is empty ([]
).
Data Types: single
| double
This property is read-only.
Unique class labels used in training, specified as a categorical or character array, logical or numeric vector, or cell array of character vectors. ClassNames
has the same data type as the observed class labels property Y
and determines the class order.
Data Types: categorical
| char
| logical
| single
| double
| cell
This property is read-only.
Misclassification costs, specified as a square numeric matrix.Cost
has K rows and columns, where_K_ is the number of classes.
Cost(i,j)
is the cost of classifying a point into classj
if its true class is i
. The order of the rows and columns of Cost
corresponds to the order of the classes in ClassNames
.
Data Types: double
This property is read-only.
Predictor names in order of their appearance in the predictor data, specified as a cell array of character vectors. The length of PredictorNames
is equal to the number of columns used as predictor variables in the training dataX
or Tbl
.
Data Types: cell
This property is read-only.
Prior class probabilities, specified as a numeric vector.Prior
has as many elements as there are classes inClassNames
, and the order of the elements corresponds to the elements of ClassNames
.
Data Types: double
This property is read-only.
Response variable name, specified as a character vector.
Data Types: char
Score transformation function to apply to predicted scores, specified as a function name or function handle.
For a kernel classification model Mdl
, and before the score transformation, the predicted classification score for the observation x (row vector) is f(x)=T(x)β+b.
- T(·) is a transformation of an observation for feature expansion.
- β is the estimated column vector of coefficients.
- b is the estimated scalar bias.
To change the CVMdl
score transformation function to function
, for example, use dot notation.
- For a built-in function, enter this code and replace
function
with a value from the table.
CVMdl.ScoreTransform = 'function';Value Description "doublelogit" 1/(1 + e_–2_x) "invlogit" log(x / (1 – x)) "ismax" Sets the score for the class with the largest score to 1, and sets the scores for all other classes to 0 "logit" 1/(1 + e_–_x) "none" or "identity" x (no transformation) "sign" –1 for x < 00 for _x_ = 01 for _x_ > 0 "symmetric" 2_x_ – 1 "symmetricismax" Sets the score for the class with the largest score to 1, and sets the scores for all other classes to –1 "symmetriclogit" 2/(1 + e_–_x) – 1 - For a MATLAB® function or a function that you define, enter its function handle.
CVMdl.ScoreTransform = @function;function
must accept a matrix of the original scores for each class, and then return a matrix of the same size representing the transformed scores for each class.
Data Types: char
| function_handle
Object Functions
gather | Gather properties of Statistics and Machine Learning Toolbox object from GPU |
---|---|
kfoldEdge | Classification edge for cross-validated kernel classification model |
kfoldLoss | Classification loss for cross-validated kernel classification model |
kfoldMargin | Classification margins for cross-validated kernel classification model |
kfoldPredict | Classify observations in cross-validated kernel classification model |
Examples
Load the ionosphere
data set. This data set has 34 predictors and 351 binary responses for radar returns, either bad ('b'
) or good ('g'
).
load ionosphere rng('default') % For reproducibility
Cross-validate a binary kernel classification model. By default, the software uses 10-fold cross-validation.
CVMdl = fitckernel(X,Y,'CrossVal','on')
CVMdl = ClassificationPartitionedKernel CrossValidatedModel: 'Kernel' ResponseName: 'Y' NumObservations: 351 KFold: 10 Partition: [1×1 cvpartition] ClassNames: {'b' 'g'} ScoreTransform: 'none'
Properties, Methods
CVMdl
is a ClassificationPartitionedKernel
model. Because fitckernel
implements 10-fold cross-validation, CVMdl
contains 10 ClassificationKernel
models that the software trains on training-fold (in-fold) observations.
Estimate the cross-validated classification error.
The classification error rate is approximately 9%.
Extended Capabilities
The object functions of a ClassificationPartitionedKernel
model fully support GPU arrays.
Version History
Introduced in R2018b
You can fit a ClassificationPartitionedKernel
object with GPU arrays by using fitckernel. TheClassificationPartitionedKernel
object functions support GPU array input arguments so that the functions can execute on a GPU.
You can also gather the properties of a ClassificationPartitionedKernel
model object from the GPU by using the gather function.
Starting in R2022a, the Cost
property stores the user-specified cost matrix, so that you can compute the observed misclassification cost using the specified cost value. The software stores normalized prior probabilities (Prior
) and observation weights (W
) that do not reflect the penalties described in the cost matrix. To compute the observed misclassification cost, specify theLossFun
name-value argument as "classifcost"
when you call the kfoldLoss
function.
Note that model training has not changed and, therefore, the decision boundaries between classes have not changed.
For training, the fitting function updates the specified prior probabilities by incorporating the penalties described in the specified cost matrix, and then normalizes the prior probabilities and observation weights. This behavior has not changed. In previous releases, the software stored the default cost matrix in the Cost
property and stored the prior probabilities and observation weights used for training in thePrior
and W
properties, respectively. Starting in R2022a, the software stores the user-specified cost matrix without modification, and stores normalized prior probabilities and observation weights that do not reflect the cost penalties. For more details, see Misclassification Cost Matrix, Prior Probabilities, and Observation Weights.
Some object functions use the Cost
and W
properties:
- The
kfoldLoss
function uses the cost matrix stored in theCost
property if you specify theLossFun
name-value argument as"classifcost"
or"mincost"
. - The
kfoldLoss
andkfoldEdge
functions use the observation weights stored in theW
property.
If you specify a nondefault cost matrix when you train a classification model, the object functions return a different value compared to previous releases.
If you want the software to handle the cost matrix, prior probabilities, and observation weights in the same way as in previous releases, adjust the prior probabilities and observation weights for the nondefault cost matrix, as described in Adjust Prior Probabilities and Observation Weights for Misclassification Cost Matrix. Then, when you train a classification model, specify the adjusted prior probabilities and observation weights by using the Prior
and Weights
name-value arguments, respectively, and use the default cost matrix.