Predict response of linear regression model
MDL
-l一世near regression modelRegressionLinear
模型对象l一世near regression model, specified as aRegressionLinear
模型对象。You can create aRegressionLinear
模型对象使用fitrlinear
。
X
-用于生成响应的预测数据预测数据用于生成反应,具体ed as a full or sparse numeric matrix or a table.
默认情况下,每一行X
corresponds to one observation, and each column corresponds to one variable.
对于数字矩阵:
列中的变量X
必须具有与训练的预测变量相同的顺序MDL
。
如果你训练MDL
使用表(例如,Tbl
)andTbl
contains only numeric predictor variables, thenX
can be a numeric matrix. To treat numeric predictors inTbl
作为培训期间的分类,通过使用CategoricalPredictors
name-value pair argument offitrlinear
。IfTbl
contains heterogeneous predictor variables (for example, numeric and categorical data types) andX
是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.
如果你训练MDL
使用表(例如,Tbl
),,,,then all predictor variables inX
必须具有与训练的变量相同的变量名称和数据类型MDL
(stored inmdl.predictictornames
)。However, the column order ofX
不需要对应于Tbl
。还,Tbl
andX
can contain additional variables (response variables, observation weights, and so on), butpredict
忽略它们。
如果你训练MDL
using a numeric matrix, then the predictor names inmdl.predictictornames
must be the same as the corresponding predictor variable names inX
。To specify predictor names during training, use thePredictorNames
name-value pair argument offitrlinear
。全部predictor variables inX
must be numeric vectors.X
can contain additional variables (response variables, observation weights, and so on), butpredict
忽略它们。
笔记
如果您将预测器矩阵定向,以使观察值与列相对应并指定'ObservationsIn','columns'
,那么您可能会大大减少优化执行时间。您无法指定'ObservationsIn','columns'
对于表中的预测数据。
Data Types:double
|single
|桌子
dimension
-Predictor data observation dimension“行”
(default) |'列'
预测数据观察维度,指定为'列'
or“行”
。
笔记
如果您将预测器矩阵定向,以使观察值与列相对应并指定'ObservationsIn','columns'
,那么您可能会大大减少优化执行时间。您无法指定'ObservationsIn','columns'
对于表中的预测数据。
YHat
-Predicted responsesPredicted responses, returned as an-by-l数字矩阵。n是观察的数量X
andl是the number of regularization strengths inMDL。lambda
。YHat(
是观察的反应一世
,,,,j
)一世
使用具有正则强度的线性回归模型mdl.lambda(
。j
)
使用正规化强度的模型的预测响应j是
X是an observation from the predictor data matrixX
,是行矢量。
是the estimated column vector of coefficients. The software stores this vector inMDL。Beta(:,
。j
)
是the estimated, scalar bias, which the software stores inmdl.bias(
。j
)
模拟此模型中的10000个观测值
是一个10000 x-1000稀疏矩阵,具有10%非零标准正常元素。
e是random normal error with mean 0 and standard deviation 0.3.
rng(1)% For reproducibilityn = 1e4;d = 1e3;NZ = 0.1;X = Sprandn(n,d,nz);y = x(:,100) + 2*x(:,200) + 0.3*randn(n,1);
Train a linear regression model. Reserve 30% of the observations as a holdout sample.
CVMdl = fitrlinear(X,Y,'Holdout',,,,0。3); Mdl = CVMdl.Trained{1}
MDL=RegressionLinear ResponseName: 'Y' ResponseTransform: 'none' Beta: [1000x1 double] Bias: -0.0066 Lambda: 1.4286e-04 Learner: 'svm' Properties, Methods
CVMdl
是a回归专业
模型。It contains the propertyTrained
,,,,which is a 1-by-1 cell array holding aRegressionLinear
model that the software trained using the training set.
Extract the training and test data from the partition definition.
trainIdx = training(CVMdl.Partition); testIdx = test(CVMdl.Partition);
Predict the training- and test-sample responses.
yHatTrain = predict(Mdl,X(trainIdx,:)); yHatTest = predict(Mdl,X(testIdx,:));
因为有一个正规化强度MDL
,,,,yHatTrain
andYhattest
are numeric vectors.
Predict responses from the best-performing, linear regression model that uses a lasso-penalty and least squares.
Simulate 10000 observations as in预测测试样本响应。
rng(1)% For reproducibilityn = 1e4;d = 1e3;NZ = 0.1;X = Sprandn(n,d,nz);y = x(:,100) + 2*x(:,200) + 0.3*randn(n,1);
从 through 。
lambda = logspace(-5,-1,15);
Cross-validate the models. To increase execution speed, transpose the predictor data and specify that the observations are in columns. Optimize the objective function using SpaRSA.
X=X'; CVMdl = fitrlinear(X,Y,'ObservationsIn',,,,'列',,,,'KFold',,,,5,,,,'lambda',,,,lambda,。。。'学习者',,,,“最少”,,,,'Solver',,,,'sparsa',,,,“正则化”,,,,'套索');numCLModels = numel(CVMdl.Trained)
numCLModels = 5
CVMdl
是a回归专业
模型。因为fitrlinear
一世mplements 5-fold cross-validation,CVMdl
contains 5RegressionLinear
该软件在每个折叠上训练的模型。
Display the first trained linear regression model.
MDL1=CVMdl.Trained{1}
MDL1=RegressionLinear ResponseName: 'Y' ResponseTransform: 'none' Beta: [1000x15 double] Bias: [-0.0049 -0.0049 -0.0049 -0.0049 -0.0049 -0.0048 ... ] Lambda: [1.0000e-05 1.9307e-05 3.7276e-05 7.1969e-05 ... ] Learner: 'leastsquares' Properties, Methods
MDL1
是aRegressionLinear
模型对象。fitrlinear
constructedMDL1
通过在前四倍上进行训练。因为兰姆达
是a sequence of regularization strengths, you can think ofMDL1
as 11 models, one for each regularization strength in兰姆达
。
估计交叉验证的MSE。
mse = kfoldloss(cvmdl);
更高的值兰姆达
lead to predictor variable sparsity, which is a good quality of a regression model. For each regularization strength, train a linear regression model using the entire data set and the same options as when you cross-validated the models. Determine the number of nonzero coefficients per model.
MDL=fitrlinear(X,Y,'ObservationsIn',,,,'列',,,,'lambda',,,,lambda,。。。'学习者',,,,“最少”,,,,'Solver',,,,'sparsa',,,,“正则化”,,,,'套索');numNZCoeff = sum(Mdl.Beta~=0);
在同一图中,为每个正则化强度绘制跨验证的MSE和非零系数的频率。在日志刻度上绘制所有变量。
数字;[H,HL1,HL2] = Plotyy(Log10(Lambda),Log10(MSE),。。。log10(Lambda),log10(numNZCoeff)); hL1.Marker ='o';hl2.marker ='o';Ylabel(h(1),'log_ {10} mse')Ylabel(H(2),'log_{10} nonzero-coefficient frequency')Xlabel('log_ {10} lambda')hold离开
选择平衡预测变量稀疏性和低MSE的正规化强度的索引(例如兰姆达(10)
)。
一世dxFinal = 10;
提取与最小MSE相对应的模型。
MDLFinal = selectModels(Mdl,idxFinal)
MDLFinal = RegressionLinear ResponseName: 'Y' ResponseTransform: 'none' Beta: [1000x1 double] Bias: -0.0050 Lambda: 0.0037 Learner: 'leastsquares' Properties, Methods
idxnzcoeff =find(MdlFinal.Beta~=0)
idxnzcoeff =2×1100200
EstCoeff = Mdl.Beta(idxNZCoeff)
EstCoeff =2×11。00511。9965
MDLFinal
是aRegressionLinear
具有一个正规化强度的模型。非零系数EstCoeff
接近模拟数据的系数。
模拟10个新观察结果,并使用表现最佳模型预测相应的响应。
Xnew = Sprandn(D,10,NZ);yhat =预测(mdlfinal,Xnew,'ObservationsIn',,,,'列');
Usage notes and limitations:
您可以为两者生成C/C ++代码predict
andupdate
通过使用a coder configurer. Or, generate code only forpredict
通过使用saveLearnerForCoder
,,,,loadLearnerForCoder
,,,,andcodegen
。
代码生成predict
andupdate
-Create a coder configurer by usinglearnerCoderConfigurer
and then generate code by usinggenerateCode
。Then you can update model parameters in the generated code without having to regenerate the code.
代码生成predict
-Save a trained model by usingsaveLearnerForCoder
。Define an entry-point function that loads the saved model by usingloadLearnerForCoder
and calls thepredict
功能。Then usecodegen
(MATLAB Coder)为入口点函数生成代码。
为生成单精度的C/C ++代码predict
,,,,specify the name-value argument'DataType','single'
当您致电loadLearnerForCoder
功能。
This table contains notes about the arguments ofpredict
。Arguments not included in this table are fully supported.
Argument | 笔记s and Limitations |
---|---|
MDL |
For the usage notes and limitations of the model object, see代码生成of the |
X |
|
Name-value pair arguments |
|
For more information, seeIntroduction to Code Generation。
你有一个修改版的这个例子。做哟u want to open this example with your edits?
您单击了与此MATLAB命令相对应的链接:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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:。
Select网站您还可以从以下列表中选择一个网站:
选择中国网站(中文或英语)以获得最佳场地性能。其他Mathworks乡村网站未针对您所在的访问进行优化。