fitcknn using Mel-frequency cepstral coefficients (MFCCs)

4 views (last 30 days)
Hello everyone,
I'm using a function provided by my professor to use kNN algorith for classification for getting the highest rate recognition with different neighbors, but I'm getting the following errors
Error using classreg.learning.FullClassificationRegressionModel.prepareDataCR (line 234)
X and Y do not have the same number of observations.
Error in classreg.learning.classif.FullClassificationModel.prepareData (line 821)
Error in ClassificationKNN.prepareData (line 926)
Error in classreg.learning.FitTemplate/fit (line 233)
Error in (line 911)
this = fit(temp,X,Y);
Error in fitcknn (line 264)
this =,Y,RemainingArgs{:});
Error in kNN_algorithm_features (line 13)
Mdl = fitcknn(trainSet',trainLabel','NumNeighbors',k(kk));
Using audio data provided from him, i'm able to use it, but when i try to use my own audio, it gives me that error.
We did that for every time features of audio (Spectral Centroid, Spread, Rolloff and MFCCs) but this error appears only with MFCC.
% function [a,b]=kNN_algorithm_features(trainSet, testSet, trainLabel, testLabel)
trainSet = MFCC_trainSet;
testSet = MFCC_testSet;
trainLabel = MFCC_trainSet_label;
testLabel = MFCC_testSet_label;
k=[1 5 10 15 20];
for kk=1:length(k)
disp(['set-up the kNN... number of neighbors: ',mat2str(k(kk))])
Mdl = fitcknn(trainSet',trainLabel','NumNeighbors',k(kk));
% test the kNN
predicted_label = predict(Mdl, testSet');
% measure the performance
correct = 0;
flag = zeros(1,length(predicted_label));
for i=1:length(predicted_label)
if length(testLabel) >= i
if predicted_label(i)==testLabel(i)
flag(i) = 1;
disp('recognition rate:')
rate(kk) = (correct/length(predicted_label))*100
For the .mat file, I cannot attach it but I'm gonna paste here how it looks like
MFCC_testSet 13x216712 double
MFCC_testSet_label 1x216712 double
MFCC_trainSet 13x219236 double
MFCC_trainSet_label 1x216972 double
  • MFCC_trainSet_label and MFCC_testSet_label are just series of 1, for comparing them
  • MFCC_trainSet contains the MFCC values of all songs
  • MFCC_testSet contains the MFCC values of the test songs
What am I doing wrong?

Accepted Answer

Shiva Kalyan Diwakaruni
Shiva Kalyan Diwakaruni on 28 May 2021
The error "X and Y do not have the same number of observations." is occurring because In this case X which is trainSet' is 219236x13 double has 219236 observations where as Y which is trainLabel' which is of size 216972x1 double has 216972 observations hence as number of training observations is not same as number of training Labels Your code is not being executed .Hence make sure both trainSet and trainLabel has same number of rows.
Hope it helps.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by