Here is my code which takes two files of positive and negative comments and creates a training and testing set for sentiment analysis using nltk, sklearn, Python and statistical algorithms. The accuracy varies between 70-80%.

How can I improve the accuracy of my code, specifically the part of the code where it is starred like **?

import nltk import random from nltk.corpus import movie_reviews from
nltk.classify.scikitlearn import SklearnClassifier import pickle

from sklearn.naive_bayes import MultinomialNB, BernoulliNB from
sklearn.linear_model import LogisticRegression, SGDClassifier from
sklearn.svm import SVC, LinearSVC, NuSVC

from nltk.classify import ClassifierI from statistics import mode

from nltk.tokenize import word_tokenize

class VoteClassifier(ClassifierI):

    def __init__(self, *classifiers):
        self._classifiers = classifiers

    def classify(self, features):
        votes = []
        for c in self._classifiers:
            v = c.classify(features)
        return mode(votes)

    def confidence(self, features):
        votes = []
        for c in self._classifiers:
            v = c.classify(features)

        choice_votes = votes.count(mode(votes))
        conf = choice_votes / len(votes)
        return conf

def createClassifier(posFileName, negFileName):
    short_pos = open(posFileName, "r").read()
    short_neg = open(negFileName, "r").read()

    documents = []

    for r in short_pos.split('\n'):
        documents.append((r, "pos"))
    posCount = len(documents)
    print("positive reviews", posCount)

    for r in short_neg.split('\n'):
        documents.append((r, "neg"))
    print("negative reviews", len(documents) - posCount)

    all_words = []

    short_pos_words = word_tokenize(short_pos)
    short_neg_words = word_tokenize(short_neg)

    for w in short_pos_words:

    for w in short_neg_words:

    **all_words = nltk.FreqDist(all_words)

    word_features = [w for (w, c) in all_words.most_common(5000)]

    def find_features(document):
        words = word_tokenize(document)
        features = {}
        for w in word_features:
            features[w] = (w in words)

        return features

    # print((find_features(movie_reviews.words('neg/cv000_29416.txt'))))

    featuresets = [(find_features(rev), category)
                   for (rev, category) in documents]


    # positive data example:
    lengthFeatureSet = len(featuresets)
    print("length feature set", lengthFeatureSet)
    if(lengthFeatureSet < 100):
        print("feature set must have atleast a 100 reviews")
        return -1
    # make this modifiable
    rangeTrainingSet = int(lengthFeatureSet * 0.85)
    training_set = featuresets[:rangeTrainingSet]
    testing_set = featuresets[rangeTrainingSet:]

    # negative data example:
    ##training_set = featuresets[100:]
    ##testing_set =  featuresets[:100]

    classifier = nltk.NaiveBayesClassifier.train(training_set)
    print("Original Naive Bayes Algo accuracy percent:",
          (nltk.classify.accuracy(classifier, testing_set)) * 100)

    MNB_classifier = SklearnClassifier(MultinomialNB())
    print("MNB_classifier accuracy percent:",
          (nltk.classify.accuracy(MNB_classifier, testing_set)) * 100)

    BernoulliNB_classifier = SklearnClassifier(BernoulliNB())
    print("BernoulliNB_classifier accuracy percent:",
          (nltk.classify.accuracy(BernoulliNB_classifier, testing_set)) * 100)

    LogisticRegression_classifier = SklearnClassifier(LogisticRegression())
    print("LogisticRegression_classifier accuracy percent:", (nltk.classify.accuracy(LogisticRegression_classifier,
                                                                                     testing_set)) * 100)

    SGDClassifier_classifier = SklearnClassifier(SGDClassifier())
    print("SGDClassifier_classifier accuracy percent:",
          (nltk.classify.accuracy(SGDClassifier_classifier, testing_set)) * 100)

    ##SVC_classifier = SklearnClassifier(SVC())
    # SVC_classifier.train(training_set)
    ##print("SVC_classifier accuracy percent:", (nltk.classify.accuracy(SVC_classifier, testing_set))*100)

    LinearSVC_classifier = SklearnClassifier(LinearSVC())
    print("LinearSVC_classifier accuracy percent:",
          (nltk.classify.accuracy(LinearSVC_classifier, testing_set)) * 100)

    NuSVC_classifier = SklearnClassifier(NuSVC())
    print("NuSVC_classifier accuracy percent:",
          (nltk.classify.accuracy(NuSVC_classifier, testing_set)) * 100)

    voted_classifier = VoteClassifier(

    print("voted_classifier accuracy percent:",
          (nltk.classify.accuracy(voted_classifier, testing_set)) * 100)
    save_classifier = open("23MNBCustom.pickle", "wb")
    pickle.dump(MNB_classifier, save_classifier)
    save_classifier = open("23BernoulliCustom.pickle", "wb")
    pickle.dump(BernoulliNB_classifier, save_classifier)
    save_classifier = open("23LogisticRegressionCustom.pickle", "wb")
    pickle.dump(LogisticRegression_classifier, save_classifier)
    save_classifier = open("23LinearCustom.pickle", "wb")
    pickle.dump(LinearSVC_classifier, save_classifier)
    save_classifier = open("23NuSVCCustom.pickle", "wb")
    pickle.dump(NuSVC_classifier, save_classifier)

    print("finished building and saving classifiers") createClassifier("positive.txt", "negative.txt")

closed as off-topic by IEatBagels, dfhwze, Toby Speight, VisualMelon, Heslacher Aug 15 at 12:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – IEatBagels, dfhwze, Toby Speight, VisualMelon, Heslacher
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Your imports seem messed up. Also your whole code is indented 3 spaces too much (fixed that) and there is a linebreak missing here: return conf def createClassifier(posFileName,negFileName): (fixed that). \$\endgroup\$ – Graipher Dec 20 '16 at 10:19
  • \$\begingroup\$ ok thanks I'ill post the gist is here in nicer format \$\endgroup\$ – gimp770 Dec 20 '16 at 19:08
  • \$\begingroup\$ gist.github.com/egimple/08e8737140cc93604ef2541d803e8987 \$\endgroup\$ – gimp770 Dec 20 '16 at 19:08
  • 2
    \$\begingroup\$ Note that the code included in the question is what counts. Do not expect anybody to actually have a look at any externally hosted code not included in the question. The code, as posted, also has to work-as-intended for it to be on-topic here. Have a look at the help center to see what exactly is on or off-topic. \$\endgroup\$ – Graipher Dec 20 '16 at 19:12
  • 2
    \$\begingroup\$ The imports are still messed up \$\endgroup\$ – IEatBagels Aug 14 at 17:36