0% found this document useful (0 votes)
5 views17 pages

CPEN 429 Lab 2-Machine Learning With Python and MATLAB

The document outlines a lab task for students at the University of Ghana to develop a predictive maintenance model using MATLAB, focusing on data preprocessing, feature engineering, model training, and evaluation. Students will utilize various datasets, including the NASA Turbofan Engine Degradation Simulation dataset, to predict machinery failure based on sensor data. The task includes steps for data acquisition, model training using machine learning algorithms, and evaluation of model performance, with enhancements for advanced learning topics such as deep learning and hyperparameter optimization.

Uploaded by

Kwesi Adinkrah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

CPEN 429 Lab 2-Machine Learning With Python and MATLAB

The document outlines a lab task for students at the University of Ghana to develop a predictive maintenance model using MATLAB, focusing on data preprocessing, feature engineering, model training, and evaluation. Students will utilize various datasets, including the NASA Turbofan Engine Degradation Simulation dataset, to predict machinery failure based on sensor data. The task includes steps for data acquisition, model training using machine learning algorithms, and evaluation of model performance, with enhancements for advanced learning topics such as deep learning and hyperparameter optimization.

Uploaded by

Kwesi Adinkrah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIVERSITY OF GHANA

School of Engineering Sciences


Department of Computer Engineering
First Semester 2025/2026 Academic Year
CPEN 429: Emerging Trends in Computer Engineering
(3 credits)
Total points: 100 points
LAB 3: BUILDING AND TRAINING MACHINE LEARNING MODELS

Task 1: Predictive Maintenance Using Machine Learning

Focus: Data Preprocessing, Feature Engineering, Model Training, and Evaluation


Tools: MATLAB

Objective:

Students will use MATLAB to develop a predictive maintenance model. The goal is to predict
machinery failure using sensor data, focusing on data preprocessing, feature extraction, and model
performance evaluation.

Steps:

1. Data Preprocessing:
o Load a dataset (e.g., vibration, temperature, and RPM sensor data from MATLAB's
Predictive Maintenance Toolbox).
o Handle missing values, normalize the data, and apply PCA for dimensionality
reduction.
2. Feature Engineering:
o Extract time-domain and frequency-domain features using MATLAB's Signal
Processing Toolbox.
o Perform feature selection using feature importance metrics.
3. Model Training:
o Split the dataset into training, validation, and testing sets.
oTrain a classification model (e.g., Random Forest, SVM) using MATLAB's
Statistics and Machine Learning Toolbox.
4. Model Evaluation:
o Evaluate performance using confusion matrix, precision, recall, and F1-score.
o Visualize results using MATLAB's plotting tools (e.g., ROC curves).
5. Report and Presentation:
o Document preprocessing, feature engineering, and results.
o Create a presentation summarizing findings.

Libraries/Toolboxes:

• Predictive Maintenance Toolbox: For predictive maintenance workflows.


• Statistics and Machine Learning Toolbox: For model training and evaluation.
• Signal Processing Toolbox: For feature extraction.
• MATLAB Visualization Tools: For result visualization.

Task 1: Predictive Maintenance Using Machine Learning in MATLAB

Objective:
To predict equipment failure using sensor data sourced from the Kaggle or UCI Machine
Learning Repository, implementing machine learning algorithms for predictive maintenance.

Steps

Step 1: Data Acquisition

1. Turbofan Engine Degradation Simulation Data Set (CMAPSS)

• Source: NASA and available on Kaggle


• Description: This dataset contains simulated turbofan engine degradation data that
tracks engine cycles over time with sensor readings, useful for predicting
Remaining Useful Life (RUL).
• Kaggle Link: NASA Turbofan Engine Degradation Simulation Dataset
• UCI Repository: Not available here but accessible directly from NASA.

2. Air Quality Prediction and Maintenance Data

• Source: Kaggle
• Description: This dataset is designed to forecast equipment maintenance based on
air quality metrics, with readings from various sensors over time. It is useful for
analyzing and predicting when maintenance might be needed.
• Kaggle Link: Air Quality Prediction and Maintenance Data

3. PUMS Maintenance Data

• Source: UCI Machine Learning Repository


• Description: This dataset provides data for preventive maintenance activities in
industrial settings, including feature values that can be used to predict machine
failure or maintenance needs.
• UCI Repository Link: PUMS Maintenance Dataset

4. Predictive Maintenance Dataset for IoT Applications

• Source: Kaggle
• Description: This dataset includes data for predictive maintenance in industrial
machines and provides features related to sensor data, operational settings, and
machine failure predictions.
• Kaggle Link: Predictive Maintenance Dataset for IoT Applications

