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

9 ビュー (過去 30 日間)
hammad younas
hammad younas 2021 年 12 月 6 日
回答済み: yanqi liu 2021 年 12 月 7 日
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 件のコメント
hammad younas
hammad younas 2021 年 12 月 6 日
@Chunru it does not divide the dataset equaly like 20 for each class
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 Center および File ExchangeClassification についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by