Classifying data using machine learning

조회 수: 1 (최근 30일)
Dhruv Birla
Dhruv Birla 2021년 7월 14일
댓글: Dhruv Birla 2021년 7월 21일
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);

채택된 답변

Hrishikesh Borate
Hrishikesh Borate 2021년 7월 21일
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);
  댓글 수: 1
Dhruv Birla
Dhruv Birla 2021년 7월 21일
This works perfectly! Thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by