How to find the final formula or equation that is produced by regression learner.
12 次查看(过去 30 天)
显示 更早的评论
I have created a trainedModel for my data by using Train Regression Models in Regression Learner App. After I exported my model to workspace, I'm looking for a way to write my equation on a paper as y = variable1......Variable 16. The reason I need this is to see how these variables are correlated. Is there any way to study this? I see the trainedModel structure but I can’t see the equation in terms of formal. Any idea please?
I have this:
yfityfit = trainedModel.predictFcn(T)
%% but I wanna see the equation inside the trainedModel.predictFcn
rainedModel
trainedModel =
struct with fields:
predictFcn: @(x)exportableModel.predictFcn(predictorExtractionFcn(x))
RegressionTree: [1×1 classreg.learning.regr.CompactRegressionTree]
About: 'This struct is a trained model exported from Regression Learner R2019a.'
HowToPredict: 'To make predictions on a new predictor column matrix, X, use: ↵ yfit = c.predictFcn(X) ↵replacing 'c' with the name of the variable that is this struct, e.g. 'trainedModel'. ↵ ↵X must contain exactly 16 columns because this model was trained using 16 predictors. ↵X must contain only predictor columns in exactly the same order and format as your training ↵data. Do not include the response column or any columns you did not import into the app. ↵ ↵For more information, see How to predict using an exported model.'
2 个评论
Samuel Ferreira
2022-4-23
Hey, I'm have the same problem. I want get the equation of the regression learning after training. Some answer for it?
回答(1 个)
Ayush Aniket
2025-6-11
You can extract the equation from your trained model by accessing its coefficients and predictor names. Since trainedModel.predictFcn is a function handle, it doesn't directly expose the equation. However, if your model is a linear regression model, you can retrieve the coefficients as show below:
% Generate synthetic data
rng(42); % Set random seed for reproducibility
X = randn(100, 3); % 100 samples, 3 predictors
Y = 2*X(:,1) - 3*X(:,2) + 1.5*X(:,3) + randn(100,1)*0.5; % Linear relationship with noise
% Convert to table format for Regression Learner App
dataTable = array2table([X Y], 'VariableNames', {'Var1', 'Var2', 'Var3', 'Response'});
I am using fitlm function for demo purpose, you can replace this step by exporting the trained model from the Regression Learner App.
% Train a linear regression model using fitlm
trainedModel = fitlm(dataTable, 'Response ~ Var1 + Var2 + Var3');
coefficients = trainedModel.Coefficients.Estimate;
predictorNames = trainedModel.PredictorNames;
% Construct the regression equation
equation = "y = " + string(coefficients(1)); % Intercept
for i = 2:length(coefficients)
equation = equation + " + " + string(coefficients(i)) + "*" + predictorNames{i-1};
end
disp('Extracted Regression Equation:');
disp(equation);
For more details, check out the following MATLAB answer on extracting equations from Regression Learner models - https://www.mathworks.com/matlabcentral/answers/696135-how-to-view-the-equation-of-a-linear-regression-model-automatically-generated-with-regression-learn
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Classification Learner App 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!