无法从Naive Bayes分类器

时间:2015-10-20 05:43:43

标签: python-2.7 matplotlib machine-learning scikit-learn roc

我试图使用从某些变量派生的特征来预测种族。从我之前的问题How to interpret this triangular shape ROC AUC curve?开始,我学会了使用decision_function或predict_proba而不是实际预测来拟合ROC曲线。

我可以使用以下代码使用SVM分类器

生成ROC-AUC图
# coding=utf-8
import pandas as pd
from pandas import DataFrame, Series
import numpy as np
import nltk
import re
import random
from random import randint
import csv
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

from sklearn.metrics import classification_report
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction import DictVectorizer
from sklearn.metrics import confusion_matrix as sk_confusion_matrix
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt

# multi_class : str, {'ovr', 'multinomial'}
#$$
lr = LogisticRegression()
#lr = LogisticRegression(penalty='l2', class_weight='auto', solver='lbfgs', multi_class='multinomial')
nb = MultinomialNB(fit_prior=False)
#$$
svm = LinearSVC(class_weight='auto')

dv = DictVectorizer()

# Get csv file into data frame
data = pd.read_csv("FamilySearchData_All_OCT2015_newEthnicity_filledEthnicity_processedName_trimmedCol.csv", header=0, encoding="utf-8")
df = DataFrame(data)

# Class list
ethnicity2 = ['fr', 'en', 'ir', 'sc', 'others', 'ab', 'rus', 'ch', 'it', 'ja']
Ab_group = ['fr', 'en', 'ir', 'sc', 'others', 'ab', 'rus', 'ch', 'it', 'ja', 'fn', 'metis', 'inuit']
Ab_lang = ['fr', 'en', 'ir', 'sc', 'others', 'ab', 'rus', 'ch', 'it', 'ja', 'x', 'y']

############################################
########## CONTROL ROOM ####################
# change-tag: '#$$'
# Output file name decoration
# Total N = 5031794
#$$
featureUsed = 8
#$$
subsample_size = 50000
#$$
ethnicity_var = 'ethnicity2' # Ab_group, Ab_tribe, Ab_lang
count = 0

# Declaration
print 'No. features=', featureUsed
print 'N=', subsample_size, 'Training_N=', subsample_size/2, 'Test_N=', subsample_size/2
print 'ethnicity_var:', ethnicity_var
#$$
print ethnicity2
#$$
print 'ML classifier:', 'svm = LinearSVC(class_weight=\'auto\')'
print ''
print '//////////////////////////////////////////////////////'
print ''

