linear model for binary classification of high-凯发k8网页登录
linear model for binary classification of high-dimensional data
description
classificationlinear
is a trained linear model object for binary classification; the linear model is a support vector machine (svm) or logistic regression model. fitclinear
fits a classificationlinear
model by minimizing the objective function using techniques that reduce computation time for high-dimensional data sets (e.g., stochastic gradient descent). the classification loss plus the regularization term compose the objective function.
unlike other classification models, and for economical memory usage, classificationlinear
model objects do not store the training data. however, they do store, for example, the estimated linear model coefficients, prior-class probabilities, and the regularization strength.
you can use trained classificationlinear
models to predict labels or classification scores for new data. for details, see .
construction
create a classificationlinear
object by using fitclinear
.
properties
lambda
— regularization term strength
nonnegative scalar | vector of nonnegative values
regularization term strength, specified as a nonnegative scalar or vector of nonnegative values.
data types: double
| single
learner
— linear classification model type
'logistic'
| 'svm'
linear classification model type, specified as 'logistic'
or 'svm'
.
in this table,
β is a vector of p coefficients.
x is an observation from p predictor variables.
b is the scalar bias.
value | algorithm | loss function | fittedloss value |
---|---|---|---|
'svm' | support vector machine | hinge: | 'hinge' |
'logistic' | logistic regression | deviance (logistic): | 'logit' |
beta
— linear coefficient estimates
numeric vector
linear coefficient estimates, specified as a numeric vector with length equal to the number of predictors.
data types: double
bias
— estimated bias term
numeric scalar
estimated bias term or model intercept, specified as a numeric scalar.
data types: double
fittedloss
— loss function used to fit linear model
'hinge'
| 'logit'
this property is read-only.
loss function used to fit the linear model, specified as 'hinge'
or 'logit'
.
value | algorithm | loss function | learner value |
---|---|---|---|
'hinge' | support vector machine | hinge: | 'svm' |
'logit' | logistic regression | deviance (logistic): | 'logistic' |
regularization
— complexity penalty type
'lasso (l1)'
| 'ridge (l2)'
complexity penalty type, specified as 'lasso (l1)'
or 'ridge
(l2)'
.
the software composes the objective function for minimization from the sum of the average loss
function (see fittedloss
) and a regularization value from this
table.
value | description |
---|---|
'lasso (l1)' | lasso (l1) penalty: |
'ridge (l2)' | ridge (l2) penalty: |
λ specifies the regularization term
strength (see lambda
).
the software excludes the bias term (β0) from the regularization penalty.
categoricalpredictors
— categorical predictor indices
vector of positive integers | []
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
classnames
— unique class labels
categorical array | character array | logical vector | numeric vector | cell array of character vectors
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 class labels y
.
(the software treats string arrays as cell arrays of character
vectors.)
classnames
also determines the class
order.
data types: categorical
| char
| logical
| single
| double
| cell
cost
— misclassification costs
square numeric matrix
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(
is
the cost of classifying a point into class i
,j
)j
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
modelparameters
— parameters used for training model
structure
parameters used for training the classificationlinear
model, specified as a structure.
access fields of modelparameters
using dot notation. for example, access
the relative tolerance on the linear coefficients and the bias term by using
mdl.modelparameters.betatolerance
.
data types: struct
predictornames
— predictor names
cell array of character vectors
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 variables in the training data x
or
tbl
used as predictor variables.
data types: cell
expandedpredictornames
— expanded predictor names
cell array of character vectors
expanded predictor names, specified as a cell array of character vectors.
if the model uses encoding for categorical variables, then
expandedpredictornames
includes the names that describe the
expanded variables. otherwise, expandedpredictornames
is the same as
predictornames
.
data types: cell
prior
— prior class probabilities
numeric vector
this property is read-only.
prior class probabilities, specified as a numeric vector.
prior
has as many elements as
classes in classnames
, and the order of the
elements corresponds to the elements of
classnames
.
data types: double
responsename
— response variable name
character vector
response variable name, specified as a character vector.
data types: char
scoretransform
— score transformation function
'doublelogit'
| 'invlogit'
| 'ismax'
| 'logit'
| 'none'
| function handle | ...
score transformation function to apply to predicted scores, specified as a function name or function handle.
for linear classification models and before transformation, the predicted
classification score for the observation x (row vector) is f(x) =
xβ b, where β and b correspond to
mdl.beta
and mdl.bias
, respectively.
to change the score transformation function to, for example,
function
, use dot notation.
for a built-in function, enter this code and replace
function
with a value in the table.mdl.scoretransform = 'function';
value description "doublelogit"
1/(1 e–2x) "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 < 0
0 for x = 0
1 for x > 0"symmetric"
2x – 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.
mdl.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
classification edge for linear classification models | |
convert linear model for binary classification to incremental learner | |
lime | local interpretable model-agnostic explanations (lime) |
classification loss for linear classification models | |
classification margins for linear classification models | |
partialdependence | compute partial dependence |
plotpartialdependence | create partial dependence plot (pdp) and individual conditional expectation (ice) plots |
predict labels for linear classification models | |
shapley | shapley values |
choose subset of regularized, binary linear classification models | |
update | update model parameters for code generation |
copy semantics
value. to learn how value classes affect copy operations, see .
examples
train linear classification model
train a binary, linear classification model using support vector machines, dual sgd, and ridge regularization.
load the nlp data set.
load nlpdata
x
is a sparse matrix of predictor data, and y
is a categorical vector of class labels. there are more than two classes in the data.
identify the labels that correspond to the statistics and machine learning toolbox™ documentation web pages.
ystats = y == 'stats';
train a binary, linear classification model that can identify whether the word counts in a documentation web page are from the statistics and machine learning toolbox™ documentation. train the model using the entire data set. determine how well the optimization algorithm fit the model to the data by extracting a fit summary.
rng(1); % for reproducibility
[mdl,fitinfo] = fitclinear(x,ystats)
mdl = classificationlinear responsename: 'y' classnames: [0 1] scoretransform: 'none' beta: [34023x1 double] bias: -1.0059 lambda: 3.1674e-05 learner: 'svm' properties, methods
fitinfo = struct with fields:
lambda: 3.1674e-05
objective: 5.3783e-04
passlimit: 10
numpasses: 10
batchlimit: []
numiterations: 238561
gradientnorm: nan
gradienttolerance: 0
relativechangeinbeta: 0.0562
betatolerance: 1.0000e-04
deltagradient: 1.4582
deltagradienttolerance: 1
terminationcode: 0
terminationstatus: {'iteration limit exceeded.'}
alpha: [31572x1 double]
history: []
fittime: 0.0886
solver: {'dual'}
mdl
is a classificationlinear
model. you can pass mdl
and the training or new data to loss
to inspect the in-sample classification error. or, you can pass mdl
and new predictor data to predict
to predict class labels for new observations.
fitinfo
is a structure array containing, among other things, the termination status (terminationstatus
) and how long the solver took to fit the model to the data (fittime
). it is good practice to use fitinfo
to determine whether optimization-termination measurements are satisfactory. because training time is small, you can try to retrain the model, but increase the number of passes through the data. this can improve measures like deltagradient
.
predict class labels using linear classification model
load the nlp data set.
load nlpdata n = size(x,1); % number of observations
identify the labels that correspond to the statistics and machine learning toolbox™ documentation web pages.
ystats = y == 'stats';
hold out 5% of the data.
rng(1); % for reproducibility cvp = cvpartition(n,'holdout',0.05)
cvp = hold-out cross validation partition numobservations: 31572 numtestsets: 1 trainsize: 29994 testsize: 1578
cvp
is a cvpartition
object that defines the random partition of n data into training and test sets.
train a binary, linear classification model using the training set that can identify whether the word counts in a documentation web page are from the statistics and machine learning toolbox™ documentation. for faster training time, orient the predictor data matrix so that the observations are in columns.
idxtrain = training(cvp); % extract training set indices x = x'; mdl = fitclinear(x(:,idxtrain),ystats(idxtrain),'observationsin','columns');
predict observations and classification error for the hold out sample.
idxtest = test(cvp); % extract test set indices labels = predict(mdl,x(:,idxtest),'observationsin','columns'); l = loss(mdl,x(:,idxtest),ystats(idxtest),'observationsin','columns')
l = 7.1753e-04
mdl
misclassifies fewer than 1% of the out-of-sample observations.
extended capabilities
c/c code generation
generate c and c code using matlab® coder™.
usage notes and limitations:
the and
update
functions support code generation.when you train a linear classification model by using
fitclinear
, the following restrictions apply.if the predictor data input argument value is a matrix, it must be a full, numeric matrix. code generation does not support sparse data.
you can specify only one regularization strength—
'auto'
or a nonnegative scalar for the'lambda'
name-value pair argument.the value of the
'scoretransform'
name-value pair argument cannot be an anonymous function.for code generation with a coder configurer, the following additional restrictions apply.
categorical predictors (
logical
,categorical
,char
,string
, orcell
) are not supported. you cannot use thecategoricalpredictors
name-value argument. to include categorical predictors in a model, preprocess them by using before fitting the model.class labels with the
categorical
data type are not supported. both the class label value in the training data (tbl
ory
) and the value of theclassnames
name-value argument cannot be an array with thecategorical
data type.
for more information, see introduction to code generation.
version history
introduced in r2016ar2022a: cost
property stores the user-specified cost matrix
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
)
that do not reflect the penalties described in the cost matrix. to compute the observed
misclassification cost, specify the lossfun
name-value argument as
"classifcost"
when you call the loss
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 used for training in the
prior
property. starting in r2022a, the software stores the
user-specified cost matrix without modification, and stores normalized prior probabilities that do
not reflect the cost penalties. for more details, see .
some object functions use the cost
and prior
properties:
the
loss
function uses the cost matrix stored in thecost
property if you specify thelossfun
name-value argument as"classifcost"
or"mincost"
.the
loss
andedge
functions use the prior probabilities stored in theprior
property to normalize the observation weights of the input data.
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 as in previous releases, adjust the prior probabilities
and observation weights for the nondefault cost matrix, as described in . 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.
see also
| fitclinear
| | classificationecoc
| | classificationkernel
打开示例
您曾对此示例进行过修改。是否要打开带有您的编辑的示例?
matlab 命令
您点击的链接对应于以下 matlab 命令:
请在 matlab 命令行窗口中直接输入以执行命令。web 浏览器不支持 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
- (español)
- (english)
- (english)
europe
- (english)
- (english)
- (deutsch)
- (español)
- (english)
- (français)
- (english)
- (italiano)
- (english)
- (english)
- (english)
- (deutsch)
- (english)
- (english)
- switzerland
- (english)
asia pacific
- (english)
- (english)
- (english)
- 中国
- (日本語)
- (한국어)