简体   繁体   中英

Perceptron in Python

I'm using sklearn library. I have a question about the attribute: n_iter_ . When executing the code I get TypeError: __init__() got an unexpected keyword argument 'n_iter_' . Also try using n_iter but I get the same error, or maybe I am misspelling the attribute. It is not all the code, if you need more information, let me know

from sklearn.linear_model import Perceptron
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split


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

Perceptron Model in sklearn.linear_model doesn't have n_iter_ as a parameter. It has following parameters with similar names.

max_iter: int, default=1000
The maximum number of passes over the training data (aka epochs). It only impacts the behavior in the fit method, and not the partial_fit method.

and

n_iter_no_change : int, default=5
Number of iterations with no improvement to wait before early stopping.

New in version 0.20.

By looking at your code it looks like you intended to use max_iter .

So do

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

Note: You should first upgrade your sklearn using

pip install sklearn -upgrade

The attribute given in the documentation is n_iter and not n_iter_

So this should work:

from sklearn.linear_model import Perceptron
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split


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

First check which Scikit-learn version you have installed. You can do that by executing

python -c "import sklearn;print(sklearn.__version__)"

on your terminal/environment to which you have the python that executes your code.

Perceptron initial parameters have changed from n_iter to max_iter in version 0.20. The best way to keep up, head to the documentation or source code of the correct version and read the params: eg

documentation: perceptron docs v.0.23 source code: perceptions.0.23 code

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