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

9 次查看(过去 30 天)
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);

回答(2 个)

Chunru
Chunru 2021-12-6
编辑:Chunru 2021-12-6
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 个评论
Chunru
Chunru 2021-12-6
For approximately equal partition:
hpartition = cvpartition(species, 'holdout', 0.4);

请先登录,再进行评论。


yanqi liu
yanqi liu 2021-12-7
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

类别

Help CenterFile Exchange 中查找有关 Classification 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by