How to Elegantly Use Matlab for Machine Learning?

How to Elegantly Use Matlab for Machine Learning?
Author|Liao Fan Chun Qiu

Source|Zhihu

When it comes to machine learning, everyone thinks of Python. Indeed, Python offers a wealth of machine learning libraries, such as sklearn, pytorch, tensorflow, etc. Many C++ libraries also provide Python interfaces, like dlib, which makes it very convenient to use.

Although Matlab is not as open as Python, it also provides a large number of machine learning algorithms, common ones include PCA, SVM, decision trees, ensemble learning, etc., which are more than sufficient for daily needs. More importantly, Matlab provides the functionality to convert algorithms to C code, allowing trained models to be converted into C code or dynamic link libraries (dll) for software use. The following image shows the algorithms in Matlab 2019b that can be converted to C, and this will continue to increase in the future. This seems to be something Python finds difficult to achieve; converting Python to C is impossible and requires the use of C++ libraries to replicate algorithms, such as using dlib.

How to Elegantly Use Matlab for Machine Learning?

Additionally, Matlab also provides a dedicated App interface that allows for machine learning without writing code, perfect!

How to Elegantly Use Matlab for Machine Learning?

Below is a brief introduction using a classifier as an example.

1. Load Data

Matlab comes with the fisher data

>> fishertable = readtable('fisheriris.csv');
How to Elegantly Use Matlab for Machine Learning?

2. Open Classifier App

>> classificationLearner
How to Elegantly Use Matlab for Machine Learning?

3. Import Data

Create a session>> from workspace

How to Elegantly Use Matlab for Machine Learning?

Start the session with default settings

  • Default selects the fishertable variable, as this is the only variable in the workspace;
  • Default selects the last column Species as response;
  • Default uses cross-validation.
How to Elegantly Use Matlab for Machine Learning?

4. Train Tree Model

Meaning of each menu page:
  1. Features: Select features to be included in training or use PCA for dimensionality reduction, by default all features are used without reduction
  2. Options: Set misclassification costs, i.e. priority of class accuracy, which must be classified correctly and which can accept some error
  3. Model Type: Choose training model and model parameter configuration
  4. Training: Use parallel processing to train multiple models simultaneously
  5. Plotting: Classifier performance evaluation, scatter plots, confusion matrix, ROC curve, etc.
  6. Export: Export training results, training code, or model files
How to Elegantly Use Matlab for Machine Learning?
For simplicity, we will directly use the default tree model for training (the parallel pool will open by default, just agree), the result is as follows: accuracy 96%, quite good
How to Elegantly Use Matlab for Machine Learning?

5. Model Ensemble

There are many classifiers that can be trained, choose All for an ensemble:
How to Elegantly Use Matlab for Machine Learning?
Start training, and you can see that there are 4 workers training simultaneously
How to Elegantly Use Matlab for Machine Learning?
Finally, the model with the highest accuracy is the linear discriminant model, with an accuracy of 98%
How to Elegantly Use Matlab for Machine Learning?

6. Export Function

If you want to see the Matlab training code, click Export>>Generate Function to get the training code: for similar applications in the future, you can run this function directly without reopening the App interface
function[trainedClassifier, validationAccuracy] = trainClassifier(trainingData)
% [trainedClassifier, validationAccuracy] = trainClassifier(trainingData)
% Returns the trained classifier and its accuracy. The following code recreates the classification model trained in the Classification Learner App.
% You can use this generated code to automatically train the same model based on new data, or to understand how to programmatically train a model.
%
% Input:
%     trainingData: A table containing predictor variables and response column that is the same as imported into the App.
%
% Output:
%     trainedClassifier: A structure containing the trained classifier. This structure has various fields with information about the trained classifier.
%
%     trainedClassifier.predictFcn: A function for predicting new data.
%
%     validationAccuracy: A double value containing the accuracy percentage. In the App, the "History" column displays this overall accuracy score for each model.
%
% Use this code to train the model based on new data. To retrain the classifier, use the original or new data as input parameter trainingData when calling this function from the command line.
%
% For example, to retrain the classifier trained on the original dataset T, enter:
%   [trainedClassifier, validationAccuracy] = trainClassifier(T)
%
% To predict new data T2 using the returned "trainedClassifier", use
%   yfit = trainedClassifier.predictFcn(T2)
%
% T2 must be a table containing at least the same predictor variable columns used during training. For more details, enter:
%   trainedClassifier.HowToPredict

% Extract predictor variables and response
% The following code processes the data into the appropriate shape to train the model.
%
inputTable = trainingData;
predictorNames = {'SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'};
predictors = inputTable(:, predictorNames);
response = inputTable.Species;
isCategoricalPredictor = [false, false, false, false];

% Train classifier
% The following code specifies all classifier options and trains the classifier.
classificationDiscriminant = fitcdiscr(...
    predictors, ...
    response, ...
    'DiscrimType', 'linear', ...
    'Gamma', 0, ...
    'FillCoeffs', 'off', ...
    'ClassNames', {'setosa'; 'versicolor'; 'virginica'});

% Create result structure using the prediction function
predictorExtractionFcn = @(t) t(:, predictorNames);
discriminantPredictFcn = @(x) predict(classificationDiscriminant, x);
trainedClassifier.predictFcn = @(x) discriminantPredictFcn(predictorExtractionFcn(x));

% Add fields to the result structure
trainedClassifier.RequiredVariables = {'PetalLength', 'PetalWidth', 'SepalLength', 'SepalWidth'};
trainedClassifier.ClassificationDiscriminant = classificationDiscriminant;
trainedClassifier.About = 'This structure is exported from the Classification Learner R2019b trained model.';
trainedClassifier.HowToPredict = sprintf('To predict new table T, use: \n yfit = c.predictFcn(T) \n replace ''c'' with the name of the variable for this structure, e.g., ''trainedModel''.\n \n Table T must contain the variables returned by: \n c.RequiredVariables \n Variable formats (e.g., matrix/vector, data types) must match the original training data.\n Ignore other variables.\n \n For more details, see <a href="matlab:helpview(fullfile(docroot, ''stats'', ''stats.map''), ''appclassification_exportmodeltoworkspace'')">How to predict using an exported model</a>.');

% Extract predictor variables and response
% The following code processes the data into the appropriate shape to train the model.
%
inputTable = trainingData;
predictorNames = {'SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'};
predictors = inputTable(:, predictorNames);
response = inputTable.Species;
isCategoricalPredictor = [false, false, false, false];

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

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

% Compute validation accuracy
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');
Other
To perform regression analysis, similarly use Regression Learner, the following image shows the regression algorithms provided by Matlab
How to Elegantly Use Matlab for Machine Learning?
Using Matlab for machine learning is very convenient, a zero-based foolproof introduction!
   ABOUT 
    About Us 

Deep Blue Academy is an online education platform focused on artificial intelligence, with tens of thousands of partners learning on the platform, many from well-known domestic and international institutions, such as Tsinghua and Peking University.

Leave a Comment