我已经最终确定了一个模型,并且它的性能在可接受的范围内。我专门使用 python 和 scitkit-learn 。
下一步是将模型投入生产。
我可以请求帮助将这些模型投入生产吗?如何保存经过训练的模型以便将其转移到生产环境。
提前感谢您的帮助。
最佳答案
正如评论者所建议的,您应该使用pickle
。特别是对于 ML,您正在寻找的是 Model persistence 。并使用 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
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')
关于python - 如何将火车模型投入生产?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46735954/