Classifying data using machine learning

1 visualizzazione (ultimi 30 giorni)
Using the fisheriris dataset in MATLAB, I want to use the first 30 datasets of each species for training and then predict the species of the other 20 based on the training data. I tried using the predict function, but it requires the training data vector and the prediction data vector to have the same dimensions. Is there a different function I can use that works the same way as the predict function and allows me to input vectors of varying sizes for training and prediction?
Here is the code I used:
N = size(meas,1);
newLabels = cell(90,1);
newLabels(1:30,1) = species(1:30,1);
newLabels(31:60,1) = species(51:80,1);
newLabels(61:90,1) = species(101:130,1);
trainData = cell(90,2);
trainData = str2double(trainData);
trainData(1:30,1) = meas(1:30,1);
trainData(31:60,1) = meas(51:80,1);
trainData(61:90,1) = meas(101:130,1);
trainData(1:30,2) = meas(1:30,2);
trainData(31:60,2) = meas(51:80,2);
trainData(61:90,2) = meas(101:130,2);
toPredict = cell(90,2);
toPredict = str2double(toPredict);
toPredict(1:30,1) = meas(21:50,1);
toPredict(31:60,1) = meas(71:100,1);
toPredict(61:90,1) = meas(121:150,1);
toPredict(1:30,2) = meas(21:50,2);
toPredict(31:60,2) = meas(71:100,2);
toPredict(61:90,2) = meas(121:150,2);
lda = fitcdiscr(trainData(:,1:2),newLabels);
ldaClass = predict(lda,toPredict);
ldaResubErr = resubLoss(lda);
figure
ldaResubCM = confusionchart(newLabels,ldaClass);

Risposta accettata

Hrishikesh Borate
Hrishikesh Borate il 21 Lug 2021
Hi,
The following code uses the fisheriris dataset, where the first 30 instances of each class are used for training and the next 20 instances of each class are used for prediction.
load fisheriris.mat
N = size(meas,1);
newLabels = cell(90,1);
newLabels(1:30,1) = species(1:30,1);
newLabels(31:60,1) = species(51:80,1);
newLabels(61:90,1) = species(101:130,1);
trainData = cell(90,2);
trainData = str2double(trainData);
trainData(1:30,:) = meas(1:30,1:2);
trainData(31:60,:) = meas(51:80,1:2);
trainData(61:90,:) = meas(101:130,1:2);
toPredict = cell(60,2);
toPredict = str2double(toPredict);
toPredict(1:20,:) = meas(31:50,1:2);
toPredict(21:40,:) = meas(81:100,1:2);
toPredict(41:60,:) = meas(131:150,1:2);
toPredictLabels = cell(60,1);
toPredictLabels(1:20,1) = species(31:50,1);
toPredictLabels(21:40,1) = species(81:100,1);
toPredictLabels(41:60,1) = species(131:150,1);
lda = fitcdiscr(trainData(:,1:2),newLabels);
ldaClass = predict(lda,toPredict);
ldaResubErr = resubLoss(lda);
figure
ldaResubCM = confusionchart(toPredictLabels,ldaClass);

Più risposte (0)

Categorie

Scopri di più su Statistics and Machine Learning Toolbox in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by