2017-01-01 11 views
0

私は問題を抱えていました。 私はbag-of-wordsアプローチを使用して1000のベストコレクションを選択しました。今では、品詞、平均単語長などに基づいて別の機能を使用したいと思います。どうすれば実現できますか? 私はPython、NLTK、Scikitパッケージを使用しています。これは私の最初のpythonプロジェクトですので、コードがあまり良くないかもしれません。事前にさまざまな種類の機能を組み合わせる(テキスト分類)

おかげで、

(実際には、一般的な分類作業のために良いアイデアだ)異なる種類の機能を組み合わせると間違って何もありません
import nltk 
    from nltk.corpus.reader import CategorizedPlaintextCorpusReader 
    from sklearn.feature_extraction.text import TfidfVectorizer 
    import os 
    import numpy as np 
    import random 
    import pickle 
    from time import time 
    from sklearn import metrics 

    from nltk.classify.scikitlearn import SklearnClassifier 
    from sklearn.naive_bayes import MultinomialNB,BernoulliNB 
    from sklearn.linear_model import LogisticRegression,SGDClassifier 
    from sklearn.svm import SVC, LinearSVC, NuSVC 

    import matplotlib.pyplot as plt 

    def intersect(a, b, c, d): 
     return list(set(a) & set(b)& set(c)& set(d)) 

    def find_features(document, feauture_list): 
     words = set(document) 
     features = {} 
     for w in feauture_list: 
      features[w] = (w in words) 
     return features 


    def benchmark(clf, name, training_set, testing_set): 

     print('_' * 80) 
     print("Training: ") 
     print(clf) 
     t0 = time() 
     clf.train(training_set) 
     train_time = time() - t0 
     print("train time: %0.3fs" % train_time) 

     t0 = time() 
     score = nltk.classify.accuracy(clf, testing_set)*100 
     #pred = clf.predict(testing_set) 
     test_time = time() - t0 

     print("test time: %0.3fs" % test_time) 

     print("accuracy: %0.3f" % score) 
     clf_descr = name 
     return clf_descr, score, train_time, test_time 

     #print((find_features(corpus.words('fantasy/1077-0_fantasy.txt'),feature_list))) 
    path = 'c:/data/books-Copy' 
    os.chdir(path) 
     #need this if you want to save tfidf_matrix 
    corpus = CategorizedPlaintextCorpusReader(path, r'.*\.txt', 
                cat_pattern=r'(\w+)/*') 
    save_featuresets = open(path +"/features_500.pickle","rb") 
    featuresets = [] 
    featuresets = pickle.load(save_featuresets) 
    save_featuresets.close() 

    documents = [(list(corpus.words(fileid)), category) 
       for category in corpus.categories() 
       for fileid in corpus.fileids(category)] 

    random.shuffle(documents) 

    tf = TfidfVectorizer(analyzer='word', min_df = 1, 
         stop_words = 'english', sublinear_tf=True) 
    #documents_tfidf = [] 
    top_features = [] 
    tf = TfidfVectorizer(input= 'filename', analyzer='word', 
         min_df = 1, stop_words = 'english', sublinear_tf=True) 

    for category in corpus.categories(): 
     files = corpus.fileids(category) 
     tf.fit_transform(files) 
     feature_names = tf.get_feature_names() 
     #documents_tfidf.append(feature_names) 
     indices = np.argsort(tf.idf_)[::-1] 
     top_features.append([feature_names[i] for i in indices[:10000]]) 
     #print(top_features_detective) 

    feature_list = list(set(top_features[0][:500]) | set(top_features[1][:500]) | 
         set(top_features[2][:500]) | set(top_features[3][:500]) | 
         set(intersect(top_features[0], top_features[1], top_features[2], top_features[3]))) 


    featuresets = [(find_features(rev, feature_list), category) for (rev, category) in documents] 
training_set = featuresets[:50] 
testing_set = featuresets[20:] 
results = [] 
for clf, name in (
          (SklearnClassifier(MultinomialNB()), "MultinomialNB"), 
          (SklearnClassifier(BernoulliNB()), "BernoulliNB"), 
          (SklearnClassifier(LogisticRegression()), "LogisticRegression"), 
          (SklearnClassifier(SVC()), "SVC"), 
          (SklearnClassifier(LinearSVC()), "Linear SVC "), 
          (SklearnClassifier(SGDClassifier()), "SGD ")): 
    print(name) 
    results.append(benchmark(clf, name, training_set, testing_set)) 

indices = np.arange(len(results)) 
results = [[x[i] for x in results] for i in range(4)] 

clf_names, score, training_time, test_time = results 
training_time = np.array(training_time)/np.max(training_time) 
test_time = np.array(test_time)/np.max(test_time) 



plt.figure(figsize=(12, 8)) 
plt.title("Score") 
plt.barh(indices, score, .2, label="score", color='navy') 
plt.barh(indices + .3, training_time, .2, label="training time", 
       color='c') 
plt.barh(indices + .6, test_time, .2, label="test time", color='darkorange') 
plt.yticks(()) 
plt.legend(loc='best') 
plt.subplots_adjust(left=.25) 
plt.subplots_adjust(top=.95) 
plt.subplots_adjust(bottom=.05) 

for i, c in zip(indices, clf_names): 
    plt.text(-15.6, i, c) 
    plt.show() 
+0

あなたの質問は何ですか?あなたが何を求めているのかは不明であり、たくさんのコードをダンプするだけでは役に立ちません。上記のほとんどはあなたの質問に無関係です。 –

+0

私は一度にあまりにも多くの質問を参照してください。一つを選び、[mcve]を使ってそれを聞いてください。その後、新しい質問に移動します(最初に試した後)。 –

+0

あなたの回答に感謝します。私はすでにFeatureUnionとPipelineを使って2つの異なるアルゴリズムを組み合わせました。パイプライン=パイプライン( ( 'text_features'、FeatureUnion [ ( 'vect'、vect)、#道路名からngramを抽出します) #( 'num_words'、Apply(lambda s:len(s.split()))) 、#平均単語長 ]))、 ( 'ave_word_length'、Apply(ラムダ:np.mean(s.split()))))、#平均ワード長])、 ( ' clf '、clf)、#出力を分類器 ]に送ります) –

答えて

0

。 NLTKのAPIでは、フィーチャーが辞書に入ってくることを期待しているので、フィーチャーコレクションを単一の辞書にまとめるだけで済みます。

これはあなたが質問した質問に対する回答です。あなたの助けが必要だが尋ねなかったコードに問題がある場合は、おそらく新しい質問を始めるべきです。

関連する問題