-1

I'm working on linear regression algorithm with multiple variables using Numpy library for Matrix. My problem is that matrix.item((i,j)) is not working properly.here is python shell:

>>> a=h(Data,0,Theta)
>>> a
matrix([[3.78]])
>>> a.item((0,0))
3.7800000000000002

As you see the output value is 0.0000000000000002 bigger than the real answer.

  • Even computers does not work properly! – honzakuzel1989 Sep 03 '19 at 13:51
  • That's a display thing, not a bug. The first output just truncates more aggressively for display. – user2357112 Sep 03 '19 at 13:52
  • I think with a more recent NumPy version, the same rounding behavior would have been applied both times. – user2357112 Sep 03 '19 at 13:53
  • @user2357112 i calculated it manually and the correct answer is exactly 3.78 – Amirparsa Sal Sep 03 '19 at 13:54
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Ralf Sep 03 '19 at 13:56
  • 3.78 isn't representable in binary. You're going to have to get used to floating point rounding behavior if you want to do any sort of numerical work on a computer. (Even if you switch to decimal, and completely wreck your performance in the process, you'll just end up with most of the same problems, but in decimal.) – user2357112 Sep 03 '19 at 13:57
  • @AmirparsaSal maybe read [this answer](https://stackoverflow.com/a/25420021/9225671) on limitations of `float` data type or this: https://docs.python.org/3.7/tutorial/floatingpoint.html – Ralf Sep 03 '19 at 13:58

1 Answers1

0
  • This is not a bug, it's the default behavior.

  • While displaying x it truncates the value.

Please check the below example.

>>> x = np.matrix(((3.7800000000000002)))

>>> x
matrix([[3.78]])

>>> x.item(0,0)
3.7800000000000002
Karthikeyan KR
  • 1,134
  • 1
  • 17
  • 38