feature classification tasks, responses must be categorical
Show older comments
I am doing a bearing fault classification project. I have segmented the vibration signals into small portion with a window. Did one hot encoding. When i try to train neural network it says feature classification tasks, responses must be categorical. this is the portion from the project
Combined_data = readtable('Data_file.csv');
win_len = 1000;
stride = 200;
X_Segmented = [];
Y_Segmented = {};
unique_faults = unique(Combined_data.Fault); % Uncomment this line if you want to use unique_faults
for i = 1:numel(unique_faults) % Loop over unique_faults
desired_fault = unique_faults{i}; % Get the desired fault label
df_temp_2 = Combined_data(strcmp(Combined_data.Fault, desired_fault), :);
for j = 1:stride:(height(df_temp_2) - win_len)
temp = df_temp_2{j:j + win_len - 1, 1};
temp = temp(:)';
X_Segmented = [X_Segmented; temp];
Y_Segmented = [Y_Segmented; {desired_fault}]; % Store the desired fault label
end
end
encoded_Y = grp2idx(Y_Segmented);
encoded_Y_row = encoded_Y';
OHE_Y = full(ind2vec(encoded_Y_row ))';
size(X_Segmented)
disp OHE_Y
%%%%%%%%%%%%%%%%%%%%%%test train validation split
% Extract the data and labels
%X = Combined_data{:, 1:end-1}; % Features (data)
%Y = Combined_data.Fault; % Labels
% Convert labels to categorical
Y = categorical(Y_Segmented);
% Specify the proportion for the test set
testProportion = 0.3;
% Split the data into training and testing sets
cv = cvpartition(Y, 'HoldOut', testProportion);
idxTrain = training(cv);
idxTest = test(cv);
X_train = X_Segmented(idxTrain, :);
X_test = X_Segmented(idxTest, :);
y_train = OHE_Y(idxTrain, :); % Use OHE_Y for train and test
y_test = OHE_Y(idxTest, :);
%%% neural network modelling
numFeatures = size(X_train, 2);
numClasses = 14;
layers = [
featureInputLayer(numFeatures, 'Normalization', 'zscore')
fullyConnectedLayer(1024)
reluLayer
fullyConnectedLayer(512)
reluLayer
fullyConnectedLayer(256)
reluLayer
fullyConnectedLayer(128)
reluLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer
];
% Train options
options = trainingOptions('adam', ...
'MaxEpochs', 50, ...
'MiniBatchSize', 500, ...
'Shuffle', 'every-epoch', ...
'ValidationData', {X_test, y_test}, ...
'ValidationFrequency', 10, ...
'Verbose', true, ...
'Plots', 'training-progress', ...
'ExecutionEnvironment', 'auto'); % or 'gpu' if available
% Train the network
net = trainNetwork(X_train,y_train, layers, options);
The size of the X_Segmented is 15048*1000 , X_train 10534*1000 double , y_train 10534*14 double. I have 14 labels . Y_train is a subset of OHE_Y , so it should be already in a categorical form, if I am right. When I do small corrrections, like changing the response in trainNetwork, and then it says responses must be a vector of categorical responses. What might the issue actually?? Why is OHE_Y not a categorical form?
4 Comments
Image Analyst
on 28 Aug 2023
Too bad you forgot to attach 'Data_file.csv'. I'll check back later for it.
What does this show in the command window
whos OHE_Y
class(OHE_Y)
Don't use semicolons.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Hrishikesh Kesavan Nair
on 28 Aug 2023
Edited: Hrishikesh Kesavan Nair
on 28 Aug 2023
Image Analyst
on 29 Aug 2023
Too bad you forgot to attach 'Data_file.csv' AGAIN! We'll check back later for it.
Looks like it's double when it wants categorical. We'll check the code later you've uploaded the data and we can actually run it.
Hrishikesh Kesavan Nair
on 29 Aug 2023
Accepted Answer
More Answers (0)
Categories
Find more on Parallel and Cloud in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!