5. Sensor Data from the Hydraulic System (Hydraulic Test Rig Data)

• Source: UCI Machine Learning Repository


• Description: This dataset captures sensor data from a hydraulic test rig, which can
be used to predict component failure or the need for maintenance.
• UCI Repository Link: Hydraulic System Condition Monitoring Data Set

These datasets provide ample features and a variety of sensor data ideal for building
predictive maintenance models. Each dataset includes relevant sensor measurements and
sometimes failure labels, making them suitable for predicting RUL or maintenance needs
based on sensor data trends.

2. Save the dataset as a CSV file in your working directory.

Task Overview and Objective: NASA Turbofan Engine Degradation Simulation

In this task, you will work with the NASA Turbofan Engine Degradation Simulation dataset,
which consists of sensor data from a fleet of turbofan engines. The goal is to predict the
Remaining Useful Life (RUL) of the engines based on operational data from sensors and
operational settings over multiple cycles.

Objective:

• Build a predictive model that can accurately estimate the Remaining Useful Life (RUL)
of an engine given various sensor readings and operating conditions.
• Evaluate the model's performance by comparing predicted RUL values with actual RUL
data.
Step 2: Load and Explore the Data

% Load the dataset


trainData = readtable('train_FD001.csv');
testData = readtable('test_FD001_with_RUL.csv');

% Display the first few rows to confirm loading


disp('Train Data:');
head(trainData)

disp('Test Data:');
head(testData)

Step 3: Preprocess the Data

• Steps:
o Handle missing values.
o Normalize/standardize sensor readings.

Handle Missing values


% Check for missing values in the dataset
missingTrain = sum(ismissing(trainData));
missingTest = sum(ismissing(testData));

disp('Missing values in train data:');


disp(missingTrain);

disp('Missing values in test data:');


disp(missingTest);

% Handle missing values (remove rows with missing values)


trainData = rmmissing(trainData);
testData = rmmissing(testData);

Step 4: Split Data into Training and Testing Sets

% Set the random seed for reproducibility


rng(42);

% Proportion of data for training


trainProportion = 0.8;

% Get the total number of unique engine IDs


uniqueIDs = unique([Link]);

% Shuffle the engine IDs


shuffledIDs = uniqueIDs(randperm(length(uniqueIDs)));

% Split the IDs into training and testing subsets


numTrainIDs = round(trainProportion * length(shuffledIDs));
trainIDs = shuffledIDs(1:numTrainIDs);
testIDs = shuffledIDs(numTrainIDs+1:end);

% Create training and testing subsets


trainSubset = trainData(ismember([Link], trainIDs), :);
testSubset = trainData(ismember([Link], testIDs), :);

% Display sizes of the splits


fprintf('Training data size: %d rows\n', height(trainSubset));
fprintf('Testing data size: %d rows\n', height(testSubset));

Step 5: Train Machine Learning Models

1. Train a regression model (e.g., Random Forest) to predict RUL.


2. Use MATLAB's Regression Learner App or manual coding.

% Train a Random Forest model


rng(0); % Set random seed for reproducibility
model = fitrensemble(trainData{:, sensorColumns}, [Link],
...
'Method', 'Bag', 'NumLearningCycles', 50,
'Learners', 'Tree');

% Evaluate on training set


trainPredictions = predict(model, trainData{:, sensorColumns});
trainError = mean(abs(trainPredictions - [Link]));
disp(['Training Error: ', num2str(trainError)]);

Step 6: Test the Model

% Get the list of predictor variable names expected by trainedModel


predictorNames = [Link];
disp('Predictor Variables Required by the Model:');
disp(predictorNames);

% Extract only the required predictors from testSubset


testFeatures = testSubset(:, predictorNames);

%%
% Use the model to make predictions
predictedRUL = [Link](testFeatures);

% Evaluate Model Performance


trueRUL = [Link];

% Calculate Mean Absolute Error (MAE)


mae = mean(abs(trueRUL - predictedRUL));
fprintf('Mean Absolute Error (MAE): %.4f\n', mae);

% Calculate Root Mean Squared Error (RMSE)


rmse = sqrt(mean((trueRUL - predictedRUL).^2));
fprintf('Root Mean Squared Error (RMSE): %.4f\n', rmse);

% Calculate R-Squared (R²)


ssRes = sum((trueRUL - predictedRUL).^2); % Residual Sum of Squares
ssTot = sum((trueRUL - mean(trueRUL)).^2); % Total Sum of Squares
rSquared = 1 - (ssRes / ssTot);
fprintf('R-Squared (R²): %.4f\n', rSquared);

