如何将火车模型投入生产? [英] How to move the train model to production?
问题描述
我已经确定了一个模型,并且该模型在可接受的范围内运行.我专门使用python和scitkit-learn.
I have finalized a model and it is performing within acceptable limits. I am using python and scitkit-learn specifically.
下一步是将模型投入生产.
Next is to move the model to production.
我可以要求帮助将这些模型投入生产.如何保存经过训练的模型,以便可以将其移至生产环境.
May I request help to move these models to production. How can I save a trained model in such a way that I can move it to production.
预先感谢您的帮助.
推荐答案
如评论员所建议,您应使用pickle
.专门针对ML,您需要的是模型持久性.并使用scikit-learn:
As the commentor suggested, you should use pickle
. Specifically for ML, what you're looking for is Model persistence. And with scikit-learn:
在训练了scikit-learn模型之后,希望有一种方法可以持久保存该模型以供将来使用,而不必重新训练.
After training a scikit-learn model, it is desirable to have a way to persist the model for future use without having to retrain.
以及他们的示例:
>>> from sklearn import svm
>>> from sklearn import datasets
>>> clf = svm.SVC()
>>> iris = datasets.load_iris()
>>> X, y = iris.data, iris.target
>>> clf.fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
>>> import pickle
>>> s = pickle.dumps(clf)
>>> clf2 = pickle.loads(s)
>>> clf2.predict(X[0:1])
array([0])
>>> y[0]
0
在scikit的特定情况下,使用joblib代替pickle(
joblib.dump
&joblib.load
)可能会更有趣,这在内部通常携带大型numpy数组的对象上更有效适用于适合的scikit-learn估计器,但只能在磁盘上腌制而不能在字符串上腌制:
In the specific case of the scikit, it may be more interesting to use joblib’s replacement of pickle (
joblib.dump
&joblib.load
), which is more efficient on objects that carry large numpy arrays internally as is often the case for fitted scikit-learn estimators, but can only pickle to the disk and not to a string:
>>> from sklearn.externals import joblib
>>> joblib.dump(clf, 'filename.pkl')
这篇关于如何将火车模型投入生产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!