rsteca / sklearn-deap

Use evolutionary algorithms instead of gridsearch in scikit-learn
MIT License
771 stars 131 forks source link

AttributeError: can't set attribute #44

Open vlatorre847 opened 6 years ago

vlatorre847 commented 6 years ago

This is quite puzzling. macos 10.9.5, python 3.5.2 with anaconda and spyder

My code:

import sklearn
import numpy
from sklearn.model_selection import KFold
import sklearn.neural_network
import sklearn.svm
import sklearn.ensemble
import sklearn.datasets
import time
from evolutionary_search import EvolutionaryAlgorithmSearchCV

dataset=sklearn.datasets.load_iris()
X=dataset.data
Y=dataset.target
seed=1
test_size = int( 0.2 * len( Y ) )
numpy.random.seed( seed )
indices = numpy.random.permutation(len(X))
X_train = X[ indices[:-test_size]]
Y_train = Y[ indices[:-test_size]]
X_test = X[ indices[-test_size:]]
Y_test = Y[ indices[-test_size:]]

network=sklearn.svm.SVC()
lb=[-15,-5]
ub=[3,15]
space_num=[]
space_points=[0]*len(lb)

for i in range(len(lb)):
    space_num.append(int(ub[i]-lb[i]+1))    
    space_points[i]=numpy.logspace(lb[i],ub[i],space_num[i],base=2)
param_grid={'gamma':space_points[0],'C':space_points[1]}

score=sklearn.metrics.make_scorer(sklearn.metrics.accuracy_score)
clf=EvolutionaryAlgorithmSearchCV(network,params=param_grid,cv=3,scoring=score,verbose=2,n_jobs=1)
time_begin=time.time()
clf.fit(X_train,Y_train)
time_end=time.time()-time_begin

print(time_end,sklearn.metrics.accuracy_score(clf.predict(X_test),Y_test))

I get the following error: File "~/anaconda/lib/python3.5/site-packages/evolutionary_search/cv.py", line 301, in init self.bestscore = None

AttributeError: can't set attribute

Any idea?

rsteca commented 6 years ago

I couldn't reproduce this error. What version of the library are you using? Can you give me a pip freeze of your env?

vlatorre847 commented 6 years ago

Here the pip freeze:

