Example #1
0
while not eof:
    line = f_pos.readline()
    eof = not len(line)
    x.append(count_words(line.decode('utf-8', 'ignore')))
    y.append(+1)
    x.append(count_words(f_neg.readline().decode('utf-8', 'ignore')))
    y.append(-1)
    i += 1

fold = int(i * 0.9)

f_pos.close()
f_neg.close()

from ml.nb import NaiveBayes

classifier = NaiveBayes()
classifier.train(x[0:fold], y[0:fold])
estim_y = classifier.predict(x[fold:])
(acc, ) = classifier.evaluate(y[fold:], estim_y)

print('Naive Bayes accuracy = {0:.2f}%'.format(acc * 100))

from ml.svm import SVM

classifier = SVM()
classifier.train(x[0:fold], y[0:fold])
estim_y = classifier.predict(x[fold:])
(acc, ) = classifier.evaluate(y[fold:], estim_y)

print('SVM accuracy = {0:.2f}%'.format(acc * 100))
Example #2
0
# divide into training and test sets
test_x = []
test_y = []
train_x = []
train_y = []
for i in range(0, len(x)):
    if i % 3:
        train_x.append(list_to_dict(x[i]))
        train_y.append(y[i])
    else:
        test_x.append(list_to_dict(x[i]))
        test_y.append(y[i])

from ml.nb import NaiveBayes

classifier = NaiveBayes()
classifier.train(train_x, train_y)
estim_y = classifier.predict(test_x)
(acc, ) = classifier.evaluate(test_y, estim_y)

print('Naive Bayes accuracy = {0:.2f}%'.format(acc * 100))

from ml.svm import SVM

classifier = SVM()
classifier.train(train_x, train_y)
estim_y = classifier.predict(test_x)
(acc, ) = classifier.evaluate(test_y, estim_y)

print('SVM accuracy = {0:.2f}%'.format(acc * 100))
Example #3
0
while not eof:
	line = f_pos.readline()
	eof = not len(line)
	x.append(count_words(line.decode('utf-8', 'ignore')))
	y.append(+1)
	x.append(count_words(f_neg.readline().decode('utf-8', 'ignore')))
	y.append(-1)
	i += 1
	
fold = int(i * 0.9)

f_pos.close()
f_neg.close()

from ml.nb import NaiveBayes

classifier = NaiveBayes()
classifier.train(x[0:fold], y[0:fold])
estim_y = classifier.predict(x[fold:])
(acc, ) = classifier.evaluate(y[fold:], estim_y)

print('Naive Bayes accuracy = {0:.2f}%'.format(acc * 100))

from ml.svm import SVM

classifier = SVM()
classifier.train(x[0:fold], y[0:fold])
estim_y = classifier.predict(x[fold:])
(acc, ) = classifier.evaluate(y[fold:], estim_y)

print('SVM accuracy = {0:.2f}%'.format(acc * 100))
Example #4
0
def list_to_dict(l):
#	return dict(zip(l, [1 for _ in range(0, len(l))]))
	return dict(zip(range(0, len(l)), l))

# divide into training and test sets
test_x = []; test_y = []; train_x = []; train_y = [];
for i in range(0, len(x)):
	if i % 3:
		train_x.append(list_to_dict(x[i]))
		train_y.append(y[i])
	else:
		test_x.append(list_to_dict(x[i]))
		test_y.append(y[i])
	
from ml.nb import NaiveBayes

classifier = NaiveBayes()
classifier.train(train_x, train_y)
estim_y = classifier.predict(test_x)
(acc, ) = classifier.evaluate(test_y, estim_y)

print('Naive Bayes accuracy = {0:.2f}%'.format(acc * 100))

from ml.svm import SVM

classifier = SVM()
classifier.train(train_x, train_y)
estim_y = classifier.predict(test_x)
(acc, ) = classifier.evaluate(test_y, estim_y)

print('SVM accuracy = {0:.2f}%'.format(acc * 100))
Example #5
0
	(name, sex) = line.strip().split(' ')
	x.append({name[-1:]: 1, name[-2:]: 1, name[-3:]: 1, name[-4:]: 1})
	y.append(sex)
	
fold = -100

f.close()

import os

from ml.nb import NaiveBayes

filename = 'tmp/nb.pickle'
exists = os.path.isfile(filename)
if exists:
	classifier = NaiveBayes.load(filename)
else:
	classifier = NaiveBayes()
	
classifier.train(x[0:fold], y[0:fold])
estim_y = classifier.predict(x[fold:])
(acc, ) = classifier.evaluate(y[fold:], estim_y)

if not exists:
	classifier.save(filename)

print('Naive Bayes accuracy = {0:.2f}%'.format(acc * 100))

from ml.svm import SVM

filename = 'tmp/svm.pickle'