保存决策树模型python

nEO*_*nEO 6 python decision-tree

我正在使用Python中的scikit learn构建决策树.我已经在特定数据集上训练了模型,现在我想保存这个决策树,以便以后可以使用它(在新数据集上).任何人都知道如何做到这一点.

Mat*_*cer 5

摘自本教程的模型持久性部分:

可以使用 Python 的内置持久化模型(即pickle )将模型保存在 scikit 中:

>>> 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, degree=3, gamma=0.0,
  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])
array([0])
>>> y[0]
0
Run Code Online (Sandbox Code Playgroud)


Bas*_*hur 5

目前没有可靠的方法来做到这一点。虽然酸洗确实有效,但它还是不够好,因为不能保证您的酸洗数据可以通过更高版本的 scikit-learn 正确解压。

引自:http : //scikit-learn.org/stable/modules/model_persistence.html#security-maintainability-limitations

保存在一个版本的 scikit-learn 中的模型可能无法加载到另一个版本中。