% Input: % trainingData: A table containing the same predictor and response columns. % Output: % trainedClassifier: A struct containing the trained classifier. % % trainedClassifier.predictFcn: A function to make predictions on new data. % % validationAccuracy: A double containing the accuracy as a percentage. % % 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 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. % trainedClassifier.HowToPredict % Extract predictors and response % This code processes the data into the right shape for training the model. inputTable = trainingData; predictorNames = {'HospitalCode', 'TypeDescription', 'Age', 'ServiceSupport', 'AssetCondition', 'ServiceIntention', 'FrequencyMaintenanceRequirement', 'MaintenanceComplexity', 'TotalDowntime', 'AlternativeBackup', 'Operations', 'MaintenanceCost', 'Make', 'Model', 'Manufacturer', 'Brand', 'PurchaseDate'}; predictors = inputTable(:, predictorNames); response = inputTable.MonthsToFailure; isCategoricalPredictor = [true, true, false, false, false, false, false, false, false, false, false, false, true, true, true, true, false]; % Create the result struct with predict function predictorExtractionFcn = @(t) t(:, predictorNames); trainedClassifier.predictFcn = @(x) predictorExtractionFcn(x); % Add additional fields to the result struct trainedClassifier.RequiredVariables = {'Age', 'AlternativeBackup', 'AssetCondition', 'Brand', 'FrequencyMaintenanceRequirement', 'HospitalCode', 'MaintenanceComplexity', 'MaintenanceCost', 'Make', 'Manufacturer', 'Model', 'Operations', 'PurchaseDate', 'ServiceIntention', 'ServiceSupport', 'TotalDowntime', 'TypeDescription'}; trainedClassifier.About = 'This struct is a trained model.'; trainedClassifier.HowToPredict = sprintf('To make predictions on a new table.'); % Extract predictors and response % This code processes the data into the right shape for training the model. inputTable = trainingData; predictorNames = {'HospitalCode', 'TypeDescription', 'Age', 'ServiceSupport', 'AssetCondition', 'ServiceIntention', 'FrequencyMaintenanceRequirement', 'MaintenanceComplexity', 'TotalDowntime', 'AlternativeBackup', 'Operations', 'MaintenanceCost', 'Make', 'Model', 'Manufacturer', 'Brand', 'PurchaseDate'}; predictors = inputTable(:, predictorNames); response = inputTable.MonthsToFailure; isCategoricalPredictor = [true, true, false, false, false, false, false, false, false, false, false, false, true, true, true, true, false];