%% Classify Feature Data Using Trained Network
% Load the pretrained network TransmissionCasingNet. This network classifies the gear tooth condition of a transmission system given a mixture of numeric sensor readings, statistics, and categorical inputs.

load TransmissionCasingNet.mat

% View the network architecture.

net.Layers

% Read the transmission casing data from the CSV file "transmissionCasingData.csv".

filename = "transmissionCasingData.csv";
tbl = readtable(filename,'TextType','String');

% Convert the labels for prediction to categorical using the convertvars function.

labelName = "GearToothCondition";
tbl = convertvars(tbl,labelName,'categorical');

% To make predictions using categorical features, you must first convert the categorical features to numeric. First, convert the categorical predictors to categorical using the convertvars function by specifying a string array containing the names of all the categorical input variables. In this data set, there are two categorical features with names "SensorCondition" and "ShaftCondition".

categoricalInputNames = ["SensorCondition" "ShaftCondition"];
tbl = convertvars(tbl,categoricalInputNames,'categorical');

% Loop over the categorical input variables. For each variable:
% Convert the categorical values to one-hot encoded vectors using the onehotencode function.
% Add the one-hot vectors to the table using the addvars function. Specify to insert the vectors after the column containing the corresponding categorical data.
% Remove the corresponding column containing the categorical data.

for i = 1:numel(categoricalInputNames)
    name = categoricalInputNames(i);
    oh = onehotencode(tbl(:,name));
    tbl = addvars(tbl,oh,'After',name);
    tbl(:,name) = [];
end

% Split the vectors into separate columns using the splitvars function.

tbl = splitvars(tbl);

% View the first few rows of the table.

head(tbl)

% Predict the labels of the test data using the trained network and calculate the accuracy. Specify the same mini-batch size used for training.

YPred = classify(net,tbl(:,1:end-1));

% Calculate the classification accuracy. The accuracy is the proportion of the labels that the network predicts correctly.

YTest = tbl{:,labelName};
accuracy = sum(YPred == YTest)/numel(YTest)

% Copyright 2020 The MathWorks, Inc.