In Python 3.6, why does a negative number to the power of a fraction return nan when in a numpy array?

不打扰是莪最后的温柔 提交于 2021-02-07 12:32:33

问题


I have started learning Python recently and I've been going through the NumPy official quickstart guide which includes this example for iterating.

>>> a
array([-1000,     1, -1000,    27, -1000,   125,   216,   343,   512,   
729])
>>> for i in a:
...     print(i**(1/3.))
...
nan
1.0
nan
3.0
nan
5.0
6.0
7.0
8.0
9.0

However, if I just try to raise -1000 to the power of (1/3.) outside of the loop it returns a value.

>>> -1000**(1/3.)
-9.999999999999998

With parentheses around -1000 it also returns a value.

>>> (-1000)**(1/3.)
(5+8.660254037844384j)

Why is it that the same action returns nan in the for loop? I'm using Python 3.6.3 :: Anaconda custom (64-bit). I also tried with different fractions that do not round up and it's the same. With a fraction that rounds up to .0 it works though.

I couldn't find a similar question. Excuse me if I'm missing something very obvious.

Edit: A few comments mentioned that the question duplicates NumPy, RuntimeWarning: invalid value encountered in power and it's true, the problem was I didn't see such an error. The discussion there, however, seems to include a few possible workarounds.


回答1:


Exponentiation in python has higher precedence than the negative operator. Thus -1000**(1/3) is equivalent to -(1000**(1/3)).

When you doing this operation inside the loop you get (-1000)**(1/3). This equal to 10 * (-1**(1/3)) which a complex number. Now the array you have, uses a default data type since you did not define any that is determined according to the documentation as follows:

dtype : data-type, optional

The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence. This argument can only be used to ‘upcast’ the array. For downcasting, use the .astype(t) method.

So it is probably np.int16.

Putting all the information together, we can conclude that your array is not equipped with the appropriate dtype attribute to be able to hold the result of (-1000)**(1/3) even though the result exists.

This does not happen outside arrays since there, no dtype is assumed.


Fix \ Workaround:

>>> a = np.array([-1000, 1], dtype=np.complex)
>>> for i in a:
...     print(i**(1/3.))
...
(5+8.66025403784j)
(1+0j)



回答2:


This is just a short cut to fix it for your question.

 def ownpow(a, b):
        if a > 0:
            return a**b
        if a < 0:
            temp = abs(a)**b
            return -1*temp

    a= np.array([-1000,     1, -1000,    27, -1000,   125,   216,   343,   512,  729]) 

    for i in a:
        print(ownpow(i,(1/3.)))

apparent numpy array values cannot be raised to negative number, as this could result in complex numbers.



来源:https://stackoverflow.com/questions/49029019/in-python-3-6-why-does-a-negative-number-to-the-power-of-a-fraction-return-nan

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!