How to Split fisher iris data into 60% training and 40% Testing

18 views (last 30 days)
Hello I hope you are doing well.
I want to split the fisher iris dataset betwee 60% training and 40% testing Dataset How can i divide that?
i am using this Example
It used all training examples not test example i want to divide it betwee train and test
load fisheriris
f = figure;
gscatter(meas(:,1), meas(:,2), species,'rgb','osd');
xlabel('Sepal length');
ylabel('Sepal width');
N = size(meas,1);
lda = fitcdiscr(meas(:,1:2),species);
ldaClass = resubPredict(lda);

Answers (2)

Chunru
Chunru on 6 Dec 2021
Edited: Chunru on 6 Dec 2021
load fisheriris
n = size(meas, 1);
%hpartition = cvpartition(n, 'holdout', 0.4); % 40% for test
hpartition = cvpartition(species, 'holdout', 0.4); % 40% for test
idxTrain = training(hpartition);
idxTest = test(hpartition);
pie(categorical(species(idxTrain))); % distribution of training samples
XTrain = meas(idxTrain, :);
TTrain = species(idxTrain);
XTest = meas(idxTest, :);
TTest = species(idxTest);
% Training
lda = fitcdiscr(XTrain(:,1:2), TTrain);
% Prediction
testClass = predict(lda, XTest(:, 1:2));
  4 Comments
Chunru
Chunru on 6 Dec 2021
For approximately equal partition:
hpartition = cvpartition(species, 'holdout', 0.4);

Sign in to comment.


yanqi liu
yanqi liu on 7 Dec 2021
yes,sir,may be use the follow split method ,such as
close all;
clear all;
clc;
load fisheriris
cs = categorical(species);
ds = categories(cs);
training_x = [];training_y = [];
testing_x = [];testing_y = [];
for i = 1 : length(ds)
ind = find(cs == ds{i});
% rand suffer
ind = ind(randperm(length(ind)));
% 60% training and 40% testing
training_x = [training_x; meas(ind(1:round(length(ind)*0.6)),:)];
training_y = [training_y; cs(ind(1:round(length(ind)*0.6)),:)];
testing_x = [testing_x; meas(ind(1+round(length(ind)*0.6):end),:)];
testing_y = [testing_y; cs(ind(1+round(length(ind)*0.6):end),:)];
end

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!