I precise that I am aware of the concepts about decimal representation and the supposedly relevant questions such as this, this and this, and the suggested documentation.
Still I am astonished that in Python3 round()
sometimes returns an integer (as per the documentation) and sometimes a float (weird to me), depending upon an unsignificant difference in the type of its first argument.
I understand that, e.g., floor()
or ceil()
always return a float, and of course I found reasonable that round(x, digits)
returns an integer or a float depending on the second parameter digits
being specified or not, and even that different implementations of round
exist (e.g. the builtin
and the numpy
one).
Specifically, the following code shows that a float32
and a numpy.float32
make round()
return different types:
>>> x=101/100
>>> x
1.01
>>> import numpy as np
>>> nx=np.float32(x)
>>> nx
1.01
>>> round(x)
1
>>> round(nx)
1.0
For the curious, I get my x
value as the prediction of a classification model in Keras, and need to obtain an index out of it, by rounding to the nearest integer, to select the right label out of a list of strings.
And a search in StackOverflow/Stackexchange doesn't reveal the matter was already treated. Pls give me a helpful link in case.
To be more specific, I am getting a predicted value out of a Neural Network classifier, i.e. a number approaching either 0 or 1 or another integer, and want to use it as an index to print the correct label associated to the prediction (it should be an almost basic need).
The solution is quite simple, although ugly: int(round(x))
(also explained here); but I still consider it a kind of workaround to an undesired behaviour.
I know that why does this work so questions are not appreciated, so let's ask what is the correct pythonic way to obtain that integer and that label out of a numpy.float32
value, in the hope that somebody unveils also an interesting rationale behind this ...ehm... functionality.