Numpy nan

Cause:

https://docs.scipy.org/doc/numpy/user/quickstart.html#indexing-slicing-and-iterating

>>> a = np.arange(10)**3
>>> a
array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])
>>> a[2]
8
>>> a[2:5]
array([ 8, 27, 64])
>>> a[:6:2] = -1000    # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000
>>> a
array([-1000,     1, -1000,    27, -1000,   125,   216,   343,   512,   729])
>>> a[ : :-1]                                 # reversed a
array([  729,   512,   343,   216,   125, -1000,    27, -1000,     1, -1000])
>>> 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

(-1000)**(1/3) =nan??

解释:

https://stackoverflow.com/questions/52925585/cube-root-of-negative-numbers-in-a-numpy-array-returns-nan

numpy 中(-1000)**(1/3)默认算出来为complex复数,与数组类型float不符,所有未nan;

计算负实数根,使用numpy.cbrt函数。

 

你可能感兴趣的:(Python,Numpy)