Fitrnet having assignment size errors

5 views (last 30 days)
Will
Will on 10 Mar 2025
Answered: Gayathri on 14 Mar 2025
I am trying to create a neural network using fitrnet and a dataset that is 1000x24. I am trying to use cvpartition to split the dataset into 80% training and 20% test. However, when doing so, I regularly get an error in the "RegressionChainEnsemble.m" file on line 140 saying "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side." This happens every time when using 80% on cvpartition but does not when I lower the percent down to around 50%. Does anyone know how to help with this?
Follow on question: when I have trained a model with fitrnet, how do I then pass in predictor variable values? For example, I would like to pass in a set of input data to get the 14 response variable values. Thanks!

Accepted Answer

Gayathri
Gayathri on 14 Mar 2025
Hi @Will,
I am not able to reproduce the issue at my end. But please refer to the sample code for creating a neural network using "fitrnet" function and with a dataset of size "1000x24". This code might help you resolve the issue faced.
% Generate a synthetic dataset
rng(0); % For reproducibility
numObservations = 1000;
numFeatures = 24;
% Create random data
X = rand(numObservations, numFeatures);
y = rand(numObservations, 1); % Random target variable
% Create a cvpartition object for an 80/20 split
cv = cvpartition(numObservations, 'HoldOut', 0.2);
% Get training and test indices
trainIdx = training(cv);
testIdx = test(cv);
% Split the data into training and test sets
XTrain = X(trainIdx, :);
yTrain = y(trainIdx);
XTest = X(testIdx, :);
yTest = y(testIdx);
% Set up and train a neural network regression model
try
% Define the neural network
model = fitrnet(XTrain, yTrain, 'LayerSizes', [10, 10], 'Activations', 'relu');
% Predict on the test set
yPred = predict(model, XTest);
% Calculate and display the RMSE
rmse = sqrt(mean((yTest - yPred).^2));
fprintf('RMSE: %.4f\n', rmse);
catch ME
% Display the error message if one occurs
fprintf('Error: %s\n', ME.message);
end
RMSE: 0.4445
For more information on the "cvpartition" function, please refer to the below link.
If this does not solve your issue, please reply back with relevant files so that I can get to know the issue encountered in detail.
Thanks!

More Answers (0)

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!