EpistasisLab / tpot

A Python Automated Machine Learning tool that optimizes machine learning pipelines using genetic programming.
http://epistasislab.github.io/tpot/
GNU Lesser General Public License v3.0
9.76k stars 1.57k forks source link

AttributeError: 'TPOTClassifier' object has no attribute 'classes_' #666

Closed whitewizard333 closed 6 years ago

whitewizard333 commented 6 years ago

I am trying to convert TPOT binary classifier result into probability score using CalibratedClassifierCV.

Code is as below

**from tpot import TPOTClassifier from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split

from sklearn.calibration import CalibratedClassifierCV

tpot = TPOTClassifier(max_time_mins=6, verbosity=2) tpot.fit(titanic_x, titanic['Survived'])

clf_sigmoid = CalibratedClassifierCV(tpot, cv=2, method='sigmoid') clf_sigmoid.fit(titanic_x, titanic['Survived']) prob_pos_sigmoid = clf_sigmoid.predict_proba(titanic_test_x)[:,1]**

it throws the following error.

AttributeError Traceback (most recent call last)

in () 63 64 clf_sigmoid = CalibratedClassifierCV(tpot, cv=2, method='sigmoid') ---> 65 clf_sigmoid.fit(titanic_x, titanic['Survived']) 66 prob_pos_sigmoid = clf_sigmoid.predict_proba(titanic_test_x)[:,1] 67 print("tpot") /opt/conda/lib/python3.6/site-packages/sklearn/calibration.py in fit(self, X, y, sample_weight) 188 sample_weight[test]) 189 else: --> 190 calibrated_classifier.fit(X[test], y[test]) 191 self.calibrated_classifiers_.append(calibrated_classifier) 192 /opt/conda/lib/python3.6/site-packages/sklearn/calibration.py in fit(self, X, y, sample_weight) 333 Y = label_binarize(y, self.classes_) 334 --> 335 df, idx_pos_class = self._preproc(X) 336 self.calibrators_ = [] 337 /opt/conda/lib/python3.6/site-packages/sklearn/calibration.py in _preproc(self, X) 300 301 idx_pos_class = self.label_encoder_.\ --> 302 transform(self.base_estimator.classes_) 303 304 return df, idx_pos_class AttributeError: 'TPOTClassifier' object has no attribute 'classes_' Tpot should return best fitted pipeline class.
weixuanfu commented 6 years ago

I think you could change the line clf_sigmoid = CalibratedClassifierCV(tpot, cv=2, method='sigmoid') to clf_sigmoid = CalibratedClassifierCV(tpot.fitted_pipeline_, cv=2, method='sigmoid'). The attribute fitted_pipeline_ is scikit-learn Pipeline object which has this classes_ atrribute.

whitewizard333 commented 6 years ago

Yes, it's working with tpot.fittedpipeline Thank you 👍

flaviozamponi commented 5 years ago

Hi, what about the classes_ attribute for the tpot object? Is there a reason why it is missing? I am likely overlooking something but it would be helpful to have such an attribute. I am working with 13 classes and in the near future they will probably become many more: it would be good to know which element of the prediction array is what class. Is there a trick to otherwise know this?

Thanks in advance

weixuanfu commented 5 years ago

The classes_ attribute is only for TPOTClassifier class, but both TPOTRegessor and TPOTClassifier simply inherit from class TPOTbase. So we didn't add this attribute to TPOT object but expose fitted_pipeline_ attribute which is a scikit-learn Pipeline and should has the classes_ object for TPOTClassifier but not for TPOTRegressor.

ahmedafnouch816 commented 4 years ago

AttributeError: 'TPOTClassifier' object has no attribute '_optimized_pipeline'