% Display first few predictions vs. true values for reference


disp('First few predictions vs. true values:');
disp(table(trueRUL(1:10), predictedRUL(1:10), 'VariableNames', {'TrueRUL',
'PredictedRUL'}));

%%
% Plot Actual vs Predicted RUL
figure;
scatter(trueRUL, predictedRUL, 'b', 'filled'); % Scatter plot
hold on;
plot([min(trueRUL), max(trueRUL)], [min(trueRUL), max(trueRUL)], 'r-',
'LineWidth', 1.5); % y=x line
hold off;
xlabel('Actual RUL');
ylabel('Predicted RUL');
title('Actual vs Predicted RUL');
legend({'Predicted', 'Ideal Fit'}, 'Location', 'Best');
grid on;

Enhancements

1. Feature Engineering:
o Calculate statistical features (e.g., mean, standard deviation) for each sensor.
o Use Principal Component Analysis (PCA) for dimensionality reduction.
2. Deep Learning:
o Implement a Long Short-Term Memory (LSTM) network for sequential data
prediction.
3. Ensemble Methods:
o Combine predictions from multiple models (e.g., Random Forest, Gradient
Boosting) using weighted averaging.
4. Cross-Validation:
o Perform k-fold cross-validation for more reliable model evaluation.

% Example: PCA on sensor data


[coeff, score] = pca(trainData{:, sensorColumns});
explained = cumsum(var(score) ./ sum(var(score)) 100);
disp('Cumulative variance explained by principal components:');
disp(explained);

Extended Homework

1. Explore Additional Datasets:


o Use a different dataset from Kaggle or UCI with similar features.
o Compare model performance across datasets.
2. Experiment with Different Algorithms:
o Implement Support Vector Machines (SVM) and Gradient Boosting.
o Compare their performance with Random Forest.
3. Optimize Hyperparameters:
o Use MATLAB's Bayesopt for hyperparameter optimization.
4. LSTM Implementation:
o Develop an LSTM network for time series prediction.
o Compare the LSTM's performance with traditional ML models.

% Homework Hint: Use Deep Learning Toolbox for LSTM


layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(100, 'OutputMode', 'sequence')
fullyConnectedLayer(1)
regressionLayer];

options = trainingOptions('adam', ...


'MaxEpochs', 50, ...
'MiniBatchSize', 32, ...
'Plots', 'training-progress');

5. Deploy the Model:


o Save the trained model and create a MATLAB App or a web interface for real-
time predictions.

This task introduces key concepts of predictive maintenance using machine learning while
allowing students to explore advanced topics and extend their learning through the enhancements
and homework provided.

For Task 1 (Predictive Maintenance Using Machine Learning in MATLAB), you can design a lab
that uses the Classification Learner App and other MATLAB tools to predict maintenance needs
or Remaining Useful Life (RUL). Below are step-by-step instructions that guide through data
preprocessing, feature selection, model training, and evaluation.

Dataset Selection

To follow along, download one of the datasets that works well with maintenance prediction, such
as:
• Turbofan Engine Degradation Simulation Data Set (CMAPSS) from Kaggle or directly
from NASA's Prognostics Data Repository.
Task Overview

The goal is to use MATLAB's Classification Learner App to predict the likelihood of an engine
failure within a specific period, based on sensor readings and operational data. You'll preprocess
the data, split it into training and testing sets, and train several classification models to compare
their [Link] dataset required is in the ClassificationTechnique dataset

Task Steps

Step 1: Load and Explore the Dataset

1. Load the dataset into MATLAB. You can use readtable if the data is in CSV format:

% Load the training and testing datasets


trainData = readtable('Training_1_all_features.csv');
testData = readtable('Test_classification_1.csv');

2. Display the first few rows to understand the structure:

% Display the first few rows of the dataset


head(trainData) % For training data
head(testData) % For testing data

3. Identify columns corresponding to sensor readings, operating conditions, and the


target label (labels).

Step 2: Data Preprocessing

1. Handling Missing Data: Check for and handle missing values if present:
2. Normalize the data in the train and test data:

%%
% Normalize sensor measurements in the training and test data
features = trainData{:, 3:end-1}; % Select feature columns (exclude ID,
Cycle, and labels)
[featuresNorm, mu, sigma] = zscore(features); % Standardize training
features
trainData{:, 3:end-1} = featuresNorm;

% Apply the same normalization to the test set


testData{:, 3:end-1} = (testData{:, 3:end-1} - mu) ./ sigma;

data = rmmissing(data); % Remove rows with missing values

