How to import csv deep learning dataset with labels to matlab?
8 views (last 30 days)
Show older comments
Hello everyone,
I am trying to train a fully connected deep learning model.
I have my data set in a csv file so that each row represents a different signal.
The first 56 coloums represents the signal and the 2 last coloums represents the labels for the signals (there are two labels).
How can I import the cvs file in a way that i will be able to train a deep learning network with it?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/915359/image.png)
0 Comments
Accepted Answer
yanqi liu
on 7 Mar 2022
yes,sir,may be read csv and reshape the data(:,1:56) into 4-D as train_input,data(:, 57:58) make to label vector as train_output
if possible,may be upload your csv to analysis
3 Comments
yanqi liu
on 10 Mar 2022
yes,sir,now we can use
data = load('Train Data.csv');
% make X and Y
X = data(:, 10 : 65);
Y = data(:, 66 : 67);
[~, Y] = max(Y');
X = X';
Y = Y';
% make cnn
num_class = length(unique(Y));
% make data shuffle
rand('seed', 0)
ind = randperm(size(X, 2));
X = X(:,ind);
Y = Y(ind);
Y = categorical(Y);
% Split Data
rate = 0.8;
ind_split = round(length(Y)*rate);
train_X = X(:,1:ind_split);
train_Y = Y(1:ind_split);
val_X = X(:,ind_split+1:end);
val_Y = Y(ind_split+1:end);
% Data Batch
XTrain=(reshape(train_X, [size(X,1),1,1,size(train_X,2)]));
XVal=(reshape(val_X', [size(X,1),1,1,size(val_X,2)]));
% CNN
layers = [imageInputLayer([size(X,1) 1 1])
convolution2dLayer([30 1],3,'Stride',1)
dropoutLayer
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2,'Padding',[0 0 0 1])
dropoutLayer
fullyConnectedLayer(num_class)
softmaxLayer
classificationLayer];
% Specify training options.
opts = trainingOptions('adam', ...
'MaxEpochs',200, ...
'Shuffle','every-epoch', ...
'Plots','training-progress', ...
'Verbose',false, ...
'ValidationData',{XVal,val_Y},...
'ExecutionEnvironment', 'cpu', ...
'ValidationPatience',Inf);
% Train
yc = categorical(train_Y);
net1 = trainNetwork(XTrain,yc,layers,opts);
% Test
miniBatchSize = 27;
YPred = classify(net1,XVal, ...
'MiniBatchSize',miniBatchSize,...
'ExecutionEnvironment', 'cpu');
acc = mean(YPred(:) == val_Y(:))
figure
t = confusionchart(val_Y(:),YPred(:));
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/921354/image.png)
More Answers (0)
See Also
Categories
Find more on Deep Learning Toolbox 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!