필터 지우기
필터 지우기

What is wrong with this linear regression code ?

조회 수: 1 (최근 30일)
Hassan Ibrahim
Hassan Ibrahim 2018년 7월 16일
댓글: Hassan Ibrahim 2018년 7월 19일
The problem is in calculating term_0 and term_1, I tried to search for the error but found nothing useful.
syms k;
x = [100, 120, 130, 140, 150]; %area of the house
w = 7000; %cost/meter square
y = w * x; %price of the house
t0 = 0; %initialize theta as zero
t1 = 0; %initialize theta as zero
a = 0.8; %setting learning rate(alpha)
m = 5;
term_0 = (a/m) * symsum(t0 + t1*x(k) - y(k),k, 0, m);
term_1 = (a/m) * symsum((t0 + t1*x(k) - y(k)) * x(k),k, 0, m);
%While loop to get the required theta to minimize cost function
while True
temp0 = t0 - term_0;
temp1 = t1 - term_1;
if temp0 == t0 && temp1 == t1
break;
end
t0 = temp0;
t1 = temp1;
end
%calculating the prediction function and plotting the results
h = t0 + t1*x;
hold on;
plot(x, y,'xb');
plot(x, h, 'r');
hold off;

채택된 답변

Diwakar Ravichandran
Diwakar Ravichandran 2018년 7월 17일
편집: Diwakar Ravichandran 2018년 7월 17일
Hi Hassan, As I tried to reproduce the same error, I noticed that you are trying to use the variable 'k' as a index. This is not possible. As the function symsum is a single variable function. The documentation for symsum is as follows:
Also to obtain the sum of the matrix, you can directly store the value using the sum function of matlab. Its documentation is as follows:
One other thing to note, MATLAB has all of its indices from 1. Not from 0. So I would suggest you to use vectorization for your problem.
Hope this helps,
Cheers!
  댓글 수: 1
Hassan Ibrahim
Hassan Ibrahim 2018년 7월 19일
Thanks for the help. When I tried to use sum function everything worked as expected.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Support Vector Machine Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by