4

I have fitted a classification model using auto-sklearn, and managed to save it to a file with pickle.

x = automl.show_models()
results = {"ensemble": x}
pickle.dump(results, open('file.pickle','wb'))

I also have managed to reload the model.

automl = pickle.load(open('file.pickle','rb'))

But I can't manage to use the reloaded model to run predictions on new data. When I run:

y_hat = automl.predict(X_test)

I get the following error:

AttributeError: 'str' object has no attribute 'predict'
Joao
  • 41
  • 1
  • 2
  • you can try MLJAR AutoML https://github.com/mljar/mljar-supervised - it automatically save and load models, no need to directly call load/save – pplonski Mar 29 '21 at 08:48

2 Answers2

6

Incorrect:

x = automl.show_models()
results = {"ensemble": x} # <---
pickle.dump(results, open('file.pickle','wb'))

Correct:

x = automl.show_models()
#results = {"ensemble": x}
results = autml # the classifier/regressor itself
pickle.dump(results, open('file.pickle','wb'))

Sample code for Iris:

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from autosklearn.classification import AutoSklearnClassifier
import pickle


# dataset:
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

# train model:
classifier = AutoSklearnClassifier(
    time_left_for_this_task=30, 
    per_run_time_limit=60,
    memory_limit=1024*12) # depends on your computer
classifier.fit(X_train, y_train)

# save model
with open('iris-classifier.pkl', 'wb') as f:
    pickle.dump(classifier, f)

# load model
with open('iris-classifier.pkl', 'rb') as f:
    loaded_classifier = pickle.load(f)

# predict
y_true = y_test
y_pred = loaded_classifier.predict(X_test)
print('iris classifier: accuracy:', accuracy_score(y_true, y_pred))
# iris classifier: accuracy: 0.9333333333333333
蔡宗容
  • 927
  • 12
  • 9
2

Instead of dumping the output of automl.show_models(), I think you should dump the automl object instead, since this is the only class having the predict method.

xKobalt
  • 1,498
  • 2
  • 13
  • 19
widyarini
  • 21
  • 2