本文整理汇总了Python中textblob.classifiers.NaiveBayesClassifier.accuracy方法的典型用法代码示例。如果您正苦于以下问题:Python NaiveBayesClassifier.accuracy方法的具体用法?Python NaiveBayesClassifier.accuracy怎么用?Python NaiveBayesClassifier.accuracy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类textblob.classifiers.NaiveBayesClassifier
的用法示例。
在下文中一共展示了NaiveBayesClassifier.accuracy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
class ExpenseClassifier:
def __init__(self):
training_data = self._load_data("data")
self.category_classifier = NaiveBayesClassifier([(x[0], x[1]) for x in training_data])
self.avoidability_classifier = NaiveBayesClassifier([(x[0], x[2]) for x in training_data])
self.ordinary_classifier = NaiveBayesClassifier([(x[0], x[3]) for x in training_data])
def classify(self, description):
res = {}
res['category'] = self.category_classifier.classify(description)
res['avoidable'] = self.avoidability_classifier.classify(description)
res['ordinary'] = self.ordinary_classifier.classify(description)
return res
def accuracy(self):
test_data = self._load_data("test")
res = {}
res['category'] = self.category_classifier.accuracy([(x[0], x[1]) for x in test_data])
res['avoidable'] = self.avoidability_classifier.accuracy([(x[0], x[2]) for x in test_data])
res['ordinary'] = self.ordinary_classifier.accuracy([(x[0], x[3]) for x in test_data])
return res
def _load_data(self, folder):
data = []
for f in glob.glob(folder + "/*.csv"):
with open(f) as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
if row[DESCRIPTION] and row[CATEGORY] and row[AVOIDABLE] and row[ORDINARY]:
data.append((norm(row[DESCRIPTION]), row[CATEGORY], row[AVOIDABLE], row[ORDINARY]))
return data
示例2: train_n_test
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
def train_n_test(file_path):
documents= load_data(file_path)
random.shuffle(documents)
generate_bigrams(data.wordlist)
train = documents[0:110]
test = documents[110:]
#classifier = NaiveBayesClassifier(train)
#classifier = NaiveBayesClassifier(train,feature_extractor=get_features)
classifier = NaiveBayesClassifier(train,feature_extractor=get_feats)
print classifier.accuracy(test)
示例3: generate_model
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
def generate_model(self):
print("Gathering and processing tweets...")
# Shuffle list of username-label tuples
tuple_list = usermapping.data_tuples.items()
# Split and grab tweets for users
results = utils.flatten([ self.fetch_data(t)
for t in tuple_list ])
# TODO: Cross-validation generation
trn_ratio = int(len(results) * 0.85)
shuffle(results)
print(len(results))
print(trn_ratio)
train = results[:trn_ratio]
test = results[trn_ratio:]
# Instantiate and train classifier
print("Training...")
cl = NaiveBayesClassifier(train)
cl.train()
# Save model
print("Saving model...")
utils.save_model(cl)
# Classify test
print("Testing...")
print("Accuracy: {0}".format(cl.accuracy(test)))
return cl
示例4: create_sentiment
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
def create_sentiment():
"""
Train sentiment model and save.
Input type: None
Output: Model as pickle
"""
random.seed(1)
test = [
("The dude presenting Unravel seems like one of the most genuine game developers Ive ever seen I really hope this game works out for him",'pos'),
("His hands are shaking Dude looks so stoked and scared at the same time",'pos'),
("Right I just felt like I was watching his dream come true It was nice The game looks very well done as well Good for him",'pos'),
("Seriously Unravel looks really good actually and honestly seeing him so happy about what hes made is contagious I want to see more of Unravel ",'pos'),
("He was so nervous shaking all over his voice quivering",'neg'),
("The game looked nice too very cute art style ",'pos'),
("You could tell he genuinely wanted to be there it looked like he was even shaking from the excitement I hope it works out for them aswell",'pos'),
("However following that up with the weird PvZ thing was odd To say the least",'neg'),
("Haha The game did look nice though Im definitely going to keep an eye on it I enjoy supporting such hopeful developers",'pos'),
("Very personable This looks like a buy for me As a dev in a other sector I appreciate this passion",'pos'),
("I want to give him a cookie",'pos'),
("Im getting a copy Im gonna support my indie devs",'pos'),
("The twitch leak was accurate It was like a play by play you start speaking French then switch to English",'neg'),
("yep exactly what i was thinking lol its important to note that the twitch leak never had them saying it was Dishonored 2 but that they were honored to be here very different",'neg'),
("Honored Im 100 sure that was intentional",'neg'),
("oh yea for sure but wasnt solid enough evidence imo to be like dishonored 2 confirmed just based off that",'neg'),
("The confirmation was who was talking not what they were talking about ",'neg'),
("How awkward is it for a pop singer to perform at a video game conference",'neg'),
("Oh god did they warn him that he will get zero reaction",'neg'),
("I really hope so",'pos'),
("Almost as bad as Aisha fucking up her dialogue constantly Shes doing alright though E3 is really becoming a mainstream media event Hollywood has nothing like this ComicCon is the only comparison and they dont dazzle it up like E3",'neg')
]
# Grab review data
reviews = [
(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)
]
random.shuffle(reviews)
# Divide into 10% train/test splits
new_train, new_test = reviews[:1900], reviews[1900:]
# Train the NB classifier on the train split
cl = NaiveBayesClassifier(new_train)
# Compute accuracy
accuracy = cl.accuracy(test + new_test)
print("Accuracy: {0}".format(accuracy))
# Show 5 most informative features
cl.show_informative_features(5)
# Save model for use in creating social model sentiment
with open('sentiment_clf_full.pkl', 'wb') as pk:
pickle.dump(cl, pk)
print 'done saving model'
示例5: main
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
def main():
data =[]
train =[]
test =[]
with open('hellopeter_labelled.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
spamreader = list(spamreader)
for row in spamreader:
if (row[13] =='strongly positive'):
data.append((row[8],'pos'))
if (row[13] =='positive' ):
data.append((row[8],'pos'))
if ( row[13] =='neutral' ):
data.append((row[8],'neu'))
if ( row[13] =='negative'):
data.append((row[8],'neg'))
if (row[13] =='strongly negative' ):
data.append((row[8],'neg'))
train = data[:1000]
test = data[1001:]
for innf in test:
print innf
cl = NaiveBayesClassifier(train)
for tnew in test:
print '%%%%%%%'
print ' '
print tnew[0]
print tnew[1]
print '%%%%%%%'
print '#######'
cl.classify(tnew[0])
prob_class = cl.prob_classify(tnew[0])
print '----max prob---'
print prob_class.max()
print '-----+ve-----'
print prob_class.prob("pos")
print '-----neutral-----'
print prob_class.prob("neu")
print '------ve-----'
print prob_class.prob("neg")
cl.accuracy(test)
示例6: LanguageDetector
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
class LanguageDetector(object):
def __init__(self, train=SAMPLE_TRAIN, feature_extractor=FeatureExtractors.last_word_extractor()):
self.train = train
self.classifier = NaiveBayesClassifier(self.train, feature_extractor)
def accuracy(self, test_set=SAMPLE_TEST):
return self.classifier.accuracy(test_set)
def show_features(self):
return self.classifier.show_informative_features(5)
示例7: run_test
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
def run_test(train, test, name):
print "Training..."
cll = NaiveBayesClassifier(train)
print "Done training\n"
accuracy = cll.accuracy(test)
print "Accuracy: " + str(accuracy)
# get matching lists of predicted and true labels
pred_labels = list()
true_labels = list()
for obj in test:
prob_label = cll.prob_classify(obj[0]).max()
true_label = obj[1]
true_labels.append(true_label)
pred_labels.append(prob_label)
# transform our labels to numbers
labels = cll.labels()
i = 0
label_num = dict()
for label in labels:
label_num[label] = i
i = i + 1
# match our predicted and true labels with the number representations
true_label_nums = list()
pred_label_nums = list()
for true_l, pred_l in zip(true_labels, pred_labels):
true_label_nums.append(label_num[true_l])
pred_label_nums.append(label_num[pred_l])
cm = confusion_matrix(true_label_nums, pred_label_nums)
print cm
print "\n"
with open("test_results.txt", "a") as tr:
tr.write(str(name) + "\n")
tr.write(str(accuracy) + "\n")
tr.write(str(cm))
tr.write("\n\n")
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(cm)
plt.title("Confusion Matrix For "+name)
fig.colorbar(cax)
ax.set_xticklabels(['']+labels)
ax.set_yticklabels(['']+labels)
plt.xlabel("Predicted")
plt.ylabel("True")
plt.savefig('plots/'+name+'.pdf', bbox_inches='tight')
示例8: main
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
def main():
print "This is Naive Bayes' Classifier..."
#read training data
#training_data = open("training_data").readlines()
training_data = open("training_data_final").readlines()
#load training data
training_tuples = loadData(training_data)
training_tuples_api = make_api_tuples(training_tuples)
print training_tuples_api
#display tuples
#for t in training_tuples:
# t.show()
#gather classes
classes = filterClasses(training_tuples)
#print "classes = ", classes
#gather vocab
vocab = getVocab(training_tuples)
#print vocab
#generate prior
prior = generatePrior(training_tuples, classes)
#print prior
#generate likelihood
likelihood = generateLikelihood(training_tuples, vocab, classes)
#print likelihood
#read test data
#test_data = open("test_data").readlines()
test_data = open("test_data_final").readlines()
#load test data
test_tuples = loadData(test_data)
test_tuples_api = make_api_tuples(test_tuples)
#calculate C-MAP
posterior = predict(test_tuples, classes, prior, likelihood)
showResults(training_data, test_data, posterior)
#calculate accuracy
evaluateAccuracy(test_tuples, posterior)
#Naive Bayes API
cl = NaiveBayesClassifier(training_tuples_api)
# Compute accuracy
print("Accuracy: {0}".format(cl.accuracy(test_tuples_api)))
示例9: train
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
def train(pos_examples, neg_examples, train_fraction=0.6):
"""Train a classifier, holding out train_fraction of pos_examples and neg_examples as a test set.
Return the tuple:
(the classifier, accuracy, positive test example list, negative test example list, )
"""
pos_split = int(train_fraction * len(pos_examples))
pos_train, pos_test = pos_examples[0:pos_split], pos_examples[pos_split:]
neg_split = int(train_fraction * len(neg_examples))
neg_train, neg_test = neg_examples[0:neg_split], neg_examples[neg_split:]
cl = NaiveBayesClassifier(pos_train + neg_train)
return cl, cl.accuracy(pos_test + neg_test), pos_test, neg_test
示例10: create_sentiment_model
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
def create_sentiment_model():
random.seed(1)
# Grab some movie review data
reviews = [(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]
random.shuffle(reviews)
new_train, new_test = reviews[:1900], reviews[1900:]
cl = NaiveBayesClassifier(new_train)
# Compute accuracy
accuracy = cl.accuracy(new_test)
print("Accuracy: {0}".format(accuracy))
# Show 5 most informative features
print cl.show_informative_features(5)
with open('sentiment_clf_full.pkl', 'wb') as pk:
dill.dump(cl, pk)
print 'done saving model'
示例11: open
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
with open(name) as f:
text = f.read()
text = text.replace("\n", " ")
text = unicode(text, "utf-8", errors="ignore")
data.append((text, "pro"))
i += 1
files = glob.glob(NonPropath)
for name in files:
with open(name) as f:
text = f.read()
text = text.replace("\n", " ")
text = unicode(text, "utf-8", errors="ignore")
data.append((text, "non-pro"))
random.shuffle(data)
number_of_elements = len(data)
split = (number_of_elements / 3) * 2
train = data[:split]
test = data[split:]
# print 'content of line 5 ' , train[4]
cl = NaiveBayesClassifier(train)
cl.accuracy(test)
cl.classify(
"Your symptoms may be caused due to a musculo-skeletal strain. I would advise you to take OTC pain-killers/NSAIDS and see if it helps. Rest and ice will also help to relieve the symptoms. If the pain does not get better, you may need to visit your doctor for a physical examination. X-rays will usually be normal in most cases."
)
示例12: test_train_from_lists_of_words
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
def test_train_from_lists_of_words(self):
# classifier can be trained on lists of words instead of strings
train = [(doc.split(), label) for doc, label in train_set]
classifier = NaiveBayesClassifier(train)
assert_equal(classifier.accuracy(test_set),
self.classifier.accuracy(test_set))
示例13: TestNaiveBayesClassifier
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
class TestNaiveBayesClassifier(unittest.TestCase):
def setUp(self):
self.classifier = NaiveBayesClassifier(train_set)
def test_default_extractor(self):
text = "I feel happy this morning."
assert_equal(self.classifier.extract_features(text), basic_extractor(text, train_set))
def test_classify(self):
res = self.classifier.classify("I feel happy this morning")
assert_equal(res, 'positive')
assert_equal(len(self.classifier.train_set), len(train_set))
def test_classify_a_list_of_words(self):
res = self.classifier.classify(["I", "feel", "happy", "this", "morning"])
assert_equal(res, "positive")
def test_train_from_lists_of_words(self):
# classifier can be trained on lists of words instead of strings
train = [(doc.split(), label) for doc, label in train_set]
classifier = NaiveBayesClassifier(train)
assert_equal(classifier.accuracy(test_set),
self.classifier.accuracy(test_set))
def test_prob_classify(self):
res = self.classifier.prob_classify("I feel happy this morning")
assert_equal(res.max(), "positive")
assert_true(res.prob("positive") > res.prob("negative"))
def test_accuracy(self):
acc = self.classifier.accuracy(test_set)
assert_true(isinstance(acc, float))
def test_update(self):
res1 = self.classifier.prob_classify("lorem ipsum")
original_length = len(self.classifier.train_set)
self.classifier.update([("lorem ipsum", "positive")])
new_length = len(self.classifier.train_set)
res2 = self.classifier.prob_classify("lorem ipsum")
assert_true(res2.prob("positive") > res1.prob("positive"))
assert_equal(original_length + 1, new_length)
def test_labels(self):
labels = self.classifier.labels()
assert_true("positive" in labels)
assert_true("negative" in labels)
def test_show_informative_features(self):
feats = self.classifier.show_informative_features()
def test_informative_features(self):
feats = self.classifier.informative_features(3)
assert_true(isinstance(feats, list))
assert_true(isinstance(feats[0], tuple))
def test_custom_feature_extractor(self):
cl = NaiveBayesClassifier(train_set, custom_extractor)
cl.classify("Yay! I'm so happy it works.")
assert_equal(cl.train_features[0][1], 'positive')
def test_init_with_csv_file(self):
with open(CSV_FILE) as fp:
cl = NaiveBayesClassifier(fp, format="csv")
assert_equal(cl.classify("I feel happy this morning"), 'pos')
training_sentence = cl.train_set[0][0]
assert_true(isinstance(training_sentence, unicode))
def test_init_with_csv_file_without_format_specifier(self):
with open(CSV_FILE) as fp:
cl = NaiveBayesClassifier(fp)
assert_equal(cl.classify("I feel happy this morning"), 'pos')
training_sentence = cl.train_set[0][0]
assert_true(isinstance(training_sentence, unicode))
def test_init_with_json_file(self):
with open(JSON_FILE) as fp:
cl = NaiveBayesClassifier(fp, format="json")
assert_equal(cl.classify("I feel happy this morning"), 'pos')
training_sentence = cl.train_set[0][0]
assert_true(isinstance(training_sentence, unicode))
def test_init_with_json_file_without_format_specifier(self):
with open(JSON_FILE) as fp:
cl = NaiveBayesClassifier(fp)
assert_equal(cl.classify("I feel happy this morning"), 'pos')
training_sentence = cl.train_set[0][0]
assert_true(isinstance(training_sentence, unicode))
def test_init_with_custom_format(self):
redis_train = [('I like turtles', 'pos'), ('I hate turtles', 'neg')]
class MockRedisFormat(formats.BaseFormat):
def __init__(self, client, port):
self.client = client
self.port = port
@classmethod
def detect(cls, stream):
return True
#.........这里部分代码省略.........
示例14: len
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
("scored and got sent off", "non-player"),
("yellow cards received by Wayne Rooney", "player"),
("red cards for Lee Cattermole", "player"),
("second half red cards for John Terry", "player"),
("goals scored by Wayne Rooney in the second half against Arsenal", "player"),
("red cards for Manchester United", "non-player"),
("teams with the most red cards", "non-player"),
]
split = len(data) // 2
random.shuffle(data)
cl = NaiveBayesClassifier(data[split:])
cl.classify("games Steven Gerrard played in")
cl.accuracy(data[:split])
cl2 = NaiveBayesClassifier(data[split:], feature_extractor=word_extractor)
cl2.accuracy(data[:split])
from nltk.stem.lancaster import LancasterStemmer
st = LancasterStemmer()
st.stem("provision")
# break_down("goals scored by Gareth Bale")
# [('goals', u'NNS'), ('scored', u'VBD'), ('by', u'IN'), ('Gareth', u'NNP'), ('Bale', u'NNP')]
# -> MATCH (p:Player)-[:played]->(stats)
# WHERE p.name = "Gareth Bale",
# RETURN SUM(stats.goals) AS goals
示例15: _ask_about_result
# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import accuracy [as 别名]
def _ask_about_result():
i = raw_input("are you satistfied ? ")
if i == "y":
return True
if i == 'n':
return False
else:
print " y or n please"
return self._ask_about_result()
if __name__ == '__main__':
print "Hello"
data = load_sample()
splitIndex = 2*len(data)/3
train = data[:splitIndex]
test = data[splitIndex:]
cl = NaiveBayesClassifier(train)
for item in test:
print_item(item)
print "accuarciy", cl.accuracy(test)
happy = _ask_about_result()
if happy:
with open('classifier.pickle', "wb") as f:
pickle.dump(cl, f)