main content

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

linear classification properties

regularization term strength, specified as a nonnegative scalar or vector of nonnegative values.

data types: double | single

linear classification model type, specified as 'logistic' or 'svm'.

in this table, f(x)=xβ b.

  • β is a vector of p coefficients.

  • x is an observation from p predictor variables.

  • b is the scalar bias.

valuealgorithmloss functionfittedloss value
'svm'support vector machinehinge: [y,f(x)]=max[0,1yf(x)]'hinge'
'logistic'logistic regressiondeviance (logistic): [y,f(x)]=log{1 exp[yf(x)]}'logit'

linear coefficient estimates, specified as a numeric vector with length equal to the number of predictors.

data types: double

estimated bias term or model intercept, specified as a numeric scalar.

data types: double

this property is read-only.

loss function used to fit the linear model, specified as 'hinge' or 'logit'.

valuealgorithmloss functionlearner value
'hinge'support vector machinehinge: [y,f(x)]=max[0,1yf(x)]'svm'
'logit'logistic regressiondeviance (logistic): [y,f(x)]=log{1 exp[yf(x)]}'logistic'

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.

valuedescription
'lasso (l1)'lasso (l1) penalty: λj=1p|βj|
'ridge (l2)'ridge (l2) penalty: λ2j=1pβj2

λ specifies the regularization term strength (see lambda).

the software excludes the bias term (β0) from the regularization penalty.

other classification properties

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

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

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 class 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

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

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

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

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

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 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';
    valuedescription
    "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 ex)
    "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 ex) – 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
limelocal interpretable model-agnostic explanations (lime)
classification loss for linear classification models
classification margins for linear classification models
partialdependencecompute partial dependence
plotpartialdependencecreate partial dependence plot (pdp) and individual conditional expectation (ice) plots
predict labels for linear classification models
shapleyshapley values
choose subset of regularized, binary linear classification models
updateupdate model parameters for code generation

copy semantics

value. to learn how value classes affect copy operations, see .

examples

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.

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

version history

introduced in r2016a
网站地图