def process(training_file, test_file, check, draw): # Load training data. with open(training_file) as f: class_1 = array(pickle.load(f)) class_2 = array(pickle.load(f)) labels = pickle.load(f) model = bayes.BayesClassifier() model.train([class_1, class_2], [1, -1]) # Load test data. with open(test_file) as f: class_1 = array(pickle.load(f)) class_2 = array(pickle.load(f)) labels = pickle.load(f) if check: n = class_1.shape[0] n_correct = 0 n_correct += sum(model.classify(class_1)[0] == labels[:n]) n_correct += sum(model.classify(class_2)[0] == labels[n:]) print 'percent correct:', 100 * n_correct / float(2 * n) if draw: def classify(x, y, model=model): points = vstack((x, y)) return model.classify(points.T)[0] imtools.plot_2d_boundary([-6, 6, -6, 6], [class_1, class_2], classify, [1, -1]) show()
def process(training_file, test_file, check, draw): # Load training data. with open(training_file) as f: class_1 = pickle.load(f) class_2 = pickle.load(f) labels = pickle.load(f) model = knn.KnnClassifier(labels, vstack((class_1, class_2))) # Load test data. with open(test_file) as f: class_1 = pickle.load(f) class_2 = pickle.load(f) labels = pickle.load(f) if check: n = class_1.shape[0] n_correct = 0 for i in range(n): if model.classify(class_1[i]) == labels[i]: n_correct += 1 if model.classify(class_2[i]) == labels[n + i]: n_correct += 1 print 'percent correct:', 100 * n_correct / float(2 * n) if draw: def classify(x, y, model=model): return array([model.classify([xx, yy]) for (xx, yy) in zip(x, y)]) imtools.plot_2d_boundary([-6, 6, -6, 6], [class_1, class_2], classify, [1, -1]) show()
def process(training_file, test_file, check, draw): # Load training data. with open(training_file) as f: class_1 = pickle.load(f) class_2 = pickle.load(f) labels = pickle.load(f) model = knn.KnnClassifier(labels, vstack((class_1, class_2))) # Load test data. with open(test_file) as f: class_1 = pickle.load(f) class_2 = pickle.load(f) labels = pickle.load(f) if check: n = class_1.shape[0] n_correct = 0 for i in range(n): if model.classify(class_1[i]) == labels[i]: n_correct += 1 if model.classify(class_2[i]) == labels[n + i]: n_correct += 1 print 'percent correct:', 100 * n_correct / float(2 * n) if draw: def classify(x, y, model=model): return array([model.classify([xx, yy]) for (xx, yy) in zip(x, y)]) imtools.plot_2d_boundary( [-6, 6, -6, 6], [class_1, class_2], classify, [1, -1]) show()
def process(training_file, test_file, check, draw): # Load training data. with open(training_file) as f: class_1 = array(pickle.load(f)) class_2 = array(pickle.load(f)) labels = pickle.load(f) model = bayes.BayesClassifier() model.train([class_1, class_2], [1, -1]) # Load test data. with open(test_file) as f: class_1 = array(pickle.load(f)) class_2 = array(pickle.load(f)) labels = pickle.load(f) if check: n = class_1.shape[0] n_correct = 0 n_correct += sum(model.classify(class_1)[0] == labels[:n]) n_correct += sum(model.classify(class_2)[0] == labels[n:]) print "percent correct:", 100 * n_correct / float(2 * n) if draw: def classify(x, y, model=model): points = vstack((x, y)) return model.classify(points.T)[0] imtools.plot_2d_boundary([-6, 6, -6, 6], [class_1, class_2], classify, [1, -1]) show()
def process(training_file, test_file, check, draw): # Load training data. with open(training_file) as f: class_1 = pickle.load(f) class_2 = pickle.load(f) labels = pickle.load(f) # Convert data to lists for libsvm. class_1 = map(list, class_1) class_2 = map(list, class_2) labels = list(labels) samples = class_1 + class_2 problem = svmutil.svm_problem(labels, samples) # Don't print to stdout, use radial basis functions. param = svmutil.svm_parameter('-q -t 2') model = svmutil.svm_train(problem, param) # Load test data. with open(test_file) as f: class_1 = pickle.load(f) class_2 = pickle.load(f) labels = pickle.load(f) class_1 = map(list, class_1) class_2 = map(list, class_2) labels = list(labels) if check: # Sadly, this prints to stdout too :-/ svmutil.svm_predict(labels, class_1 + class_2, model) # Prints accuracy. if draw: def classify(x, y, model=model): return array( svmutil.svm_predict([0] * len(x), map(list, zip(x, y)), model)[0]) imtools.plot_2d_boundary( [-6, 6, -6, 6], [array(class_1), array(class_2)], classify, [1, -1]) show()
def process(training_file, test_file, check, draw): # Load training data. with open(training_file) as f: class_1 = pickle.load(f) class_2 = pickle.load(f) labels = pickle.load(f) # Convert data to lists for libsvm. class_1 = map(list, class_1) class_2 = map(list, class_2) labels = list(labels) samples = class_1 + class_2 problem = svmutil.svm_problem(labels, samples) # Don't print to stdout, use radial basis functions. param = svmutil.svm_parameter('-q -t 2') model = svmutil.svm_train(problem, param) # Load test data. with open(test_file) as f: class_1 = pickle.load(f) class_2 = pickle.load(f) labels = pickle.load(f) class_1 = map(list, class_1) class_2 = map(list, class_2) labels = list(labels) if check: # Sadly, this prints to stdout too :-/ svmutil.svm_predict(labels, class_1 + class_2, model) # Prints accuracy. if draw: def classify(x, y, model=model): return array(svmutil.svm_predict([0] * len(x), map(list, zip(x, y)), model)[0]) imtools.plot_2d_boundary( [-6, 6, -6, 6], [array(class_1), array(class_2)], classify, [1, -1]) show()