朴素贝叶斯分类器(前十个字)

时间:2016-02-10 11:21:50

标签: python

我想列出每个类别的10个最可区分的单词,这些单词将类别与其他类别分开,并且alpha = 0.01将这些单词与类别中的主题相关联。我写了一些代码然后我被卡住了我不知道如何列出10个字。

   >>> import numpy as np
>>> import operator
>>> from sklearn import datasets, feature_extraction, naive_bayes, metrics, linear_model
>>> data_train = datasets.fetch_20newsgroups(subset = 'train', shuffle = True, random_state =
2016, remove = ('headers', 'footers', 'quotes'))
>>> data_test = datasets.fetch_20newsgroups(subset = 'test', shuffle = True, random_state =
2016, remove = ('headers', 'footers', 'quotes'))
>>> categories = data_train.target_names
>>> target_map = {}
>>> for i in range(len(categories)):
    if 'comp.' in categories[i]:
        target_map[i] = 0
    elif 'rec.' in categories[i]:
        target_map[i] = 1
    elif 'sci.' in categories[i]:
        target_map[i] = 2
    elif 'misc.forsale' in categories[i]:
        target_map[i] = 3
    elif 'talk.politics' in categories[i]:
        target_map[i] = 4
    else:
        target_map[i] = 5


>>> y_temp = data_train.target
>>> y_train = []
>>> for y in y_temp:
    y_train.append(target_map[y])


>>> y_temp = data_test.target
>>> y_test = []
>>> for y in y_temp:
    y_test.append(target_map[y])


>>> count_vectorizer = feature_extraction.text.CountVectorizer(min_df = 0.01, max_df = 0.5, stop_words = 'english')
>>> x_train = count_vectorizer.fit_transform(data_train.data)
>>> 
KeyboardInterrupt
>>> x_test = count_vectorizer.transform(data_test.data)
>>> mnb = naive_bayes.MultinomialNB(alpha = 0.01)
>>> mnb.fit(x_train, y_train)
MultinomialNB(alpha=0.01, class_prior=None, fit_prior=True)
>>> y_pred = mnb.predict(x_test)
>>> print('accuracy of Multinomial Naive Bayes: ', metrics.accuracy_score(y_test, y_pred))
accuracy of Multinomial Naive Bayes:  0.692910249602
>>> mnb = naive_bayes.MultinomialNB(alpha = 0.001)
>>> mnb.fit(x_train, y_train)
MultinomialNB(alpha=0.001, class_prior=None, fit_prior=True)
>>>  y_pred = mnb.predict(x_test)

SyntaxError: unexpected indent
>>> y_pred = mnb.predict(x_test)
>>>  print('accuracy of Multinomial Naive Bayes: ', metrics.accuracy_score(y_test, y_pred))

SyntaxError: unexpected indent
>>> print('accuracy of Multinomial Naive Bayes: ', metrics.accuracy_score(y_test, y_pred))
accuracy of Multinomial Naive Bayes:  0.692379182156
>>> mnb = naive_bayes.MultinomialNB(alpha = 0.1)
>>> mnb.fit(x_train, y_train)
MultinomialNB(alpha=0.1, class_prior=None, fit_prior=True)
>>> y_pred = mnb.predict(x_test)
>>> print('accuracy of Multinomial Naive Bayes: ', metrics.accuracy_score(y_test, y_pred))
accuracy of Multinomial Naive Bayes:  0.692379182156
>>> mnb = naive_bayes.MultinomialNB(alpha = 1)
>>> mnb.fit(x_train, y_train)
MultinomialNB(alpha=1, class_prior=None, fit_prior=True)
>>> y_pred = mnb.predict(x_test)
>>> print('accuracy of Multinomial Naive Bayes: ', metrics.accuracy_score(y_test, y_pred))
accuracy of Multinomial Naive Bayes:  0.691848114711
>>> mnb = naive_bayes.MultinomialNB(alpha = 10)
>>> mnb.fit(x_train, y_train)
MultinomialNB(alpha=10, class_prior=None, fit_prior=True)
>>> y_pred = mnb.predict(x_test)
>>> print('accuracy of Multinomial Naive Bayes: ', metrics.accuracy_score(y_test, y_pred))
accuracy of Multinomial Naive Bayes:  0.686537440255

1 个答案:

答案 0 :(得分:0)

count_vectorizer = feature_extraction.text.CountVectorizer(min_df = 0.01, max_df = 0.5, stop_words = 'english')
x_train = count_vectorizer.fit_transform(data_train.data)
x_test = count_vectorizer.transform(data_test.data)
acc=[]
i=0
rr=[0.001,0.01,0.1,1,10]
for alp in [0,1,2,3,4]:
    mnb = naive_bayes.MultinomialNB(alpha = alp)
    mnb.fit(x_train, y_train)
    y_pred = mnb.predict(x_test)
    print('accuracy of Multinomial Naive Bayes for alpha ',rr[alp],'=', metrics.accuracy_score(y_test, y_pred))
    acc.append(metrics.accuracy_score(y_test, y_pred))


import operator
pos,m = max(enumerate(acc), key=operator.itemgetter(1))
print("Max accuracy=",m," for alpha=",rr[pos])

for ss in [0,1,2,3,4,5]:
    mnb = naive_bayes.MultinomialNB(alpha = rr[pos])
    mnb.fit(x_train, y_train)
    y_pred = mnb.predict(x_test)

    acc[alp]=metrics.accuracy_score(y_test, y_pred)
    feature_names = count_vectorizer.get_feature_names()
    diff = mnb.feature_log_prob_[ss,:] - np.max(mnb.feature_log_prob_[-ss:])

    name_diff = {}
    for i in range(len(feature_names)):
       name_diff[feature_names[i]] = diff[i]

       names_diff_sorted = sorted(name_diff.items(), key = op.itemgetter(1), reverse = True)
    for i in range(10):
       print(ss,names_diff_sorted[i])