简体   繁体   中英

TypeError: __init__() got an unexpected keyword argument 'n_iter'. Is this a problem with the way I installed scikit-learn?

from sklearn import datasets 
import numpy as np

# Assigning the petal length and petal width of the 150 flower samples to Matrix X 
# Class labels of the flower to vector y

iris = datasets.load_iris() 
X = iris.data[:, [2, 3]] 
y = iris.target
print('Class labels:', np.unique(y))

from sklearn.model_selection import train_test_split 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1, stratify=y)

print('Labels counts in y:', np.bincount(y)) 
print('Labels counts in y_train:', np.bincount(y_train)) 
print ('Labels counts in y_test:', np.bincount(y_test))

from sklearn.preprocessing import StandardScaler 
sc = StandardScaler()
sc.fit(X_train) 
X_train_std = sc.transform(X_train) 
X_test_std = sc.transform(X_test)

from sklearn.linear_model import Perceptron

ppn = Perceptron(n_iter=40, eta0=0.1, random_state=1)
ppn.fit(X_train_std, y_train)

y_pred = ppn.predict(X_test_std) 
print('Misclassified samples: %d' % (y_test != y_pred).sum())

When I run I get this error message:

Traceback (most recent call last):
  File "c:/Users/Desfios 5/Desktop/Python/Ch3.py", line 27, in <module>
    ppn = Perceptron(n_iter=40, eta0=0.1, random_state=1)
  File "C:\Users\Desfios 5\AppData\Roaming\Python\Python38\site-packages\sklearn\utils\validation.py", line 72, in inner_f
    return f(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'n_iter'

I've tried uninstalling and installing scikit-learn but that did not help. Any help?

我只是将n_iter更改为max_iter ,它对我max_iter

ppn = Perceptron(max_iter=40, eta0=0.3, random_state=0)

You receive this error

TypeError: init () got an unexpected keyword argument 'n_iter'

because the Perceptron has no parameter 'n_iter' you can use before fitting it.

You are trying to access the n_iter_ attribute, which is an "Estimated attribute" (you can tell by the underscore at the end) and only stored after the fit method has been called. Reference in Documentation

Before fitting, you can only access the n_iter_no_change parameter for n_iter .

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