Python3 - TypeError: 'numpy.float64' object is not iterable
Python3 - TypeError: 'numpy.float64' object is not iterable
我想使用 KNN 制作一个图表,显示错误分类错误与 de K 个邻居的关系。
这是我为此构建的代码:
# creating odd list of K for KNN
myList = list(range(1,50))
# subsetting just the odd ones
neighbors = filter(lambda x: x % 2 != 0, myList)
# empty list that will hold cv scores
cv_scores = []
# perform 10-fold cross validation
for k in neighbors:
knn = KNN(n_neighbors=k, n_jobs = 6, metric = 'minkowski', contamination = 0.05)
scores = cross_val_score(knn, X_test, pred, cv=10, scoring='accuracy')
cv_scores.append(scores.mean())
### Create Plot
import matplotlib.pyplot as plt
plt.style.use('ggplot')
# changing to misclassification error
MSE = [1 - x for x in cv_scores]
# determining best k
optimal_k = neighbors[MSE.index(min(next(iter(MSE))))]
print ("The optimal K neighbors number is %d" % optimal_k)
# plot misclassification error vs k
plt.plot(neighbors, MSE, figsize = (20,12))
plt.xlabel('Number of Neighbors K')
plt.ylabel('Misclassification Error')
plt.show()
问题出在这一行:
optimal_k = neighbors[MSE.index(min(next(iter(MSE))))]
这段代码好像是写在python2.这是原来的行:
optimal_k = neighbors[MSE.index(min(MSE))]
我添加了 next()
和 iter()
来解决这个问题,正如其他与此类似的线程中的一些用户所建议的那样。但我收到此错误:
TypeError: 'numpy.float64' object is not iterable
我知道为什么会出现这个错误,它应该在列表中迭代,但它只取数字。我认为问题出在这一行的 filter()
使用:
neighbors = filter(lambda x: x % 2 != 0, myList)
如何在 python 3 上将此代码修复为 运行 并防止这种情况发生??
提前致谢
编辑:
我使用的 KNN 版本不是 sklearn 中的版本,对于那些想尝试此代码的人来说。它来自一个名为 PYOD 的异常检测包。 Linkhere
您也可以使用 sklearn 的原始 KNN 进行尝试,但请注意,您需要删除污染参数,也许还需要删除距离参数
问题在于代码将 neighbors
定义为生成器并在第一个循环中耗尽它。解决方案:使用列表。
neighbors = list(filter(lambda x: x % 2 != 0, myList))
此外,您获得最佳结果的原始语法是正确的(不需要 iter
或 next
):
optimal_k = neighbors[MSE.index(min(MSE))]
我想使用 KNN 制作一个图表,显示错误分类错误与 de K 个邻居的关系。
这是我为此构建的代码:
# creating odd list of K for KNN
myList = list(range(1,50))
# subsetting just the odd ones
neighbors = filter(lambda x: x % 2 != 0, myList)
# empty list that will hold cv scores
cv_scores = []
# perform 10-fold cross validation
for k in neighbors:
knn = KNN(n_neighbors=k, n_jobs = 6, metric = 'minkowski', contamination = 0.05)
scores = cross_val_score(knn, X_test, pred, cv=10, scoring='accuracy')
cv_scores.append(scores.mean())
### Create Plot
import matplotlib.pyplot as plt
plt.style.use('ggplot')
# changing to misclassification error
MSE = [1 - x for x in cv_scores]
# determining best k
optimal_k = neighbors[MSE.index(min(next(iter(MSE))))]
print ("The optimal K neighbors number is %d" % optimal_k)
# plot misclassification error vs k
plt.plot(neighbors, MSE, figsize = (20,12))
plt.xlabel('Number of Neighbors K')
plt.ylabel('Misclassification Error')
plt.show()
问题出在这一行:
optimal_k = neighbors[MSE.index(min(next(iter(MSE))))]
这段代码好像是写在python2.这是原来的行:
optimal_k = neighbors[MSE.index(min(MSE))]
我添加了 next()
和 iter()
来解决这个问题,正如其他与此类似的线程中的一些用户所建议的那样。但我收到此错误:
TypeError: 'numpy.float64' object is not iterable
我知道为什么会出现这个错误,它应该在列表中迭代,但它只取数字。我认为问题出在这一行的 filter()
使用:
neighbors = filter(lambda x: x % 2 != 0, myList)
如何在 python 3 上将此代码修复为 运行 并防止这种情况发生??
提前致谢
编辑:
我使用的 KNN 版本不是 sklearn 中的版本,对于那些想尝试此代码的人来说。它来自一个名为 PYOD 的异常检测包。 Linkhere
您也可以使用 sklearn 的原始 KNN 进行尝试,但请注意,您需要删除污染参数,也许还需要删除距离参数
问题在于代码将 neighbors
定义为生成器并在第一个循环中耗尽它。解决方案:使用列表。
neighbors = list(filter(lambda x: x % 2 != 0, myList))
此外,您获得最佳结果的原始语法是正确的(不需要 iter
或 next
):
optimal_k = neighbors[MSE.index(min(MSE))]