Error using classreg.learning.internal.numPredictorsCheck X data must have 3540 column(s). Error in RegressionLinear/predict (line 141) classreg.learning.internal
8 views (last 30 days)
Show older comments
I faces this Problem
Error using classreg.learning.internal.numPredictorsCheck
X data must have 3540 column(s).
Error in RegressionLinear/predict (line 141)
classreg.learning.internal.numPredictorsCheck(X,...
Error in FRM_PLS (line 60)
predictions = predict(mdl, testFeaturesPLS);
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 = 50; % 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} = double(features' .* betaPLS);
end
%% Train the linear regression model on the modified LBP features
trainFeaturesPLS = cell2mat(trainFeaturesPLS);
lambda = 0.1;
mdl = fitrlinear(trainFeaturesPLS', trainingLabels, 'Learner', 'leastsquares', 'Lambda', lambda);
% Save the model to a file
save('my_Model.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));
features = features';
numFeatures = size(features, 2); % Number of elements in features
testFeaturesPLS{i} = double(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);
0 Comments
Answers (1)
TARUN
on 11 Jun 2025
The error you're facing — X data must have 3540 column(s) — occurs because the test data you pass to the predict function doesn't match the feature dimensions expected by the model trained on the PLS-transformed data.
This typically happens when the PLSprojection is not applied consistently during training and testing.
To correctly apply PLS transformation, ensure you're projecting the test features using the same formula used during training, and avoid element-wise multiplication.
Here's how to fix it:
1. Replace this line in your training phase:
trainFeaturesPLS{i} = double(features' .* betaPLS);
with the correct PLS projection:
trainFeaturesPLS = (trainFeaturesMat - mean(trainFeaturesMat)) * betaPLS(2:end, :);
2. Then in your testing phase, replace:
testFeaturesPLS{i} = double(features' .* betaPLS);
with:
testFeaturesMat = cell2mat(testFeatures);
testFeaturesPLS = (testFeaturesMat - mean(trainFeaturesMat)) * betaPLS(2:end, :);
This ensures your train and test features are projected in the same PLSspace, keeping the column count consistent.
Feel free to go through the official MATLABdocumentation on plsregress for more on interpreting the beta coefficients and projecting data:
PLS Regression:
Linear Prediction:
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!