kfoldfun
Cross-validate function for regression
Description
Examples
Estimate Regression Loss Using Custom Loss Function
Train a regression tree model, and then cross-validate it using a custom k-fold loss function.
Load the imports-85
data set. Train a regression tree using a subset of the data.
load imports-85 Mdl = fitrtree(X(:,[4 5]),X(:,16),... 'PredictorNames',{'Length','Width'},... 'ResponseName','Price');
Cross-validate the regression tree, and obtain the mean squared error.
CVMdl = crossval(Mdl); L = kfoldLoss(CVMdl)
L = 1.9167e+07
Examine the error when you use a simple averaging of training responses instead of predictions in the calculation.
f = @(CMP,Xtrain,Ytrain,Wtrain,Xtest,Ytest,Wtest)...
mean((Ytest-mean(Ytrain)).^2)
f = function_handle with value:
@(CMP,Xtrain,Ytrain,Wtrain,Xtest,Ytest,Wtest)mean((Ytest-mean(Ytrain)).^2)
mean(kfoldfun(CVMdl,f))
ans = 6.3586e+07
Input Arguments
CVMdl
— Cross-validated model
RegressionPartitionedModel
object | RegressionPartitionedEnsemble
object | RegressionPartitionedGAM
object | RegressionPartitionedGP
object | RegressionPartitionedNeuralNetwork
object | RegressionPartitionedSVM
object
Cross-validated model, specified as a RegressionPartitionedModel
object, RegressionPartitionedEnsemble
object, RegressionPartitionedGAM
object, RegressionPartitionedGP
object, RegressionPartitionedNeuralNetwork
, or RegressionPartitionedSVM
object.
fun
— Cross-validated function
function handle
Cross-validated function, specified as a function handle. fun
has the
syntax:
testvals = fun(CMP,Xtrain,Ytrain,Wtrain,Xtest,Ytest,Wtest)
CMP
is a compact model stored in one element of theCVMdl
.Trained
property.Xtrain
is the training matrix of predictor values.Ytrain
is the training array of response values.Wtrain
are the training weights for observations.Xtest
andYtest
are the test data, with associated weightsWtest
.The returned value
testvals
must have the same size across all folds.
Data Types: function_handle
Output Arguments
vals
— Cross-validation results
numeric matrix
Cross-validation results, returned as a numeric matrix. vals
contains the
arrays of testvals
output, concatenated vertically over all folds.
For example, if testvals
from every fold is a numeric vector of
length N
, kfoldfun
returns a
KFold
-by-N
numeric matrix with one row per
fold.
Data Types: double
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
This function fully supports GPU arrays for the following models.
RegressionPartitionedModel
object fitted usingfitrtree
, or by passing aRegressionTree
object tocrossval
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2011aR2024b: Specify GPU arrays for neural network models (requires Parallel Computing Toolbox)
kfoldfun
fully supports GPU arrays for RegressionPartitionedNeuralNetwork
models.
R2023a: GPU support for RegressionPartitionedSVM
models
Starting in R2023a, kfoldfun
fully supports GPU arrays for RegressionPartitionedSVM
models.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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)