naive bayes classifier dynamic training

做~自己de王妃 提交于 2021-02-05 07:43:17

问题


Is it possible (and how if it is) to dynamically train sklearn MultinomialNB Classifier? I would like to train(update) my spam classifier every time I feed an email in it.

I want this (does not work):

x_train, x_test, y_train, y_test = tts(features, labels, test_size=0.2)
clf = MultinomialNB()
for i in range(len(x_train)):
    clf.fit([x_train[i]], [y_train[i]])
preds = clf.predict(x_test)

to have similar result as this (works OK):

x_train, x_test, y_train, y_test = tts(features, labels, test_size=0.2)
clf = MultinomialNB()
clf.fit(x_train, y_train)
preds = clf.predict(x_test)

回答1:


Scikit-learn supports incremental learning for multiple algorithms, including MultinomialNB. Check the docs here

You'll need to use the method partial_fit() instead of fit(), so your example code would look like:

x_train, x_test, y_train, y_test = tts(features, labels, test_size=0.2)
clf = MultinomialNB()
for i in range(len(x_train)):
    if i == 0:
        clf.partial_fit([x_train[i]], [y_train[I]], classes=numpy.unique(y_train))
    else:
        clf.partial_fit([x_train[i]], [y_train[I]])
preds = clf.predict(x_test)

Edit: added the classes argument to partial_fit, as suggested by @BobWazowski



来源:https://stackoverflow.com/questions/62023963/naive-bayes-classifier-dynamic-training

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!