Python + Numpy:TypeError:'int'对象不可调用

4

我正在尝试使用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(交叉验证)的不同参数值运行了相同的代码。这是我第一次遇到这个错误。为什么?


1
我不明白你为什么会遇到这个问题——它似乎是一个整数的引用。你是否在IPython工作流程的其他地方将min设置为某些值了? - CJR
1个回答

6

你的代码中的其他地方可能会出现以下类似的内容:

 min = 10

然后你写下这个:
optimal_alpha = alphas[MSE.index(min(MSE))]

因此,min()被解释为一次函数调用。

2
完全正确,除了索引是列表的函数,因此您不会遇到命名空间冲突。它必须是min。 - CJR
1
你们说得完全正确。我最近在我的笔记本中添加了一段新的代码,在其中加入了一行“min=999”。感谢你们两个。 - Debbie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接