2017-10-30 5 views
2

Wir in der Regel die folgenden Ergebnisse erhalten, wenn wir den folgenden Befehl ausführen: -Wie wird der informativste Feature-Prozentsatz in Naive Bayes Nltk Python berechnet?

classifier.show_most_informative_features(10) 

Ergebnisse:

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 

Hat jemand weiß, wie dies 13,9, 13,7 usw. berechnet wird?

Auch können wir die informativsten Funktionen mit der folgenden Methode classifier.show_most_informative_features (10) mit Naive Bayes erhalten, aber wenn wir die gleichen Ergebnisse mit logistischen Regression erhalten möchten, könnte bitte jemand den Weg vorschlagen, um das zu bekommen. Ich habe einen Post im Stackoverflow gesehen, aber dafür benötige ich einen Vektor, den ich nicht zum Erstellen von Features verwende.

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

Irgendwelche Hilfe? ............................ – user3222101

Antwort

1

Die informativsten Funktionen für the Naive Bayes classifier in NLTK als solche dokumentiert:

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] 

Im Fall der binären Klassifikation ('po' vs 'neg'), wo Ihre Funktionen von einem Unigramm- bag- sind der „Informationswert“ zurückgegeben von der most_informative_features() Funktion für die Wort-Wort (BoW) Modelle, outstanding ist gleich zu:

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

die func iteriert durch alle Funktionen (im Fall von Unigram-BoW-Modellen sind Features Wörter) und nehmen dann die obersten 100 Wörter mit dem höchsten "Informationswert".


Die Wahrscheinlichkeit eines des Tag gegebenen Wortes in den train() function mit der Expected-Likelihood-Schätzung von den ELEProbDist, die unter der Motorhaube ein LidstoneProbDist Objekt berechnet wird, wo das gamma Argument auf 0,5 eingestellt, und es tut:

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

Danke Alvas! Kennen Sie einen Code, der auch für die skilearn logistische Regression funktioniert, die keinen Vektor verwendet? Ich wollte verstehen, was die Wörter sind, die mehr zur logistischen Regression beitragen, um bessere Ergebnisse zu erzielen – user3222101

Verwandte Themen