predict
Predict responses for Gaussian kernel regression model
Syntax
Description
Examples
Predict Test Set Responses
Predict the test set responses using a Gaussian kernel regression model for the carbig
data set.
Load the carbig
data set.
load carbig
Specify the predictor variables (X
) and the response variable (Y
).
X = [Weight,Cylinders,Horsepower,Model_Year]; Y = MPG;
Delete rows of X
and Y
where either array has NaN
values. Removing rows with NaN
values before passing data to fitrkernel
can speed up training and reduce memory usage.
R = rmmissing([X Y]); X = R(:,1:4); Y = R(:,end);
Reserve 10% of the observations as a holdout sample. Extract the training and test indices from the partition definition.
rng(10) % For reproducibility N = length(Y); cvp = cvpartition(N,'Holdout',0.1); idxTrn = training(cvp); % Training set indices idxTest = test(cvp); % Test set indices
Standardize the training data and train the regression kernel model.
Xtrain = X(idxTrn,:);
Ytrain = Y(idxTrn);
[Ztrain,tr_mu,tr_sigma] = zscore(Xtrain); % Standardize the training data
tr_sigma(tr_sigma==0) = 1;
Mdl = fitrkernel(Ztrain,Ytrain)
Mdl = RegressionKernel ResponseName: 'Y' Learner: 'svm' NumExpansionDimensions: 128 KernelScale: 1 Lambda: 0.0028 BoxConstraint: 1 Epsilon: 0.8617 Properties, Methods
Mdl
is a RegressionKernel
model.
Standardize the test data using the same mean and standard deviation of the training data columns. Predict responses for the test set.
Xtest = X(idxTest,:);
Ztest = (Xtest-tr_mu)./tr_sigma; % Standardize the test data
Ytest = Y(idxTest);
YFit = predict(Mdl,Ztest);
Create a table containing the first 10 observed response values and predicted response values.
table(Ytest(1:10),YFit(1:10),'VariableNames', ... {'ObservedValue','PredictedValue'})
ans=10×2 table
ObservedValue PredictedValue
_____________ ______________
18 17.616
14 25.799
24 24.141
25 25.018
14 13.637
14 14.557
18 18.584
27 26.096
21 25.031
13 13.324
Estimate the test set regression loss using the mean squared error loss function.
L = loss(Mdl,Ztest,Ytest)
L = 9.2664
Input Arguments
Mdl
— Kernel regression model
RegressionKernel
model object
Kernel regression model, specified as a RegressionKernel
model object. You can create a
RegressionKernel
model object using fitrkernel
.
X
— Predictor data used to generate responses
numeric matrix | table
Predictor data used to generate responses, specified as a numeric matrix or table.
Each row of X
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 using theCategoricalPredictors
name-value pair argument offitrkernel
. 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 offitrkernel
. 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: double
| single
| table
Output Arguments
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, see Tall Arrays.
Version History
Introduced in R2018a
See Also
fitrkernel
| loss
| RegressionKernel
| resume
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 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 (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- 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)