更新scikit多项分类器

时间:2014-08-07 10:11:31

标签: python machine-learning nlp classification scikit-learn

我正在尝试使用新的训练数据更新scikit multinomial分类器。这是我试过的

from sklearn.feature_extraction.text import HashingVectorizer
import numpy as np
from sklearn.naive_bayes import MultinomialNB

# Training with first training set
targets = ['education','film','sports','laptops','phones']
x = ["football is the sport","gravity is the movie", "education is imporatant","lenovo is a laptop","android phones"]
y = np.array([2,1,0,3,4])
clf = MultinomialNB()
vectorizer = HashingVectorizer(stop_words='english', non_negative=True,
                                   n_features=32*2)
X_train = vectorizer.transform(x)
clf.partial_fit(X_train, y, classes=[0,1,2,3,4])

#Testing with First training set
test_data = ["android","lenovo","Transformers"]
X_test = vectorizer.transform(test_data)
print "Using Initial classifier"
pred = clf.predict(X_test)
for doc, category in zip(test_data, pred):
    print('%r => %s' % (doc, targets[category]))

# Training with updated training set
x = ["cricket", "Transformers is a film","which college"]
y = np.array([2,1,0])
X_train = vectorizer.transform(x)
clf.partial_fit(X_train, y)

# Testing with the updated trainign set
test_data = ["android","lenovo","Transformers"]
X_test = vectorizer.transform(test_data)
print "\nUsing Updatable classifiers"
pred = clf.predict(X_test)
for doc, category in zip(test_data, pred):
    print('%r => %s' % (doc, targets[category]))

输出到

Using Initial classifier
'android' => phones
'lenovo' => laptops
'Transformers' => education

Using Updatable classifiers
'android' => sports
'lenovo' => education
'Transformers' => film

我对此有两个问题 - >

1)" lenovo"的类别出现问题,因为更新分类器时不包括该类别的培训数据。有没有解决办法来避免这种情况。因为我不想每次更新分类器时都为每个类别提供训练数据。因此,即使我在更新时提供单个类别的数据,它也应该工作。

2)如何向现有分类器添加新类别。就像我想要一个像#34; health"到现有的分类器。那有没有办法做到这一点。

帮助表示赞赏。感谢

1 个答案:

答案 0 :(得分:1)

不是为第一批调用fit,而是调用partial_fit并将问题中的所有类列表作为classes参数:

clf.partial_fit(X, y, classes=targets)

(假设y实际上包含类标签而不是它们的索引。)

第一次致电partial_fit(或fit)后,您无法更改课程数量。您只需要预先了解课程数量或重新培训整个模型。