1

Hi so i'm writing some code for class and this for Linear Regression.
The values calculated by hand is a=1.7 and b=1.6 for the data you can see in the code.

I've tried separating different parts of the formula into different variables but the answer remains the same (1.6999999999999993).

import numpy as np  
x=np.array([2,3,5,6])  
y=np.array([4.5,7.2,9.2,11.5])  
b=(np.sum((y-np.mean(y))*x))/(np.sum((x-np.mean(x))*x))  
a=np.mean(y)-(b*(np.mean(x)))  
print(a)   
print(b)  

The expected result is a=1.7 and b=1.6, but the output is a=1.6999999999999993.

codehopper
  • 21
  • 4

2 Answers2

0

It's because you are using float number, binary floating point math is like this. In most programming languages, it is based on the IEEE 754 standard.

see Is floating point math broken?

6pi
  • 387
  • 2
  • 11
0

It is based on the IEEE 754 standard.

But if you want your result in the form of 'a=1.7'. Then you can try this..

import numpy as np  
x=np.array([2,3,5,6])  
y=np.array([4.5,7.2,9.2,11.5])  
b=(np.sum((y-np.mean(y))*x))/(np.sum((x-np.mean(x))*x))  
a=np.mean(y)-(b*(np.mean(x)))  
a = "%.1f"% (a)
print(a)   
print(b)  
Urvi Soni
  • 314
  • 1
  • 2
  • 12