简体   繁体   中英

Python3 - TypeError: 'numpy.float64' object is not iterable

I want to make a plot that shows the missclassification error versus de K neighbors using KNN.

This the code i've built for that:

# 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()

The problem is at this line:

optimal_k = neighbors[MSE.index(min(next(iter(MSE))))]

This code seemed to be written in python 2. This was the original line:

optimal_k = neighbors[MSE.index(min(MSE))]

I added next() and iter() to solve this issue, as adviced by some usersin other threads similar to this. But i'm getting this error:

TypeError: 'numpy.float64' object is not iterable

I know why this error is happening, it should be iterting through a list but it's taking only the numbers. I think the problem comes from the filter() use at this line:

neighbors = filter(lambda x: x % 2 != 0, myList)

How can i fix this code to run on python 3 and prevent this from happening??

Thanks in advance

EDIT:

The KNN version i'm using is not the one in sklearn, for those who would like to try this code. It comes from an anomaly detetction package called PYOD. Link here

You can also try it with the original KNN from sklearn, but note you will need to remove the contamination parameter and maybe the distance parameter

The issue is that the code is defining neighbors as a generator and exhausting it in the first loop. Solution: use a list.

neighbors = list(filter(lambda x: x % 2 != 0, myList))

Also your original syntax for getting the optimal was correct (no need for iter or next ):

optimal_k = neighbors[MSE.index(min(MSE))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM