genetic algorithm to optimize the variable of linear regression(a,b1,b2)
32 次查看(过去 30 天)
显示 更早的评论
I need some codes for optimize my prediction(using linear regresion) in MATLAB. I am new to genetic algorithm so if anyone has a code that can do this that would help me start off will be greatly appreciated.
0 个评论
回答(2 个)
Abdolkarim Mohammadi
2020-7-20
编辑:Abdolkarim Mohammadi
2020-7-21
First of all, you should use regress for multiple linear regression. However, if you want to know whether it is possible to use ga for it, the answer is "yes".
First let's create a dataset for a multiple linear regression analysis in the form of .
rng (0);
OriginalA = 2;
OriginalB1 = 1;
OriginalB2 = -4;
X1 = (0:10);
X2 = (5:12);
[DataX1, DataX2] = meshgrid (X1, X2);
DataY = OriginalA + OriginalB1*DataX1 + OriginalB2*DataX2 + 4*rand(numel(X2),numel(X1));
Define the objective function. Regression models minimize the mean squared error of the estimation (MSE).
function f = MSE (x, DataX1, DataX2, DataY)
a = x(1);
b1 = x(2);
b2 = x(3);
YHat = a + b1*DataX1 + b2*DataX2;
f = mean((DataY-YHat).^2, 'all');
Run the Genetic Algorithm to .
fun = @(x)MSE(x, DataX1, DataX2, DataY); % minimize MSE
nvars = 3;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [-10,-10,-10];
ub = [10,10,10];
nonlcon = [];
options = optimoptions ('ga', 'MaxGenerations', 1e3);
[x,fval,exitflag,output] = ...
ga (fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options);
Investigate the results.
RegressionA = x(1);
RegressionB1 = x(2);
RegressionB2 = x(3);
RegressionY = RegressionA + RegressionB1*DataX1 + RegressionB2*DataX2;
hold on
scatter3 (DataX1(:), DataX2(:), DataY(:), 18, ...
'Marker', 'o', ...
'MarkerEdgeColor', 'none', ...
'MarkerFaceColor', 'k', ...
'DisplayName', 'Original data');
surf (DataX1, DataX2, RegressionY, ...
'FaceAlpha', 0.8, ...
'EdgeColor', 'none', ...
'DisplayName', 'Regression model')
text (-3, 9, -28, ...
sprintf("MSE = %f\na = %f\nb_1=%f\nb_2= %f", fval, RegressionA, RegressionB1, RegressionB2));
hold off
xlabel ('x_1');
xlabel ('x_2');
xlabel ('y');
view ([-120,25]);
grid ('on')
box ('on');
legend ('Location', 'Northwest');
1 个评论
John Juston
2021-6-14
Very grateful for this example, a gateway to solving more complex nonlinear functions
Alan Weiss
2020-7-20
编辑:Alan Weiss
2020-7-20
While you CAN solve a linear regression using ga, it is unwise to do so. Your answers will be faster and more accurate while using less memory by using the MATLAB mldivide function (\). For an example, see Overdetermined Systems.
Alan Weiss
MATLAB mathematical toolbox documentation
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!