genetic algorithm to optimize the variable of linear regression(a,b1,b2)
31 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Abdolkarim Mohammadi
am 20 Jul. 2020
Bearbeitet: Abdolkarim Mohammadi
am 21 Jul. 2020
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 Kommentar
John Juston
am 14 Jun. 2021
Very grateful for this example, a gateway to solving more complex nonlinear functions
Alan Weiss
am 20 Jul. 2020
Bearbeitet: Alan Weiss
am 20 Jul. 2020
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 Kommentare
Siehe auch
Kategorien
Mehr zu Genetic Algorithm finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!