alabaster==0.7.9 anaconda-clean==1.0 anaconda-client==1.6.3 anaconda-navigator==1.6.4 anaconda-project==0.6.0 appdirs==1.4.0 appnope==0.1.0 appscript==1.0.1 argcomplete==1.0.0 astroid==1.4.7 astropy==1.2.1 auto-sklearn==0.2.1 autopep8==1.3.3 Babel==2.3.4 backports.shutil-get-terminal-size==1.0.0 backports.weakref==1.0rc1 beautifulsoup4==4.5.1 bitarray==0.8.1 blaze==0.10.1 bleach==1.5.0 bokeh==0.12.2 boto==2.42.0 Bottleneck==1.1.0 cffi==1.7.0 chardet==2.3.0 chest==0.2.3 click==6.6 cloudpickle==0.2.1 clyent==1.2.2 colorama==0.3.7 conda==4.3.30 conda-build==2.0.2 configobj==5.0.6 ConfigSpace==0.3.10 contextlib2==0.5.3 cryptography==1.5 cycler==0.10.0 Cython==0.27.1 cytoolz==0.8.0 dask==0.15.0 datashape==0.5.2 deap==1.0.2 decorator==4.0.10 dill==0.2.5 docutils==0.12 dynd==0.7.3.dev1 et-xmlfile==1.0.1 f90wrap==0.1.4 fastcache==1.0.2 filelock==2.0.6 Flask==0.11.1 Flask-Cors==2.1.2 flatdict==1.2.0 future==0.16.0 gevent==1.1.2 greenlet==0.4.10 h5py==2.6.0 HeapDict==1.0.0 -e git+https://github.com/hyperopt/hyperopt-sklearn.git@e457746664fbfd5bb9f1536a1b71b178b09598a8#egg=hpsklearn html5lib==0.9999999 hyperopt==0.1 idna==2.1 imagesize==0.7.1 ipykernel==4.5.0 ipython==5.1.0 ipython-genutils==0.1.0 ipywidgets==5.2.2 itsdangerous==0.24 jdcal==1.2 jedi==0.9.0 Jinja2==2.8 joblib==0.11 jsonpickle==0.9.4 jsonschema==2.5.1 jupyter==1.0.0 jupyter-client==4.4.0 jupyter-console==5.0.0 jupyter-core==4.2.0 Keras==2.0.5 Lasagne==0.1 lazy-object-proxy==1.2.1 liac-arff==2.1.1 llvmlite==0.13.0 locket==0.2.0 lockfile==0.12.2 lxml==3.6.4 Markdown==2.2.0 MarkupSafe==0.23 matplotlib==1.5.3 mistune==0.7.3 mock==2.0.0 mpmath==0.19 multipledispatch==0.4.8 nb-anacondacloud==1.2.0 nb-conda==2.0.0 nb-conda-kernels==2.0.0 nbconvert==4.2.0 nbformat==4.1.0 nbpresent==3.0.2 networkx==1.11 nltk==3.2.1 nose==1.3.7 notebook==4.2.3 numba==0.28.1 numexpr==2.6.1 numpy==1.13.3 numpydoc==0.6.0 odo==0.5.0 openpyxl==2.3.2 packaging==16.8 pandas==0.21.0 partd==0.3.6 path.py==0.0.0 pathlib2==2.1.0 patsy==0.4.1 pbr==1.10.0 pep8==1.7.0 pexpect==4.0.1 pickleshare==0.7.4 Pillow==3.3.1 pkginfo==1.3.2 ply==3.9 prompt-toolkit==1.0.3 protobuf==3.3.0 psutil==5.3.1 ptyprocess==0.5.1 py==1.4.31 pyasn1==0.1.9 pycodestyle==2.3.1 pycosat==0.6.1 pycparser==2.14 pycrypto==2.6.1 pycurl==7.43.0 pyflakes==1.3.0 pyFRF==0.34 Pygments==2.1.3 pylint==1.5.4 pymongo==3.5.1 pynisher==0.4.2 pyOpenSSL==16.2.0 pyparsing==2.1.10 pyrfr==0.6.1 pytest==2.9.2 python-dateutil==2.6.1 pytz==2017.3 PyYAML==3.12 pyzmq==15.4.0 QtAwesome==0.4.4 qtconsole==4.2.1 QtPy==1.3.1 redis==2.10.5 requests==2.12.4 rope-py3k==0.9.4.post1 rpy2==2.8.5 ruamel-yaml===-VERSION scikit-image==0.12.3 scikit-learn==0.18.2 scikit-neuralnetwork==0.7 scikit-rf==0.14.5 scipy==0.19.1 simplegeneric==0.8.1 singledispatch==3.4.0.3 six==1.11.0 sklearn-deap==0.2.2 smac==0.6.0 snowballstemmer==1.2.1 sockjs-tornado==1.0.3 Sphinx==1.4.6 sphinx-rtd-theme==0.2.4 spyder==3.2.4 SQLAlchemy==1.0.13 statsmodels==0.6.1 sympy==1.0 tables==3.2.3.1 tensorflow==1.2.0 terminado==0.6 Theano==0.9.0 toolz==0.8.0 tornado==4.4.1 traitlets==4.3.0 typing==3.6.2 unicodecsv==0.14.1 wcwidth==0.1.7 Werkzeug==0.12.2 widgetsnbextension==1.2.6 wrapt==1.10.6 xlrd==1.0.0 XlsxWriter==0.9.3 xlwings==0.10.0 xlwt==1.1.2

kootenpv commented 6 years ago

I also have this suddenly

kootenpv commented 6 years ago

For some reason I had to roll back to scikit-learn==0.18.2, I tried upgrading to 0.19.0 now, and the error went away.

mortendaehli commented 6 years ago

I have the same issue, but upgrading to sklearn 0.19.1 did not solve the issue.

mortendaehli commented 6 years ago

I solved it by removing these fromt init

    #self.best_score_ = None
    #self.best_params_ = None

Looks like they are already declared in the base module, and it creates some trouble for some strange reasons... Any ideas?

Seems to be working just fine without declaring None

Edit: It didn't work. It fails in the end with the same error at:

    self._best_score = current_best_score_
    self._best_params = current_best_params_

Edit2: It did work in the end by just renaming to something else.

rsteca commented 6 years ago

Maybe something changed with the new version of scikit-learn, I'll see if I have some time to fix this bug.

flipdazed commented 6 years ago

This shows how to solve this problem: https://stackoverflow.com/q/35950741/4013571

class Test(BaseSearchCV):
     best_score_ = None
     def __init__(self):
         self.best_score_ = None

 t = Test()
AbhilashMathews commented 6 years ago

I am using the same package on Linux (Fedora) and receiving the same error. This is occurring when simply running the example test code:

import sklearn.datasets
import numpy as np
import random

data = sklearn.datasets.load_digits()
X = data["data"]
y = data["target"]

from sklearn.svm import SVC
from sklearn.model_selection import StratifiedKFold

