%% Prevent Overfitting Using K-Fold Cross-Validation
% To prevent the overfitting of your tuned FIS to your training data using k-fold cross validation.
%
% Load training data. This training data set has one input and one output.

load fuzex1trnData.dat

%
% Create a fuzzy inference system for the training data.

opt = genfisOptions('GridPartition');
opt.NumMembershipFunctions = 4;
opt.InputMembershipFunctionType = "gaussmf";
inputData = fuzex1trnData(:,1);
outputData = fuzex1trnData(:,2);
fis = genfis(inputData,outputData,opt);

%
% For reproducibility, set the random number generator seed.

rng('default')

%
% Configure the options for tuning the FIS. Use the default tuning method with a maximum of 30 iterations.

tuningOpt = tunefisOptions;
tuningOpt.MethodOptions.MaxGenerations = 30;

%
% Configure the following options for using k-fold cross validation.
% Use a k-fold value of 3.
% Compute the moving average of the validation cost using a window of length 2.
% Stop each training-validation iteration when the average cost is 5% greater than the current minimum cost.

tuningOpt.KFoldValue = 3;
tuningOpt.ValidationWindowSize = 2;
tuningOpt.ValidationTolerance = 0.05;

%
% Obtain the settings for tuning the membership function parameters of the FIS.

 [in,out] = getTunableSettings(fis);

%
% Tune the FIS.

[outputFIS,info] = tunefis(fis,[in;out],inputData,outputData,tuningOpt);

%
% Evaluate the FIS for each of the training input values.

outputTuned = evalfis(outputFIS,inputData);

%
% Plot the output of the tuned FIS along with the expected training output.

plot([outputData,outputTuned])
legend("Expected Output","Tuned Output","Location","southeast")
xlabel("Data Index")
ylabel("Output value")

%
% Copyright 2019 The MathWorks, Inc.