问题描述:

The Classification Learner Gui provides the option to export the code, which looks like this

function [trainedClassifier, validationAccuracy] = trainClassifier(datasetTable)

% Convert input to table

datasetTable = table(datasetTable);

datasetTable.Properties.VariableNames = {'column'};

% Split matrices in the input table into vectors

datasetTable.column_1 = datasetTable.column(:,1);

datasetTable.column_2 = datasetTable.column(:,2);

datasetTable.column_3 = datasetTable.column(:,3);

datasetTable.column_4 = datasetTable.column(:,4);

datasetTable.column_5 = datasetTable.column(:,5);

datasetTable.column_6 = datasetTable.column(:,6);

datasetTable.column_7 = datasetTable.column(:,7);

datasetTable.column_8 = datasetTable.column(:,8);

datasetTable.column_9 = datasetTable.column(:,9);

datasetTable.column_10 = datasetTable.column(:,10);

datasetTable.column_11 = datasetTable.column(:,11);

datasetTable.column_12 = datasetTable.column(:,12);

datasetTable.column_13 = datasetTable.column(:,13);

datasetTable.column_14 = datasetTable.column(:,14);

datasetTable.column_15 = datasetTable.column(:,15);

datasetTable.column_16 = datasetTable.column(:,16);

datasetTable.column_17 = datasetTable.column(:,17);

datasetTable.column_18 = datasetTable.column(:,18);

datasetTable.column_19 = datasetTable.column(:,19);

datasetTable.column = [];

% Extract predictors and response

predictorNames = {'column_1', 'column_2', 'column_3', 'column_4', 'column_5', 'column_6', 'column_7', 'column_8', 'column_9', 'column_10', 'column_11', 'column_12', 'column_13', 'column_14', 'column_15', 'column_16', 'column_17', 'column_18'};

predictors = datasetTable(:,predictorNames);

predictors = table2array(varfun(@double, predictors));

response = datasetTable.column_19;

% Train a classifier

trainedClassifier = fitctree(predictors, response, 'PredictorNames', {'column_1' 'column_2' 'column_3' 'column_4' 'column_5' 'column_6' 'column_7' 'column_8' 'column_9' 'column_10' 'column_11' 'column_12' 'column_13' 'column_14' 'column_15' 'column_16' 'column_17' 'column_18'}, 'ResponseName', 'column_19', 'ClassNames', [0 1], 'SplitCriterion', 'gdi', 'MaxNumSplits', 20, 'Surrogate', 'off');

% Perform cross-validation

partitionedModel = crossval(trainedClassifier, 'KFold', 5);

% Compute validation accuracy

validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');

%% Uncomment this section to compute validation predictions and scores:

% % Compute validation predictions and scores

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

Now, I'd like to pass to trainClassifier a datasetTable of varying size and call it like this:

trainClassifier(datasetTable,tablesize)

So, there's gonna be a for-loop to fill datasetTable.column_i and predictorNames.

I'm not experienced in using tables, so I haven't managed to write something syntactically correct.

I think the strings in predictorNames can be created using

eval(sprintf('column_%d ', i));

So,what can you suggest about the variables of datasetTable?

相关阅读:
Top