Introduction

Machine learning is a powerful tool for making predictions and uncovering patterns in data. MATLAB provides a comprehensive environment for developing machine learning models, from data preprocessing to model training and evaluation. In this section, we will cover the basics of machine learning in MATLAB, including key concepts, practical examples, and exercises to reinforce your understanding.

Key Concepts

  1. Supervised Learning: Learning from labeled data to make predictions.
  2. Unsupervised Learning: Finding patterns in data without labels.
  3. Training and Testing: Splitting data into training and testing sets to evaluate model performance.
  4. Model Evaluation: Assessing the accuracy and performance of a model using metrics like accuracy, precision, recall, and F1 score.

Getting Started with Machine Learning in MATLAB

Loading Data

MATLAB provides various functions to load and preprocess data. For example, you can use readtable to load data from a CSV file.

% Load data from a CSV file
data = readtable('data.csv');

Splitting Data

Splitting data into training and testing sets is crucial for evaluating model performance.

% Split data into training and testing sets
cv = cvpartition(height(data), 'HoldOut', 0.3);
trainData = data(training(cv), :);
testData = data(test(cv), :);

Preprocessing Data

Data preprocessing involves cleaning and transforming data to make it suitable for machine learning algorithms.

% Normalize features
trainDataNorm = normalize(trainData);
testDataNorm = normalize(testData);

Supervised Learning Example: Classification

Training a Classification Model

MATLAB provides various functions to train classification models, such as fitctree for decision trees.

% Train a decision tree classifier
model = fitctree(trainDataNorm, trainData.ClassLabel);

Evaluating the Model

Evaluate the model using the testing set and calculate performance metrics.

% Predict class labels for the test set
predictions = predict(model, testDataNorm);

% Calculate accuracy
accuracy = sum(predictions == testData.ClassLabel) / numel(testData.ClassLabel);
disp(['Accuracy: ', num2str(accuracy)]);

Unsupervised Learning Example: Clustering

Training a Clustering Model

MATLAB provides functions like kmeans for clustering data.

% Perform k-means clustering
[idx, C] = kmeans(trainDataNorm, 3);

Visualizing Clusters

Visualize the clusters using a scatter plot.

% Scatter plot of clusters
scatter(trainDataNorm(:,1), trainDataNorm(:,2), 10, idx, 'filled');
title('K-means Clustering');
xlabel('Feature 1');
ylabel('Feature 2');

Practical Exercises

Exercise 1: Train a Support Vector Machine (SVM) Classifier

  1. Load the fisheriris dataset.
  2. Split the data into training and testing sets.
  3. Train an SVM classifier using fitcsvm.
  4. Evaluate the model using the testing set and calculate the accuracy.

Solution

% Load the fisheriris dataset
load fisheriris;
data = array2table(meas, 'VariableNames', {'SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'});
data.ClassLabel = species;

% Split data into training and testing sets
cv = cvpartition(height(data), 'HoldOut', 0.3);
trainData = data(training(cv), :);
testData = data(test(cv), :);

% Train an SVM classifier
model = fitcsvm(trainData(:, 1:4), trainData.ClassLabel);

% Predict class labels for the test set
predictions = predict(model, testData(:, 1:4));

% Calculate accuracy
accuracy = sum(predictions == testData.ClassLabel) / numel(testData.ClassLabel);
disp(['Accuracy: ', num2str(accuracy)]);

Exercise 2: Perform Hierarchical Clustering

  1. Load the fisheriris dataset.
  2. Perform hierarchical clustering using linkage and cluster.
  3. Visualize the clusters using a dendrogram.

Solution

% Load the fisheriris dataset
load fisheriris;
data = array2table(meas, 'VariableNames', {'SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'});

% Perform hierarchical clustering
Z = linkage(data{:, 1:4}, 'average');
clusters = cluster(Z, 'maxclust', 3);

% Visualize the clusters using a dendrogram
dendrogram(Z);
title('Hierarchical Clustering Dendrogram');
xlabel('Sample Index');
ylabel('Distance');

Conclusion

In this section, we covered the basics of machine learning in MATLAB, including supervised and unsupervised learning. We provided practical examples and exercises to help you understand how to load, preprocess, and split data, train models, and evaluate their performance. With these foundational skills, you are now ready to explore more advanced machine learning techniques and applications in MATLAB.

© Copyright 2024. All rights reserved