try:
    #$$
    for i in ethnicity2:
        count+=1
        ethnicity_tar = str(i) # fr, en, ir, sc, others, ab, rus, ch, it, ja
        # fn, metis, inuit; algonquian, iroquoian, athapaskan, wakashan, siouan, salish, tsimshian, kootenay
        ############################################
        ############################################

        def ethnicity_target(row):
            try:
                if row[ethnicity_var] == ethnicity_tar:
                    return 1
                else:
                    return 0
            except: return None
        df['ethnicity_scan'] = df.apply(ethnicity_target, axis=1)
        print '1=', ethnicity_tar
        print '0=', 'non-'+ethnicity_tar

        # Random sampling a smaller dataframe for debugging
        rows = random.sample(df.index, subsample_size)
        df = df.ix[rows] # Warning!!!! overwriting original df
        print 'Class count:'
        print df['ethnicity_scan'].value_counts()

        # Assign X and y variables
        X = df.raw_name.values
        y = df.ethnicity_scan.values

        # Feature extraction functions
        def feature_full_name(nameString):
            #... codes omitted

        # Transform format of X variables, and spit out a numpy array for all features
        my_dict = [{'last-name': feature_full_last_name(i)} for i in X]
        my_dict2 = [list_to_dict(feature_twoLetters(feature_full_last_name(i))) for i in X]
        my_dict3 = [list_to_dict(feature_threeLetters(feature_full_last_name(i))) for i in X]
        my_dict4 = [list_to_dict(feature_fourLetters(feature_full_last_name(i))) for i in X]

        my_dict5 = [{'first-name': feature_full_first_name(i)} for i in X]
        my_dict6 = [list_to_dict(feature_twoLetters(feature_full_first_name(i))) for i in X]
        my_dict7 = [list_to_dict(feature_threeLetters(feature_full_first_name(i))) for i in X]
        my_dict8 = [list_to_dict(feature_fourLetters(feature_full_first_name(i))) for i in X]

        all_dict = []
        for i in range(0, len(my_dict)):
            temp_dict = dict(my_dict[i].items() + my_dict2[i].items() + my_dict3[i].items() + my_dict4[i].items()
                + my_dict5[i].items() + my_dict6[i].items() + my_dict7[i].items() + my_dict8[i].items())
            all_dict.append(temp_dict)

        newX = dv.fit_transform(all_dict)

        # Separate the training and testing data sets
        half_cut = int(len(df)/2.0)*-1
        X_train = newX[:half_cut]
        X_test = newX[half_cut:]
        y_train = y[:half_cut]
        y_test = y[half_cut:]

        # Fitting X and y into model, using training data
        #$$
        svm.fit(X_train, y_train)

        # Making predictions using trained data
        #$$
        y_train_predictions = svm.predict(X_train)
        #$$
        y_test_predictions = svm.predict(X_test)

        #print (y_train_predictions == y_train).sum().astype(float)/(y_train.shape[0])
        print 'Accuracy:',(y_test_predictions == y_test).sum().astype(float)/(y_test.shape[0])

        print 'Classification report:'
        print classification_report(y_test, y_test_predictions)
        #print sk_confusion_matrix(y_train, y_train_predictions)
        print 'Confusion matrix:'
        print sk_confusion_matrix(y_test, y_test_predictions)

        #print y_test[1:20]
        #print y_test_predictions[1:20]

        #print y_test[1:10]
        #print np.bincount(y_test)
        #print np.bincount(y_test_predictions)

        # Find and plot AUC
        false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_test_predictions)
        roc_auc = auc(false_positive_rate, true_positive_rate)

        # Find and plot AUC
        y_score = svm.fit(X_train, y_train).decision_function(X_test)
        false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_score)
        roc_auc = auc(false_positive_rate, true_positive_rate)
        print 'AUC-'+ethnicity_tar+'=',roc_auc

        # Get different color each graph line
        colorSet = ['navy', 'greenyellow', 'deepskyblue', 'darkviolet', 'crimson', 
            'darkslategray', 'indigo', 'brown', 'orange', 'palevioletred', 'mediumseagreen',
            'k', 'darkgoldenrod', 'g', 'midnightblue', 'c', 'y', 'r', 'b', 'm', 'lawngreen'
            'mediumturquoise', 'lime', 'teal', 'drive', 'sienna', 'sandybrown']
        color = colorSet[count-1]

        # Plotting
        plt.title('ROC')
        plt.plot(false_positive_rate, true_positive_rate, c=color, label=('AUC-'+ethnicity_tar+'= %0.2f'%roc_auc))
        plt.legend(loc='lower right', prop={'size':8})
        plt.plot([0,1],[0,1], color='lightgrey', linestyle='--')
        plt.xlim([-0.05,1.0])
        plt.ylim([0.0,1.05])
        plt.ylabel('True Positive Rate')
        plt.xlabel('False Positive Rate')
        #plt.show()
        # Save ROC graphs
        plt.savefig('TESTROCXXX.jpg')

        print ''
        print '//////////////////////////////////////////////////////'
        print ''
except Exception as e:
    print 'Error:', str(e)
    print ''
    print '//////////////////////////////////////////////////////'
    print ''

给出了:

enter image description here

但是当我尝试使用Naive Bayes分类器时,我做了以下更改:

nb.fit(X_train, y_train) # from svm.fit(X_train, y_train)

y_train_predictions = nb.predict(X_train) # from y_train_predictions = svm.predict(X_train)

y_test_predictions = nb.predict(X_test) # from y_test_predictions = svm.predict(X_test)

y_score = nb.fit(X_train, y_train).predict_proba(X_test) # from y_score = svm.fit(X_train, y_train).decision_function(X_test)

但是我收到了错误:

Error: bad input shape (25000L, 2L)

编辑:按建议添加[:,1]后,我显示了4个ROC图,最后两个是NB,看起来很奇怪。

enter image description here

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我忘了在这个https://stackoverflow.com/a/33218642/1030820回答中提到你需要选择一些列(从可能的两个),以防你使用roc_curve的predict_proba结果。

false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_score[:,1])

这可能有效。

添加:嗯,它的朴素贝叶斯,在大多数情况下它不应该击败LR。它比LR模型更简单,并且无法捕捉到特征之间的相互作用(顺便说一句,这就是为什么它被称为朴素)。在ML论文中,作者经常使用NB只是为了在准确性方面做出一些起点,展示最简单的ML算法的结果,并将更高级的算法与它进行比较。

另见:http://scikit-learn.org/stable/modules/naive_bayes.html#naive-bayes

  

另一方面,虽然天真的贝叶斯被称为体面   分类器,它被认为是一个不好的估计,所以概率   来自predict_proba的输出不应过于严肃。

相关问题