将分类器保存到scikit-learn中的磁盘

如何将训练有素的朴素贝叶斯分类器保存到磁盘并用于预测数据?


我有来自scikit-learn网站的以下示例程序:


from sklearn import datasets

iris = datasets.load_iris()

from sklearn.naive_bayes import GaussianNB

gnb = GaussianNB()

y_pred = gnb.fit(iris.data, iris.target).predict(iris.data)

print "Number of mislabeled points : %d" % (iris.target != y_pred).sum()


一只名叫tom的猫
浏览 543回答 3
3回答

繁花如伊

分类器只是可以像其他任何东西一样被腌制和倾倒的对象。继续您的示例:import cPickle# save the classifierwith open('my_dumped_classifier.pkl', 'wb') as fid:    cPickle.dump(gnb, fid)    # load it againwith open('my_dumped_classifier.pkl', 'rb') as fid:    gnb_loaded = cPickle.load(fid)

鸿蒙传说

您还可以使用joblib.dump和joblib.load,它们在处理数字数组方面比默认的python pickler效率更高。Joblib包含在scikit-learn中:>>> from sklearn.externals import joblib>>> from sklearn.datasets import load_digits>>> from sklearn.linear_model import SGDClassifier>>> digits = load_digits()>>> clf = SGDClassifier().fit(digits.data, digits.target)>>> clf.score(digits.data, digits.target)  # evaluate training error0.9526989426822482>>> filename = '/tmp/digits_classifier.joblib.pkl'>>> _ = joblib.dump(clf, filename, compress=9)>>> clf2 = joblib.load(filename)>>> clf2SGDClassifier(alpha=0.0001, class_weight=None, epsilon=0.1, eta0=0.0,       fit_intercept=True, learning_rate='optimal', loss='hinge', n_iter=5,       n_jobs=1, penalty='l2', power_t=0.5, rho=0.85, seed=0,       shuffle=False, verbose=0, warm_start=False)>>> clf2.score(digits.data, digits.target)0.9526989426822482
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python