2016-02-12 60 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)