2016-02-12 62 views
1

遇到从this post据我所知,在那里进行评估之前,LOG10()计算。简单地说,我不明白这个问题提供了答案。也是为什么会在日志10()先评估,当然这只是导致不必要的计算?NumPy的RuntimeWarning:被零除在日志10

merge_y = np.where(N = < 1,1,N * np.log10(n))的

import matplotlib.pyplot as plt 
import numpy as np 

n = np.arange(0, 10, 0.0001) 

merge_y = np.where(n <= 1, 1, n * np.log10(n)) 
insertion_y = n*n 

plt.plot(n, merge_y,'g') 
plt.plot(n,insertion_y,'r') 
plt.grid(True) 
plt.xlabel('n') 
plt.ylabel('T(n)') 
plt.title('Time complexities of merge and insertion sort w/ input size n') 
plt.show() 
+1

你一定要明白,你试图计算'LOG10(0)',对吧? – cel

+0

我不能看到,对于(条件A,B)的心不是B如果条件为假并且如果这是真的回来... – trunks1ace

回答

2

你有要明白,np.where基于逻辑的索引工作,你思考它像一个循环。什么np.where确实是

np.where(return_this_logical_indexes, From_this_array_if_true, From_this_array_if_false) 

但是,为了做到这一点,这些阵列必须存在,如果它是一个函数,那么它就会以获得一个数组,然后指数它与创建的逻辑阵列对其进行评估条件。

你认为它更像是一个列表悟,如:

merge_y = [x*np.log10(x) if x>=1 else 1 for x in n] 
0

为什么不这样做:

merge_y = np.ones_like(n) 
mask = (n > 1) 
n_m = n[mask] 
merge_y[mask] = n_m * np.log10(n_m)