You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
AttributeError: 'float' object has no attribute 'sqrt'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in<module>
TypeError: loop of ufunc does not support argument 0 of type float which has no callable sqrt method
Python and NumPy Versions:
2.1.3
3.12.2 (main, Feb 6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)]
There are more ufuncs not working: for example np.sin(feature) gives the same error.
The following example works:
import numpy as np
import math
class A(float):
def sqrt(self):
print('sqrt method called!')
return math.sqrt(self)
feature = np.array([A(2.2)], dtype=object)
print(np.sum(feature))
print(np.sqrt(feature))
For an array with dtype object it seems np.sqrt is trying to call x.sqrt() for every element x. @MarcBresson I suspect you expected numpy would call np.sqrt on the elements of feature instead?
Not sure this is a bug or a feature, but I agree the error message might be a bit unclear.
I could be seen as a feature since object arrays can store anything, so this probably maximizes compatibility in some contexts. However, there could be a fallback onto np.sqrt in case the object does not have a sqrt method.
I wonder why sum does not yield that error. Would it be because the + operator is available for nearly all python objects?
Describe the issue:
np.sqrt is one of the only operation that crashes on a nd array of dtype object containing only floats.
Furthermore, the error yielded is very unclear, mentioning that floats does not support sqrt (which is not true).
The example speak for itself.
Reproduce the code example:
Error message:
Python and NumPy Versions:
2.1.3
3.12.2 (main, Feb 6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)]
Runtime Environment:
[{'numpy_version': '2.1.3',
'python': '3.12.2 (main, Feb 6 2024, 20:19:44) [Clang 15.0.0 '
'(clang-1500.1.0.2.5)]',
'uname': uname_result(system='Darwin', node='Marcos-Macbook-air.local', release='23.4.0', version='Darwin Kernel Version 23.4.0: Fri Mar 15 00:19:22 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T8112', machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP'],
'not_found': ['ASIMDFHM']}}]
Context for the issue:
No response
The text was updated successfully, but these errors were encountered: