genetic algorithm to optimize the variable of linear regression(a,b1,b2)
조회 수: 27 (최근 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!