• Feature Engineering: Calculate useful features from the sensor data, such as averages,
variances, or rolling averages of sensor readings to improve model performance.
• Label Encoding: The label encoding has been done already,check the readme in the
classification technique folder

Split Data into training and testing sets:


%%
% Split trainData into features (X) and labels (Y)
X_train = trainData{:, 3:end-1}; % Features (exclude ID, Cycle, and labels)
Y_train = [Link]; % Labels

% Optional: Split into further training and validation subsets


cv = cvpartition(Y_train, 'Holdout', 0.2);
X_trainSub = X_train(training(cv), :);
Y_trainSub = Y_train(training(cv), :);
X_val = X_train(test(cv), :);
Y_val = Y_train(test(cv), :);

Step 3: Load Data into the Classification Learner App

1. Open Classification Learner App in MATLAB:


o Type classificationLearner in the command window to open the app.
2. Import Data:
o In the Classification Learner App, choose New Session > From Workspace and
select trainingData.
3. Select Features and Target:
o Select the columns that represent the sensor data and operational conditions as
features, and labels the target.

Step 4: Train and Evaluate Models

1. Choose Models:
o In the app, select different models to train, such as Decision Trees, Support
Vector Machines (SVMs), k-Nearest Neighbors (k-NN), and Ensemble
Methods.
2. Training:
o Train each model by selecting Train All or train each model individually.
3. Evaluate Performance:
o Compare model accuracies using the app's built-in metrics, such as Accuracy,
Confusion Matrix, and ROC Curve.
4. Optimize the Best Model:
o Use Hyperparameter Tuning to improve the model's performance. Choose the
best-performing model and adjust hyperparameters to see if you can further
enhance accuracy.

Step 5: Export and Test the Model


1. Export the Best Model:
o After identifying the best model, export it to the MATLAB workspace by clicking
Export Model in the app.
2. Test on New Data:
o Use the test data to evaluate the model's performance:

%%
%disp([Link])
testFeatures = testData(:, [Link]);
predictedLabels = [Link](testFeatures);

% Actual labels from test data


actualLabels = [Link];

% Calculate and display accuracy


accuracy = sum(predictedLabels == actualLabels) / numel(actualLabels);
fprintf('Test Accuracy: %.2f%%\n', accuracy * 100);
%%
figure;
scatter(1:numel(actualLabels), actualLabels, 'r', 'filled'); % Actual
labels
hold on;
scatter(1:numel(predictedLabels), predictedLabels, 'b'); % Predicted
labels
hold off;
legend('Actual Labels', 'Predicted Labels');
xlabel('Sample Index');
ylabel('Class');
title('Predictions vs Actual Labels');

Enhancements to the Task

1. Time-Series Feature Engineering:


o Add rolling statistics or exponentially weighted moving averages for sensor
readings to capture trends over time.
2. Feature Selection:
o Use MATLAB's sequentialfs function to automatically select the most
important features, which can improve model performance and reduce
computational cost:

[selectedFeatures, history] = sequentialfs(@classf,


trainingData, targetVariable);

1. Experiment with Regression Models:


o Use Regression Learner App to predict Remaining Useful Life (RUL) as a
regression problem instead of a binary classification problem.

Extended Homework
For homework, students should:

1. Apply Additional Models:


o Use other algorithms like Neural Networks, XGBoost, or custom deep learning
models.
2. Visualize Sensor Data Trends:
o Create plots to visualize sensor trends over time and how they correlate with
failures.
3. Hyperparameter Tuning:
o Perform a grid search or Bayesian optimization for hyperparameters outside of the
Classification Learner App to gain hands-on experience with tuning.
4. Compare Classification and Regression Approaches:
o Run the task as a regression problem and compare RUL predictions with binary
classification to determine which approach provides better insights.
Instructions for Creating a MATLAB App for Predictive Maintenance

This guide walks you through building a MATLAB App for Predictive Maintenance. The app
will allow users to load data, preprocess it, train a machine learning model, and visualize the
results interactively.

Step 1: Open the App Designer

1. Launch MATLAB and click on "App Designer" under the "Apps" tab.
2. Click on "New App" and select "Blank App".

Step 2: Design the App Interface

Add Components:

1. Load Data Section:


o Button: Add a button labeled "Load Data".
o UI Table: Add a table to display the dataset after loading.
2. Preprocessing Section:
o Button: Add a button labeled "Preprocess Data".
3. Model Training Section:
o Drop-Down Menu: Add a dropdown menu to select the ML algorithm (e.g.,
Random Forest, SVM).
o Button: Add a button labeled "Train Model".
4. Model Testing Section:
o Button: Add a button labeled "Test Model".
o Axes: Add a plot area to visualize the predictions.
5. Additional Components:
o Label: Add labels for instructions or titles for each section.
o Text Area: Add a text area to display evaluation metrics.