paramgrid = {"kernel": ["rbf"],
             "C"     : np.logspace(-9, 9, num=25, base=10),
             "gamma" : np.logspace(-9, 9, num=25, base=10)}

random.seed(1)

from evolutionary_search import EvolutionaryAlgorithmSearchCV
cv = EvolutionaryAlgorithmSearchCV(estimator=SVC(),
                                   params=paramgrid,
                                   scoring="accuracy",
                                   cv=StratifiedKFold(n_splits=4),
                                   verbose=1,
                                   population_size=50,
                                   gene_mutation_prob=0.10,
                                   gene_crossover_prob=0.5,
                                   tournament_size=3,
                                   generations_number=5,
                                   n_jobs=4)

Traceback (most recent call last):

  File "<ipython-input-1-d9e256e00ecd>", line 29, in <module>
    n_jobs=4)

  File "/home/mathewsa/.local/lib/python2.7/site-packages/evolutionary_search/cv.py", line 301, in __init__
    self.best_score_ = None

AttributeError: can't set attribute

And the pip freeze:

[mathewsa@desktop ~]$ python -m pip freeze
Babel==1.3
Bottleneck==0.6.0
chardet==2.2.1
cssselect==0.9.1
cycler==0.10.0
Cython==0.23.4
deap==1.2.2
decorator==4.0.10
docutils==0.12
emcee==2.2.1
empy==3.3.2
eqtools==1.3.1
fail2ban==0.9.3
funcsigs==1.0.2
gps==3.15
gptools==0.2.3
h5-data==0.2.0
h5py==2.5.0
hgapi==1.7.2
husl==4.0.3
idl-python==2.0
iniparse==0.4
IPy==0.81
ipython==3.2.1
Jinja2==2.8
jsonschema==2.4.0
kitchen==1.2.1
lxml==3.4.4
MarkupSafe==0.23
matplotlib==1.5.1
mdsconnector==1.0
mdsplus-alpha==7.46.1
mercurial==3.5.2
mistune==0.6
mock==2.0.0
mpmath==1.0.0
netCDF4==1.1.6
nose==1.3.7
Numeric==24.2
numexpr==2.4.6
numpy==1.13.1
pandas==0.17.1
path.py==5.2
pbr==1.10.0
pexpect==4.0.1
Pillow==3.0.0
plumbum==1.6.3
policycoreutils-default-encoding==0.1
profiletools==1.0.0
ptyprocess==0.5.1
pwquality==1.3.0
pyandoc==0.0.1
pycurl==7.19.5.1
pyflakes==1.0.0
Pygments==2.1.3
pygobject==3.18.2
pygpgme==0.3
pyinotify==0.9.6
pyliblzma==0.5.3
pymssql==2.1.3
PyOpenGL==3.1.0
PyPAM==0.5.0
pyparsing==2.1.5
PyPDF2==1.26.0
python-dateutil==2.5.3
python-Levenshtein==0.10.1
python-systemd==231
pytz==2016.6.1
pyxattr==0.5.3
pyxdg==0.25
pyzmq==14.7.0
rope==0.10.2
rpm-python==4.13.0rc1
rpyc==3.4.4
scdate==1.10.9
scikit-learn==0.18.1
scipy==1.1.0
seaborn==0.5.1
seobject==0.1
sepolicy==1.1
simplegeneric==0.8.1
simplejson==3.5.3
six==1.10.0
sklearn==0.0
sklearn-deap==0.2.2
slip==0.6.4
Sphinx==1.2.3
spyder==2.3.8
SSSDConfig==1.14.2
tables==3.2.2
tornado==4.2.1
translate-toolkit==1.9.0
triangle-plot==0.3.0
urlgrabber==3.10.1
vboxapi==1.0
virtualenv==15.0.2
vobject==0.8.1rc0
wxPython==3.0.2.0
wxPython-common==3.0.2.0
yum-metadata-parser==1.1.4
[mathewsa@desktop ~]$ python -m pip freeze --user
deap==1.2.2
scipy==1.1.0
sklearn-deap==0.2.2
rskronek commented 1 year ago

Hi,

Getting the attribute error, as shown. installed whatever is mentioned in this thread, still the same error.


AttributeError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_10444\1573941315.py in 3 from imblearn.ensemble import BalancedRandomForestClassifier 4 rf = BalancedRandomForestClassifier(n_estimators = 100) ----> 5 rf = rf.fit(X_train, y_train)

~\anaconda3\envs\PythonData\envs\mlenv\lib\site-packages\imblearn\ensemble_forest.py in fit(self, X, y, sampleweight) 433 434 # Remap output --> 435 , self.nfeatures = X.shape 436 437 y = np.atleast_1d(y)

AttributeError: can't set attribute