D K
D K

Reputation: 59

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?

Upvotes: 2

Views: 16535

Answers (3)

Mustafain Rehman
Mustafain Rehman

Reputation: 1

for skimage version 0.21.0 it is changed to max_num_iter=100. It worked or me

Upvotes: 0

drpzet
drpzet

Reputation: 61

I just change the n_iter to max_iter and it work for me

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

Upvotes: 6

Kim Tang
Kim Tang

Reputation: 2478

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.

Upvotes: 1

Related Questions