python - 如何将火车模型投入生产?

标签 python machine-learning scikit-learn linear-regression

我已经最终确定了一个模型,并且它的性能在可接受的范围内。我专门使用 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/

相关文章:

r - 如何处理虚拟特征

python - 无需登录 django 即可安全地发布请求

pandas - ValueError MultinomialNB 的样本数量不一致错误

python - 如何在 Keras CNN 模型中预测单个图像?

python - 在 Scikit Learn 中运行 SelectKBest 后获取特征名称的最简单方法

python - 在 Scikit 中进行文本分类时是否需要标准化数据

python - 跨多行使用 str.contains

python - 比较 datetime.datetime 是否存在或无

python - Mysql查询结果插入一个L到记录中

python - CSR 格式的 scipy.sparse 矩阵是什么?