自动机器学习 python 等效代码

val*_*tin 5 python scikit-learn automl tpot

有没有办法从 auto-sklearn 的独立 python 脚本中提取自动生成的机器学习管道?

以下是使用 auto-sklearn 的示例代码:

import autosklearn.classification
import sklearn.cross_validation
import sklearn.datasets
import sklearn.metrics

digits = sklearn.datasets.load_digits()
X = digits.data
y = digits.target
X_train, X_test, y_train, y_test = sklearn.cross_validation.train_test_split(X, y, random_state=1)

automl = autosklearn.classification.AutoSklearnClassifier()
automl.fit(X_train, y_train)
y_hat = automl.predict(X_test)

print("Accuracy score", sklearn.metrics.accuracy_score(y_test, y_hat))
Run Code Online (Sandbox Code Playgroud)

以某种方式自动生成等效的 python 代码会很好。

相比之下,当使用 TPOT 时,我们可以获得如下的独立管道:

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

digits = load_digits()
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, train_size=0.75, test_size=0.25)

tpot = TPOTClassifier(generations=5, population_size=20, verbosity=2)
tpot.fit(X_train, y_train)

print(tpot.score(X_test, y_test))

tpot.export('tpot-mnist-pipeline.py')
Run Code Online (Sandbox Code Playgroud)

在检查tpot-mnist-pipeline.py整个 ML 管道时可以看到:

import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline

# NOTE: Make sure that the class is labeled 'class' in the data file
tpot_data = np.recfromcsv('PATH/TO/DATA/FILE', delimiter='COLUMN_SEPARATOR')
features = tpot_data.view((np.float64, len(tpot_data.dtype.names)))
features = np.delete(features, tpot_data.dtype.names.index('class'), axis=1)
training_features, testing_features, training_classes, testing_classes =     train_test_split(features, tpot_data['class'], random_state=42)

exported_pipeline = make_pipeline(
    KNeighborsClassifier(n_neighbors=3, weights="uniform")
)

exported_pipeline.fit(training_features, training_classes)
results = exported_pipeline.predict(testing_features)
Run Code Online (Sandbox Code Playgroud)

上面的示例与此处找到的有关自动化有点浅的机器学习的现有帖子有关。

小智 4

没有自动化的方法。您可以将对象存储为 pickle 格式并稍后加载。

with open('automl.pkl', 'wb') as output:
    pickle.dump(automl,output)
Run Code Online (Sandbox Code Playgroud)

您可以调试拟合或预测方法并查看发生了什么。