Trying to input values and determine the class of the value

1 view (last 30 days)
Nathaniel Porter
Nathaniel Porter on 14 Mar 2022
Answered: yanqi liu on 15 Mar 2022
How can I input a set of glucose values for example and let my network return the class (healthy or diabetic)
clc; clear all; close all;
load Projectdata.mat
% Split Glucose Data for training
GR_output=categorical(GR_output);
%Transposing the data
GlucoseReadings_T = GlucoseReadings';
%Renamin the transposed data for training
GlucoseReadings_train = GlucoseReadings_T;
%Segmenting the data for training
train_GlucoseReadings = GlucoseReadings_train(1:84,:);
%Output for segmented data
train_GR_output = GR_output(1:84);
%Split Insulin Data for training
INS_output=categorical(INS_output);
InsulinReadings_T = InsulinReadings';
InsulinReadings_train = InsulinReadings_T;
train_InsulinReadings = InsulinReadings_train(1:84,:);
train_INS_output = INS_output(1:84);
% Split glucose data for validation
GlucoseReadingsTrain=(reshape(train_GlucoseReadings', [1749,84]));
val_GlucoseReadings = GlucoseReadings_train(85:102,:);
val_GR_output = GR_output(85:102);
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1749,18]));
%Split glucose data for testing
test_GlucoseReadings =GlucoseReadings_train(103:120,:);
test_GR_output = GR_output(103:120);
GlucoseReadingsTest=(reshape(test_GlucoseReadings', [1749,18]));
numFeatures = size(GlucoseReadings_T,2);
%Splitting Insulin data for validation
InsulinReadingsTrain=(reshape(train_InsulinReadings', [1758,84]));
val_InsulinReadings = InsulinReadings_train(85:102,:);
val_INS_output = INS_output(85:102);
InsulinReadingsVal=(reshape(val_InsulinReadings', [1758,18]));
%Split Insulin data for testing
test_InsulinReadings = InsulinReadings_train(103:120,:);
test_INS_output = INS_output(103:120);
InsulinReadingsTest=(reshape(test_InsulinReadings', [1758,18]));
numFeatures1 = size(InsulinReadings_T,2);
% number of hidden units represent the size of the data
numHiddenUnits = 120;
%number of classes represents the different variations(normal and diabetic)
numClasses = length(categories(categorical(GR_output)));
numClasses1 = length(categories(categorical(INS_output)));
layers = [ ...
sequenceInputLayer(numFeatures)
dropoutLayer(0.5)
%instanceNormalizationLayer
bilstmLayer(round(numHiddenUnits/2),'OutputMode','sequence')
fullyConnectedLayer(numClasses)
%instanceNormalizationLayer
dropoutLayer(0.5)
softmaxLayer
classificationLayer];
layers1 = [ ...
sequenceInputLayer(numFeatures1)
dropoutLayer(0.5)
%instanceNormalizationLayer
bilstmLayer(round(numHiddenUnits/2),'OutputMode','sequence')
fullyConnectedLayer(numClasses)
%instanceNormalizationLayer
dropoutLayer(0.5)
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',200, ...
'GradientThreshold',1, ...
'Verbose',false, ...
'ValidationData',{GlucoseReadingsVal, val_GR_output},...
'LearnRateDropFactor',0.4,...
'LearnRateDropPeriod',5,...
'Plots','training-progress');
options1 = trainingOptions('adam', ...
'MaxEpochs',200, ...
'GradientThreshold',1, ...
'Verbose',false, ...
'ValidationData',{InsulinReadingsVal, val_INS_output},...
'LearnRateDropFactor',0.4 ,...
'LearnRateDropPeriod',5,...
'Plots','training-progress');
% Train
% whos
net = trainNetwork(GlucoseReadingsTrain,train_GR_output,layers,options);
net1 = trainNetwork(InsulinReadingsTrain,train_INS_output,layers1,options1);
% Test
miniBatchSize = 27;
GR_outputPred = classify(net,GlucoseReadingsTest,...
'MiniBatchSize',miniBatchSize,...
'ExecutionEnvironment','cpu');
acc = mean(GR_outputPred(:) == categorical(test_GR_output(:)))
INS_outputPred = classify(net1,InsulinReadingsTest,...
'MiniBatchSize',miniBatchSize,...
'ExecutionEnvironment','cpu');
acc1 = mean(INS_outputPred(:) == categorical(test_INS_output(:)))
figure
t = confusionchart(categorical(test_GR_output(:)),GR_outputPred(:));
figure
t1 = confusionchart(categorical(test_INS_output(:)),INS_outputPred(:));

Answers (1)

yanqi liu
yanqi liu on 15 Mar 2022
clc; clear all; close all;
load Projectdata.mat
% make class to healthy or diabetic
for i = 1 : length(GR_output)
if ~isempty(strfind(lower(GR_output(i)), 'no'))
GR_output(i) = 'healthy';
else
GR_output(i) = 'diabetic';
end
end
for i = 1 : length(INS_output)
if ~isempty(strfind(lower(INS_output(i)), 'no'))
INS_output(i) = 'healthy';
else
INS_output(i) = 'diabetic';
end
end
% Split Glucose Data for training
GR_output=categorical(GR_output);
%Transposing the data
GlucoseReadings_T = GlucoseReadings';
%Renamin the transposed data for training
GlucoseReadings_train = GlucoseReadings_T;
%Segmenting the data for training
train_GlucoseReadings = GlucoseReadings_train(1:84,:);
%Output for segmented data
train_GR_output = GR_output(1:84);
%Split Insulin Data for training
INS_output=categorical(INS_output);
InsulinReadings_T = InsulinReadings';
InsulinReadings_train = InsulinReadings_T;
train_InsulinReadings = InsulinReadings_train(1:84,:);
train_INS_output = INS_output(1:84);
% Split glucose data for validation
GlucoseReadingsTrain=(reshape(train_GlucoseReadings', [1749,84]));
val_GlucoseReadings = GlucoseReadings_train(85:102,:);
val_GR_output = GR_output(85:102);
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1749,18]));
%Split glucose data for testing
test_GlucoseReadings =GlucoseReadings_train(103:120,:);
test_GR_output = GR_output(103:120);
GlucoseReadingsTest=(reshape(test_GlucoseReadings', [1749,18]));
numFeatures = size(GlucoseReadings_T,2);
%Splitting Insulin data for validation
InsulinReadingsTrain=(reshape(train_InsulinReadings', [1758,84]));
val_InsulinReadings = InsulinReadings_train(85:102,:);
val_INS_output = INS_output(85:102);
InsulinReadingsVal=(reshape(val_InsulinReadings', [1758,18]));
%Split Insulin data for testing
test_InsulinReadings = InsulinReadings_train(103:120,:);
test_INS_output = INS_output(103:120);
InsulinReadingsTest=(reshape(test_InsulinReadings', [1758,18]));
numFeatures1 = size(InsulinReadings_T,2);
% number of hidden units represent the size of the data
numHiddenUnits = 120;
%number of classes represents the different variations(normal and diabetic)
numClasses = length(categories(categorical(GR_output)));
numClasses1 = length(categories(categorical(INS_output)));
layers = [ ...
sequenceInputLayer(numFeatures)
dropoutLayer(0.5)
%instanceNormalizationLayer
bilstmLayer(round(numHiddenUnits/2),'OutputMode','sequence')
fullyConnectedLayer(numClasses)
%instanceNormalizationLayer
dropoutLayer(0.5)
softmaxLayer
classificationLayer];
layers1 = [ ...
sequenceInputLayer(numFeatures1)
dropoutLayer(0.5)
%instanceNormalizationLayer
bilstmLayer(round(numHiddenUnits/2),'OutputMode','sequence')
fullyConnectedLayer(numClasses)
%instanceNormalizationLayer
dropoutLayer(0.5)
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',200, ...
'GradientThreshold',1, ...
'Verbose',false, ...
'ValidationData',{GlucoseReadingsVal, val_GR_output},...
'LearnRateDropFactor',0.4,...
'LearnRateDropPeriod',5,...
'Plots','training-progress');
options1 = trainingOptions('adam', ...
'MaxEpochs',200, ...
'GradientThreshold',1, ...
'Verbose',false, ...
'ValidationData',{InsulinReadingsVal, val_INS_output},...
'LearnRateDropFactor',0.4 ,...
'LearnRateDropPeriod',5,...
'Plots','training-progress');
% Train
% whos
net = trainNetwork(GlucoseReadingsTrain,train_GR_output,layers,options);
net1 = trainNetwork(InsulinReadingsTrain,train_INS_output,layers1,options1);
% Test
miniBatchSize = 27;
GR_outputPred = classify(net,GlucoseReadingsTest,...
'MiniBatchSize',miniBatchSize,...
'ExecutionEnvironment','cpu');
acc = mean(GR_outputPred(:) == categorical(test_GR_output(:)))
INS_outputPred = classify(net1,InsulinReadingsTest,...
'MiniBatchSize',miniBatchSize,...
'ExecutionEnvironment','cpu');
acc1 = mean(INS_outputPred(:) == categorical(test_INS_output(:)))
figure
t = confusionchart(categorical(test_GR_output(:)),GR_outputPred(:));
figure
t1 = confusionchart(categorical(test_INS_output(:)),INS_outputPred(:));
% test demo
GR_outputPred_demo = classify(net,GlucoseReadingsTest(:,1),...
'ExecutionEnvironment','cpu')
INS_outputPred_demo = classify(net1,InsulinReadingsTest(:,10),...
'ExecutionEnvironment','cpu')
GR_outputPred_demo =
categorical
healthy
INS_outputPred_demo =
categorical
diabetic

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by