Classifying data using machine learning
1 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
采纳的回答
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!