0

I'm trying to find a set of unique floats in numpy array and realized that the content of the array is different when I use the set() after rounding on the numpy array. This is what happens when I enter the script in command prompt (or run .py)

import numpy as np

>>> a = np.array([12.32, 2.32, 1.0])
>>> a
array([12.32, 2.32, 1. ])
>>> b = np.round(a, 1)
>>> b
array([12.3, 2.3, 1.])
>>> c = set(b)
>>> c
set([2.2999999999999998, 12.300000000000001, 1.0])

what in the world is going on? I'm using Python 2.7.10 and numpy 1.9.2

NOTE: The question Python float - str - float weirdness was proposed as a duplicate. However, I don't think this is a str(), repr() issue since I've checked all the variable a, b, and c using both str and repr method and they both give same values.

I've also tried it with np.unique and the issue does not come up, so it may be some issue with set(). Or it could also be some float conversion error to and from numpy array as suggested.

Community
  • 1
  • 1
J. Doenut
  • 11
  • 3
  • I thnk it's different object repr's. See https://stackoverflow.com/questions/18859312/extra-decimals-in-numpy-array – progmatico Mar 12 '18 at 23:12
  • This is due to floating point representation and was covered in numerous stack posts like for instance [this one](https://stackoverflow.com/questions/1778368/python-float-str-float-weirdness). I had a similar issue recently, and for some reasons needed exact values - so I just used strings in my set instead of floats, i.e. I rounded the floats and then added them to sets as strings. That may not be very efficient or pythonic but it worked for me. – atru Mar 12 '18 at 23:12
  • Not happening on numpy version `'1.14.0'`. Couldn't reproduce it. – Guiem Bosch Mar 12 '18 at 23:14
  • @GuiemBosch floating point representation in this sense is not version specific... it's more likely not happening on your computer, right now, for this number set. Like it didn't happen for the last member in the post. – atru Mar 12 '18 at 23:17
  • @atru I was just downgrading to `'1.9.2'` to try it, and I could reproduce it. I don't know if it makes much sense, but it's empirical, same operations under the different versions mentioned produce different results. Using his version I got exactly his same results ;) – Guiem Bosch Mar 12 '18 at 23:20
  • @GuiemBosch it's good to know, I just thought that float representation is you know.. a float representation, can't do anything about it except when printed to screen or converted to something else :) Maybe that's what it is, worth keeping in mind for sure. – atru Mar 12 '18 at 23:22

0 Answers0