Numpy Odd Behavior

Hello Pythonistas,

I am using Python IDLE.

I imported the numpy module. When I tested some functions, the following outputs for various function operations were observed.

Does anyone know why the output results are formatted this way? How do you disable this output format so that only values are provided without the wrappers.

import numpy as np

>>> np.sqrt(25)
np.float64(5.0)
>>> np.square(10)
np.int64(100)
>>> np.sin(45)
np.float64(0.8509035245341184)
>>> np.sin(90/57.3)
np.float64(0.9999999933069259)

By the way, on my previous computer, I had Windows 10. I did not observe these types of outputs when using the numpy module. I am now using a new computer with Windows 11 Pro. Not sure if this is related.

Can someone please help or offer insights.

Win10 versus Win11 should have no effect. Also, IDLE versus the standard REPL versus any other shell should have no effect. (You did not ask, but some people guess wrongly.)

Does the new computer have a new version of numpy? Or perhaps numpy has an output format setting. Someone else will have to answer this.

Numpy 2.0 updated the __repr__ format so that you can tell that you’re working with numpy scalars and not Python floats/ints. __str__() still prints the number as expected:

In [1]: import numpy as np

In [2]: np.float64(1.299)
Out[2]: np.float64(1.299)

In [3]: print(np.float64(1.299))
1.299

If this bothers you, you can modify the printoptions:

In [5]: np.set_printoptions(legacy='1.25')

In [6]: np.float64(1.299)
Out[6]: 1.299

Note that this is a global setting, so a library should not do this.

2 Likes

Thank you for responding to my query.

Mathematically, does it make a difference? If not, this appears to be something that wasn’t broken and didn’t need fixing.

Thank you for the workaround, however.

Much appreciated.

Mathematically it is the same, with the proviso that different numpy types may have different precision and so on. But it can make a difference in other ways that are relevant to writing a program; for instance, some of the numpy types are not JSON serializable.

The second sentence of

says “For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval();”. Numpy used 2.0 as an opportunity to join ‘many types’. Note that print() applies str to its args, so stored program output is not changed. The IDLE shell follows the standard REPR in using repr() when echoing expression values.

1 Like

See NEP 51 — Changing the representation of NumPy scalars.

1 Like

Thank you.

This link explains it rather well.

Much appreciated.

They are different types, and if a function treats one in a different way, you get different results.
It is good for you to know that you have an np.float64 rather than a float.

In Python 3.12 with the compensated sum that they inserted for sum specifically for float.

>>> import numpy as np
>>> sum([np.float64(2**53), np.float64(1.0), np.float64(1.0)])
9007199254740992.0
>>> sum([float(2**53), float(1.0), float(1.0)])
9007199254740994.0
1 Like