Error using classreg.learning.internal.numPredictorsCheck X data must have 3540 column(s). Error in RegressionLinear/predict (line 141) classreg.learning.internal
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!