function [trainedClassifier, validationAccuracy] = trainClassifier_IDHinLGGnew(trainingData)
% [trainedClassifier, validationAccuracy] = trainClassifier(trainingData)
% returns a trained classifier and its accuracy. This code recreates the
% classification model trained in Classification Learner app. Use the
% generated code to automate training the same model with new data, or to
% learn how to programmatically train models.
%
%  Input:
%      trainingData: a table containing the same predictor and response
%       columns as imported into the app.
%
%  Output:
%      trainedClassifier: a struct containing the trained classifier. The
%       struct contains various fields with information about the trained
%       classifier.
%
%      trainedClassifier.predictFcn: a function to make predictions on new
%       data.
%
%      validationAccuracy: a double containing the accuracy in percent. In
%       the app, the History list displays this overall accuracy score for
%       each model.
%
% Use the code to train the model with new data. To retrain your
% classifier, call the function from the command line with your original
% data or new data as the input argument trainingData.
%
% For example, to retrain a classifier trained with the original data set
% T, enter:
%   [trainedClassifier, validationAccuracy] = trainClassifier(T)
%
% To make predictions with the returned 'trainedClassifier' on new data T2,
% use
%   yfit = trainedClassifier.predictFcn(T2)
%
% T2 must be a table containing at least the same predictor columns as used
% during training. For details, enter:
%   trainedClassifier.HowToPredict

% Auto-generated by MATLAB on 09-Apr-2018 23:42:53


% Extract predictors and response
% This code processes the data into the right shape for training the
% model.
inputTable = trainingData;
predictorNames = {'CET1_ORI_F56', 'CET1_ORI_F886', 'CET1_ORI_F910', 'CET1_ORI_F911', 'CET1_ORI_F915', 'CET1_ORI_F917', 'CET1_ORI_F1081', 'CET1_LLL_F189', 'CET1_LLL_F190', 'CET1_LLL_F191', 'CET1_LLL_F194', 'CET1_LLL_F195', 'CET1_LLL_F197', 'CET1_LHH_F26', 'CET1_LHH_F29', 'CET1_LHH_F31', 'CET1_LHH_F32', 'CET1_LHH_F42', 'CET1_HLL_F42', 'CET1_HLL_F60', 'CET1_HLL_F65', 'CET1_HLL_F71', 'CET1_HHH_F49', 'T2_ORI_F487', 'T2_HLL_F29', 'T2_HLL_F31', 'T2_HLL_F32', 'T2_HLL_F42', 'T2_HLL_F54', 'FLAIR_LLL_F727', 'FLAIR_HHL_F8', 'FLAIR_HHL_F47', 'ADC_ORI_F846', 'ADC_ORI_F847', 'ADC_ORI_F855', 'ADC_ORI_F917', 'ADC_HHL_F62', 'ADC_HHL_F727', 'ADC_HHH_F56', 'age', 'gender'};
predictors = inputTable(:, predictorNames);
response = inputTable.IDH;
isCategoricalPredictor = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true];

% Train a classifier
% This code specifies all the classifier options and trains the classifier.
classificationSVM = fitcsvm(...
    predictors, ...
    response, ...
    'KernelFunction', 'linear', ...
    'PolynomialOrder', [], ...
    'KernelScale', 'auto', ...
    'BoxConstraint', 1, ...
    'Standardize', true, ...
    'ClassNames', {'Mutant'; 'WT'});

% Create the result struct with predict function
predictorExtractionFcn = @(t) t(:, predictorNames);
svmPredictFcn = @(x) predict(classificationSVM, x);
trainedClassifier.predictFcn = @(x) svmPredictFcn(predictorExtractionFcn(x));

% Add additional fields to the result struct
trainedClassifier.RequiredVariables = {'CET1_ORI_F56', 'CET1_ORI_F886', 'CET1_ORI_F910', 'CET1_ORI_F911', 'CET1_ORI_F915', 'CET1_ORI_F917', 'CET1_ORI_F1081', 'CET1_LLL_F189', 'CET1_LLL_F190', 'CET1_LLL_F191', 'CET1_LLL_F194', 'CET1_LLL_F195', 'CET1_LLL_F197', 'CET1_LHH_F26', 'CET1_LHH_F29', 'CET1_LHH_F31', 'CET1_LHH_F32', 'CET1_LHH_F42', 'CET1_HLL_F42', 'CET1_HLL_F60', 'CET1_HLL_F65', 'CET1_HLL_F71', 'CET1_HHH_F49', 'T2_ORI_F487', 'T2_HLL_F29', 'T2_HLL_F31', 'T2_HLL_F32', 'T2_HLL_F42', 'T2_HLL_F54', 'FLAIR_LLL_F727', 'FLAIR_HHL_F8', 'FLAIR_HHL_F47', 'ADC_ORI_F846', 'ADC_ORI_F847', 'ADC_ORI_F855', 'ADC_ORI_F917', 'ADC_HHL_F62', 'ADC_HHL_F727', 'ADC_HHH_F56', 'age', 'gender'};
trainedClassifier.ClassificationSVM = classificationSVM;
trainedClassifier.About = 'This struct is a trained model exported from Classification Learner R2017a.';
trainedClassifier.HowToPredict = sprintf('To make predictions on a new table, T, use: \n  yfit = c.predictFcn(T) \nreplacing ''c'' with the name of the variable that is this struct, e.g. ''trainedModel''. \n \nThe table, T, must contain the variables returned by: \n  c.RequiredVariables \nVariable formats (e.g. matrix/vector, datatype) must match the original training data. \nAdditional variables are ignored. \n \nFor more information, see <a href="matlab:helpview(fullfile(docroot, ''stats'', ''stats.map''), ''appclassification_exportmodeltoworkspace'')">How to predict using an exported model</a>.');

% Extract predictors and response
% This code processes the data into the right shape for training the
% model.
inputTable = trainingData;
predictorNames = {'CET1_ORI_F56', 'CET1_ORI_F886', 'CET1_ORI_F910', 'CET1_ORI_F911', 'CET1_ORI_F915', 'CET1_ORI_F917', 'CET1_ORI_F1081', 'CET1_LLL_F189', 'CET1_LLL_F190', 'CET1_LLL_F191', 'CET1_LLL_F194', 'CET1_LLL_F195', 'CET1_LLL_F197', 'CET1_LHH_F26', 'CET1_LHH_F29', 'CET1_LHH_F31', 'CET1_LHH_F32', 'CET1_LHH_F42', 'CET1_HLL_F42', 'CET1_HLL_F60', 'CET1_HLL_F65', 'CET1_HLL_F71', 'CET1_HHH_F49', 'T2_ORI_F487', 'T2_HLL_F29', 'T2_HLL_F31', 'T2_HLL_F32', 'T2_HLL_F42', 'T2_HLL_F54', 'FLAIR_LLL_F727', 'FLAIR_HHL_F8', 'FLAIR_HHL_F47', 'ADC_ORI_F846', 'ADC_ORI_F847', 'ADC_ORI_F855', 'ADC_ORI_F917', 'ADC_HHL_F62', 'ADC_HHL_F727', 'ADC_HHH_F56', 'age', 'gender'};
predictors = inputTable(:, predictorNames);
response = inputTable.IDH;
isCategoricalPredictor = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true];

% Perform cross-validation
partitionedModel = crossval(trainedClassifier.ClassificationSVM, 'KFold', 5);

% Compute validation predictions
[validationPredictions, validationScores] = kfoldPredict(partitionedModel);

% Compute validation accuracy
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');
