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))
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))
# 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))