简体   繁体   中英

TypeError: __init__() got an unexpected keyword argument 'i'

I am trying to determine the suitable neighbor number for machine learning KNneighbors algorithm.

from sklearn.neighbors import KNeighborsClassifier
training_accuracy = []
test_accuracy = []
n_s = range(1,11)
for i in n_s:
    clf = KNeighborsClassifier(i=i)
    clf.fit(X_train, y_train)
    training_accuracy.append(clf.score(X_train, y_train))
    test_accuracy.append(clf.score(X_test, y_test))

The error I am getting:

TypeError                                 Traceback (most recent call last)
<ipython-input-12-80ad682622ef> in <module>
      4 n_s = range(1,11)
      5 for i in n_s:
----> 6     clf = KNeighborsClassifier(i=i)
      7     clf.fit(X_train, y_train)
      8     training_accuracy.append(clf.score(X_train, y_train))

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\neighbors\_classification.py in __init__(self, n_neighbors, weights, algorithm, leaf_size, p, metric, metric_params, n_jobs, **kwargs)
    152             leaf_size=leaf_size, metric=metric, p=p,
    153             metric_params=metric_params,
--> 154             n_jobs=n_jobs, **kwargs)
    155         self.weights = _check_weights(weights)
  

TypeError: __init__() got an unexpected keyword argument 'i'

How can I fix this issue?

Instead of using i parameter, use n_neighbors instead.

for i in n_s:
    clf = KNeighborsClassifier(n_neighbors=i)

Check the documentation of KNeighborsClassifier . Here you can see that there is no i parameter and n_neighbors is the first argument.

There is few updation in syntax of algorithm in wrt to time Now This works for me !!

> for i in range(1,40):
>     knn = RandomForestClassifier(i)

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