2017-10-30 2 views
2

我々は通常、次のような結果を得る: -Naive Bayes nltk pythonで最も有益な機能の割合はどのように計算されますか?私たちは、以下のコマンドを実行したときに

classifier.show_most_informative_features(10) 

結果:

Most Informative Features 
      outstanding = 1     pos : neg =  13.9 : 1.0 
       insulting = 1     neg : pos =  13.7 : 1.0 
       vulnerable = 1     pos : neg =  13.0 : 1.0 
       ludicrous = 1     neg : pos =  12.6 : 1.0 
      uninvolving = 1     neg : pos =  12.3 : 1.0 
       astounding = 1     pos : neg =  11.7 : 1.0 

を誰もが、この13.9、13.7などを計算する方法を知っていますか?

また、私たちはNaive Bayesを使った以下のメソッドclassifier.show_most_informative_features(10)で最も有益な機能を得ることができますが、ロジスティック回帰を使用して同じ結果を得たい場合は、誰かがそれを得る方法を提案することができます。私はstackoverflow上の1つのポストを見たが、それは私が機能を作成するために使用していないベクトルが必要です。

classifier = nltk.NaiveBayesClassifier.train(train_set) 
print("Original Naive bayes accuracy percent: ", nltk.classify.accuracy(classifier,dev_set)* 100) 
classifier.show_most_informative_features(10) 

LogisticRegression_classifier = SklearnClassifier(LogisticRegression()) 
LogisticRegression_classifier.train(train_set) 
print("LogisticRegression accuracy percent: ", nltk.classify.accuracy(LogisticRegression_classifier, dev_set)*100) 
+0

何か助けてください... ............................ – user3222101

答えて

1

the Naive Bayes classifier in NLTKのための最も有益な特徴は、次のような文書化されている:あなたの機能はユニグラムからあるバイナリ分類( 'NEG' VS 'POS')の場合、

def most_informative_features(self, n=100): 
    """ 
    Return a list of the 'most informative' features used by this 
    classifier. For the purpose of this function, the 
    informativeness of a feature ``(fname,fval)`` is equal to the 
    highest value of P(fname=fval|label), for any label, divided by 
    the lowest value of P(fname=fval|label), for any label: 
    | max[ P(fname=fval|label1)/P(fname=fval|label2) ] 
    """ 
    # The set of (fname, fval) pairs used by this classifier. 
    features = set() 
    # The max & min probability associated w/ each (fname, fval) 
    # pair. Maps (fname,fval) -> float. 
    maxprob = defaultdict(lambda: 0.0) 
    minprob = defaultdict(lambda: 1.0) 

    for (label, fname), probdist in self._feature_probdist.items(): 
     for fval in probdist.samples(): 
      feature = (fname, fval) 
      features.add(feature) 
      p = probdist.prob(fval) 
      maxprob[feature] = max(p, maxprob[feature]) 
      minprob[feature] = min(p, minprob[feature]) 
      if minprob[feature] == 0: 
       features.discard(feature) 

    # Convert features to a list, & sort it by how informative 
    # features are. 
    features = sorted(features, 
         key=lambda feature_: 
         minprob[feature_]/maxprob[feature_]) 
    return features[:n] 

bag-ワード(BOW)モデル、ワードoutstandingためmost_informative_features()関数によって返された「情報値は」に等しい:

p('outstanding'|'pos')/p('outstanding'|'neg') 

FUNCすべてのフィーチャを反復処理します(Unigram BoWモデルの場合、フィーチャは単語です)。次に最高の「情報値」を持つ上位100ワードを取ります。


タグgamma引数が0.5に設定されているフードの下LidstoneProbDistオブジェクトであり、それがないELEProbDistから予想される尤度推定を用いtrain() functionで計算された所定の単語の確率:

class LidstoneProbDist(ProbDistI): 
    """ 
    The Lidstone estimate for the probability distribution of the 
    experiment used to generate a frequency distribution. The 
    "Lidstone estimate" is parameterized by a real number *gamma*, 
    which typically ranges from 0 to 1. The Lidstone estimate 
    approximates the probability of a sample with count *c* from an 
    experiment with *N* outcomes and *B* bins as 
    ``c+gamma)/(N+B*gamma)``. This is equivalent to adding 
    *gamma* to the count for each bin, and taking the maximum 
    likelihood estimate of the resulting frequency distribution. 
    """ 
+0

おかげでalvas!ベクトルを使用しないskilearnロジスティック回帰でも動作するコードを知っていますか?よりよい結果を得るためにロジスティック回帰に役立つ言葉が何であるかを理解したかった – user3222101

関連する問題