Error using classreg.l​earning.Li​near.prepa​reDataCR X and Y do not have the same number of observations.

2 vues (au cours des 30 derniers jours)
I have faced this issue when I was implement a face recognition model using PLS method
Error using classreg.learning.Linear.prepareDataCR
X and Y do not have the same number of observations.
Error in RegressionLinear.prepareData (line 629)
[X,Y,W,dataSummary] = classreg.learning.Linear.prepareDataCR(...
Error in classreg.learning.FitTemplate/fit (line 246)
this.PrepareData(X,Y,this.BaseFitObjectArgs{:});
Error in RegressionLinear.fit (line 488)
[varargout{1:nargout}] = fit(temp,X,Y);
Error in fitrlinear (line 147)
[varargout{1:nargout}] = RegressionLinear.fit(X,y,RemainingArgs{:});
Error in FRM_PLS (line 39)
mdl = fitrlinear(trainFeaturesPLS, trainingLabels, 'Learner', 'leastsquares', 'Lambda', lambda);
although this is my original code
% PLS Method
% Load a dataset of grayscale face images
Dataset = imageDatastore('ExtendedYaleB', 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
% Split the data into training and testing sets
[trainImgs, testImgs] = splitEachLabel(Dataset, 0.7, 'randomized');
% Extract local patches from the training images using the extractLBPFeatures function
numNeighbors = 8;
radius = 1;
trainFeatures = cell(numel(trainImgs.Files), 1);
for i = 1:numel(trainImgs.Files)
img = readimage(trainImgs, i);
trainFeatures{i} = extractLBPFeatures(img, 'NumNeighbors', numNeighbors, 'Radius', radius);
end
%% Perform PLS regression
numComponents = 59; % Number of PLS components to retain
trainingLabels = double(trainImgs.Labels);
trainFeaturesMat = cell2mat(trainFeatures);
[~,~,~,~,betaPLS,~] = plsregress(trainFeaturesMat, trainingLabels, numComponents);
%% Project train features onto PLS subspace
trainFeaturesPLS = cell(numel(trainImgs.Files), 1);
for i = 1:size(trainFeaturesMat, 2)
features = trainFeaturesMat(:, i);
trainFeaturesPLS{i} = features .* betaPLS';
end
% Convert train features to a consistent data type
trainFeaturesPLS = cellfun(@double, trainFeaturesPLS, 'UniformOutput', false);
% Convert train features to matrix
trainFeaturesPLS = cell2mat(trainFeaturesPLS);
%% Train the linear regression model on the modified LBP features
lambda = 0.1; % Set the value of lambda
mdl = fitrlinear(trainFeaturesPLS, trainingLabels, 'Learner', 'leastsquares', 'Lambda', lambda);
% Save the model to a file
save('FRM_PLS.mat', 'mdl', 'betaPLS');
% Extract local patches from the testing images and make predictions using the predict function
testFeatures = cell(numel(testImgs.Files), 1);
for i = 1:numel(testImgs.Files)
img = readimage(testImgs, i);
testFeatures{i} = extractLBPFeatures(img, 'NumNeighbors', numNeighbors, 'Radius', radius);
end
% Project test features onto PLS subspace
testFeaturesPLS = cell(numel(testImgs.Files), 1);
for i = 1:numel(testImgs.Files)
features = cell2mat(testFeatures(i));
testFeaturesPLS{i} = features' * betaPLS;
end
% Convert test features to matrix
testFeaturesPLS = cell2mat(testFeaturesPLS);
% Make predictions using the loaded model
load('my_Model.mat', 'mdl');
predictions = predict(mdl, testFeaturesPLS);
% Evaluate the performance of the model using the confusionmat and classificationReport functions
confMat = confusionmat(testImgs.Labels, predictions);
classificationReport = classificationReport(testImgs.Labels, predictions);
% Use the loaded model for prediction
testImg = imread('test_image.jpg');
testFeatures = extractLBPFeatures(testImg, 'NumNeighbors', numNeighbors, 'Radius', radius);
testFeaturesPLS = testFeatures' * betaPLS;
prediction = predict(mdl, testFeaturesPLS);
I want help
and this s the dataset if any one will run code
  2 commentaires
Walter Roberson
Walter Roberson le 11 Août 2023
A common source of this kind of problem is if either the X or the Y needs to be transposed. More often the X.

Connectez-vous pour commenter.

Réponses (1)

Drew
Drew le 1 Sep 2023
The error message "X and Y do not have the same number of observations" indicates that, on line 39, which is
mdl = fitrlinear(trainFeaturesPLS, trainingLabels, 'Learner', 'leastsquares', 'Lambda', lambda),
trainFeaturesPLS and trainingLabels have a different number of rows. The observations are stored in rows, so those two variables need to have the same number of rows in order for the fitrlinear training to proceed.

Community Treasure Hunt

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

Start Hunting!

Translated by