Forums

AttributeError: 'SGDClassifier' object has no attribute 'n_iter'

Hi Guys,

I keep getting the error:

AttributeError: 'SGDClassifier' object has no attribute 'n_iter'

in my web app. I have no idea why as SGDClassifier is able to understand 'n_iter' on my local machine running scikit-learn 0.19.0. I even opened up a bash shell and ran

pip --user --upgrade scikit-learn==0.19.0

and it upgraded successfully. Why would 'n_iter' be okay on my machine, but not here? This occurs when trying to update the SGDClassifier using 'partial_fit'.

Where are you running your script/code from? Is it a webapp? what version of python are you using?

I'm running python version 3.6.1 (via Anaconda). Yes, this is a web app run from /home/safesocialapp/application/. The app was working okay a few days ago, then I upgraded scikit-learn to the latest version. In the scikit-learn docs for SGDClassifier here

http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html

I just found for 'n_iter':

n_iter : int, optional The number of passes over the training data (aka epochs). Defaults to None. Deprecated, will be removed in 0.21. Changed in version 0.19: Deprecated

Why would it work in my 0.19.0 version, but not on pythonanywhere?

If you are running python3.6, were you using pip3.6 to install?

UPDATE1: So I just realized in the scikit-learn 0.19.0 docs for the SGDClassifier, there is now a 'max_iter' parameter which can be set instead of using 'n_iter'. However, if I create a new classifier where only 'max_iter' is set, it defaults to setting n_iter = None. Pythonanywhere still sees this 'n_iter' parameter and throws the same error as before.

UPDATE 2: I didn't use pip3.6 the first time. But now if I run

pip3.6 --user --upgrade scikit-learn==0.19.0

in the Bash Console, I get only: "usage pip <command> [options]"

If I just run pip3.6 install scikit-learn==0.19.0, it says:

Installing collected packages: scikit-learn
  Found existing installation: scikit-learn 0.18.1
    Uninstalling scikit-learn-0.18.1:

Exception:
Traceback (most recent call last):
  File "/usr/lib/python3.6/shutil.py", line 538, in move
    os.rename(src, real_dst)
OSError: [Errno 18] Invalid cross-device link: '/usr/local/lib/python3.6/dist-packages/scikit_learn-0.18.1.dist-info/DE
SCRIPTION.rst' -> '/tmp/pip-x0bf1otj-uninstall/usr/local/lib/python3.6/dist-packages/scikit_learn-0.18.1.dist-info/DESC
RIPTION.rst'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python3.6/dist-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/usr/local/lib/python3.6/dist-packages/pip/req/req_set.py", line 778, in install
    requirement.uninstall(auto_confirm=True)
  File "/usr/local/lib/python3.6/dist-packages/pip/req/req_install.py", line 754, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/usr/local/lib/python3.6/dist-packages/pip/req/req_uninstall.py", line 115, in remove
    renames(path, new_path)
  File "/usr/local/lib/python3.6/dist-packages/pip/utils/__init__.py", line 267, in renames
    shutil.move(old, new)
  File "/usr/lib/python3.6/shutil.py", line 553, in move
    os.unlink(src)
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/dist-packages/scikit_learn-0.18.1.dist-info/DE
SCRIPTION.rst'

You were missing install in pip3.6 --user --upgrade scikit-learn==0.19.0

Should be pip3.6 install --user --upgrade scikit-learn==0.19.0

Ah man alright, thanks a lot!

I'm running into basically the same problem. I can run a web app locally on my machine, but when I try to run it on pythonanywhere I get the error message:

'AttributeError: 'SGDClassifier' object has no attribute 'n_iter'

I'm running Python 3.5 and scikit-learn 0.19.

Are you sure you've installed the right version of scikit-learn for python 3.5?

pip3.5 install --user --upgrade scikit-learn==0.19.0

Worked like a charm! Thanks for your help! It looks like I just needed to spend a little more time getting familiar with pythonanywhere's Dashboard.

:)