python - Python + Numpy:TypeError:'int'对象不可调用
问题描述
我正在尝试通过 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()
被解释为函数调用。
推荐阅读
- azure - 通过 Azure 策略读取资源的元数据
- css - 无法将字体粗细与谷歌字体匹配并对样式组件做出反应
- sql - 使用 GROUP BY 在 SAS PROC SQL 中的同一表中使用 COUNT 返回 NULL 作为 0
- r - R将double转换为字符串r而不转换为科学
- python - 通过 OpenFaaS 在 Kubernetes 中部署 FastAPI 微服务
- r - 基于具有附加条件的其他列在 R 中创建一个新列
- c# - 是否有从 appsettings 配置 HttpClient 的功能?
- python - 执行其他进程时的进度条
- arrays - Minitest:一个字符串数组作为输入,返回数组中的所有元素,这些元素是数组中至少一个其他字符串的子字符串
- git - 如何使用 git 发布我已经工作了很长时间的项目