类型错误:__init__() 获得意外的关键字参数“n_iter”。难道是我安装scikit-learn的方式有问题?

D K*_*D K 2 python 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())
Run Code Online (Sandbox Code Playgroud)

当我运行时,我收到此错误消息:

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'
Run Code Online (Sandbox Code Playgroud)

我尝试卸载并安装 scikit-learn 但这没有帮助。有什么帮助吗?

小智 6

我只是将其更改n_itermax_iter,它对我有用

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