Step 3: Write the Callback Functions

1. Load Data Button Callback:


o This function allows the user to load a dataset.

function LoadDataButtonPushed(app, event)


[file, path] = uigetfile('.csv');
if isequal(file, 0)
uialert([Link], 'No file selected!', 'Error');
else
[Link] = readtable(fullfile(path, file));
[Link] = [Link];
uialert([Link], 'Data Loaded Successfully!',
'Success');
end
end

Preprocess Data Button Callback:

• Preprocess the data, such as normalizing and creating RUL.

function PreprocessDataButtonPushed(app, event)


sensorColumns = 3:26; % Example sensor columns
[Link]{:, sensorColumns} = normalize([Link]{:,
sensorColumns});
maxCycle = max([Link]);
[Link] = maxCycle - [Link];
uialert([Link], 'Data Preprocessed Successfully!',
'Success');
end

Train Model Button Callback:

• Train the selected model.

function TrainModelButtonPushed(app, event)


modelType = [Link];
trainData = [Link](1:round(0.8height([Link])), :);
sensorColumns = 3:26;

switch modelType
case 'Random Forest'
[Link] = fitrensemble(trainData{:,
sensorColumns}, [Link], ...
'Method', 'Bag',
'NumLearningCycles', 50, 'Learners', 'Tree');
case 'SVM'
[Link] = fitrsvm(trainData{:, sensorColumns},
[Link]);
end
uialert([Link], 'Model Trained Successfully!',
'Success');
end

Test Model Button Callback:


• Test the trained model and visualize predictions.

function TestModelButtonPushed(app, event)


testData = [Link](round(0.8height([Link]))+1:end, :);
sensorColumns = 3:26;
predictions = predict([Link], testData{:,
sensorColumns});
actualRUL = [Link];

% Display metrics
testError = mean(abs(predictions - actualRUL));
[Link] = sprintf('Test Error: %.2f', testError);

% Plot predictions
plot([Link], actualRUL, predictions, 'o');
xlabel([Link], 'Actual RUL');
ylabel([Link], 'Predicted RUL');
title([Link], 'Actual vs Predicted RUL');
end

Step 4: Test the App

1. Save the app as [Link].


2. Click Run in the App Designer to test the app.
3. Interact with the app:
o Load a dataset using the "Load Data" button.
o Preprocess the data with "Preprocess Data".
o Select an algorithm from the dropdown and click "Train Model".
o Test the model with "Test Model" and view the results.

Step 5: Enhance the App

1. Add More Algorithms:


o Include options for Gradient Boosting or LSTM networks.
2. Real-Time Predictions:
o Add a feature to upload new sensor data for real-time predictions.
3. Save and Load Models:
o Include buttons to save and load trained models using MATLAB's save and load
functions.
4. Export Results:
o Allow users to export the results (predictions and metrics) as a CSV file.

function ExportResultsButtonPushed(app, event)


[file, path] = uiputfile('[Link]');
if ~isequal(file, 0)
results = table(actualRUL, predictions, 'VariableNames',
{'ActualRUL', 'PredictedRUL'});
writetable(results, fullfile(path, file));
uialert([Link], 'Results Exported Successfully!',
'Success');
end
end

Step 6: Assign Homework

• Extend the app to:


1. Implement k-fold cross-validation.
2. Add a feature importance plot for Random Forest.
3. Use PCA to visualize high-dimensional data in 2D or 3D plots.
REFLECTION QUESTIONS FOR TASK 1 (Predictive Maintenance Using Machine
Learning)

1. What is predictive maintenance, and how does it differ from reactive or preventive
maintenance?
2. Why is it important to preprocess data before training a machine learning model?

3. What role do features play in predictive maintenance models, and how can you
identify useful features?

6. What is the purpose of splitting the dataset into training and testing subsets?
What are the advantages of using MATLAB's Classification Learner App for this
task?
7. What are some common evaluation metrics for classification models, and how do they
apply to predictive maintenance?

7. How does hyperparameter tuning improve model performance?

8. What is the impact of imbalanced datasets in predictive maintenance, and how can it
be addressed?

9. Why might one consider a regression approach (e.g., Remaining Useful Life) instead
of classification for predictive maintenance?

10. What challenges might you face in deploying a predictive maintenance model in a
real-world environment?

You might also like