[PYTHON] Use numpy without stress! (Interpretation of error "Attribute Error:'float' object has no attribute'sin'" (action?))

Purpose

** Use numpy without stress! ** ** ** numpy ** is often used when doing calculations in Python. Therefore, it is not uncommon to encounter numpy-related errors. In order to resolve the error as soon as possible, it is necessary to raise the level of understanding as much as possible. Stick to anything and deepen your understanding.

The errors to be addressed here are as follows.

AttributeError: 'float' object has no attribute 'sin'

For example, the following is the situation in which it appears.

>>>
>>> import numpy as np
>>> a = np.array([1.1, 2.2],dtype=object)
>>> np.sin(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'sin'
>>>

The reason for picking up this error is ** I don't understand the meaning of the error ** Because I think it will be. 'float' object does not have sin It is said, but I have not requested such processing. .. .. ..

Study environment

Name: numpy Version: 1.16.4

Error description

The following article will be helpful for this error.

https://github.com/numpy/numpy/issues/13666

The reporter here is

Many NumPy methods fail if an array has dtype object, and produce a misleading error message.

If you use other power to translate it into Japanese (Google Translate (Mama))

** If the array has dtype objects, many NumPy methods will fail with a misleading error message. ** **

Is claimed. ** ⇒ I think that's right. ** **

I'm not sure about the conclusion of the exchange on this site, but ... Anyways, numpy side too ** Error message is not very appropriate ** I seem to admit that. It can be read as if it is cured. .. .. .. I don't think it has healed.

How to deal with the error

How to deal with the error depends on what you want to do in the first place, If for some reason dtype = object needs to be ** Cast astype before calculating sin ** I think that is good.

>>> a = np.array([1.1, 2.2],dtype=object)
>>> np.sin(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'sin'
>>> np.sin(a.astype(np.float64))
array([0.89120736, 0.8084964 ])
>>>

Summary

I became a little familiar with numpy. numpy is deep, so I think it's still within the margin of error.

Related (person)

Learn Python carefully using both English and Japanese.

from now on

numpy, let's learn. If you have any comments, please let us know. : candy:

Recommended Posts

Use numpy without stress! (Interpretation of error "Attribute Error:'float' object has no attribute'sin'" (action?))
[Dlib] Attribute Error: module'dlib' has no attribute ...
[Python] Attribute Error:'list' object has no attribute'replace'
[Wandb] Attribute Error: module'wandb' has no attribute'init'