簡體   English   中英

Python + Numpy:TypeError:'int'對象不可調用

[英]Python + Numpy : TypeError: 'int' object is not callable

我試圖通過10倍交叉驗證找到加性平滑的最佳平滑參數。 我寫了以下代碼:

alphas = list(np.arange(0.0001, 1.5000, 0.0001))

#empty list that stores cv scores
cv_scores = []

#perform k fold cross validation
for alpha in alphas:
    naive_bayes = MultinomialNB(alpha=alpha)
    scores = cross_val_score(naive_bayes, x_train_counts, y_train, cv=10, scoring='accuracy')
    cv_scores.append(scores.mean())

#changing to misclassification error
MSE = [1 - x for x in cv_scores]  

#determining best alpha
optimal_alpha = alphas[MSE.index(min(MSE))]

我收到以下錯誤:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-9d171ddceb31> in <module>()
     18 
     19 #determining best alpha
---> 20 optimal_alpha = alphas[MSE.index(min(MSE))]
     21 print('\nThe optimal value of alpha is %f' % optimal_alpha)
     22 

TypeError: 'int' object is not callable

我為arange()和K(交叉驗證)的不同參數值運行了相同的代碼。 這是我第一次遇到這個錯誤。 為什么?

代碼中的其他地方你有一些看起來像這樣的東西:

 min = 10

然后你寫這個:

optimal_alpha = alphas[MSE.index(min(MSE))]

因此, min()被解釋為函數調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM