Example #1
0
def output_round(features, ada_classifiers, round):
    best_feature_idx = ada_classifiers[round - 1]['index']
    best_feature = features[best_feature_idx]

    print("\nAdaboost rounds: %d" % round)

    # print image with top feature rectangle
    if best_feature.type == FeatureType.TWO_HORIZONTAL:
        printed_img = draw_feature_2h(open_face(test_face_images[0]),
                                      best_feature)
        print("\nType: TWO_HORIZONTAL")
    elif best_feature.type == FeatureType.TWO_VERTICAL:
        printed_img = draw_feature_2v(open_face(test_face_images[0]),
                                      best_feature)
        print("Type: TWO_VERTICAL")
    elif best_feature.type == FeatureType.THREE_HORIZONTAL:
        printed_img = draw_feature_3h(open_face(test_face_images[0]),
                                      best_feature)
        print("Type: THREE_HORIZONTAL")
    elif best_feature.type == FeatureType.THREE_VERTICAL:
        printed_img = draw_feature_2v(open_face(test_face_images[0]),
                                      best_feature)
        print("Type: THREE_VERTICAL")
    elif best_feature.type == FeatureType.FOUR:
        printed_img = draw_feature_4(open_face(test_face_images[0]),
                                     best_feature)
        print("Type: FOUR")

    print("Position: (%d, %d)" % (best_feature.x, best_feature.y))
    print("Width: %d" % best_feature.width)
    print("Height: %d" % best_feature.height)
    print("Threshold: %f" % ada_classifiers[round - 1]['thresh'])
    print("Training accuracy: %f" % ada_classifiers[round - 1]['accuracy'])

    printed_img.show()

    # classify
    face_prediction = adaboost.classify(test_face_features, ada_classifiers)
    non_face_prediction = adaboost.classify(test_non_face_features,
                                            ada_classifiers)

    false_positive_cnt = count_labels(face_prediction[0], -1.)
    false_negative_cnt = count_labels(non_face_prediction[0], 1.)
    sample_num = len(test_face_features) + len(test_non_face_features)
    test_accuracy = (0. + sample_num - false_positive_cnt -
                     false_negative_cnt) / sample_num

    print("Total accuracy: %f" % test_accuracy)
    print("False Positive: %d" % false_positive_cnt)
    print("False Negative: %d" % false_negative_cnt)
Example #2
0
def adaboost_ml(train_data, test_data, stump_count):
    """
	Adaboost algorithm
	"""
    model = adaboost.fit(read_data(train_data), stump_count)
    accuracy, confusion, output = adaboost.classify(model, test_data)
    confusion_printer(confusion)
    print("Classification Accuracy of Adaboost: " + str(accuracy) + "%")
    write_output(output, "adaboost_output.txt")
    print("Done! Thank you!")
Example #3
0
    def test_classify(self):
        print("Test Classify:")
        print("Classify Simple Data:")
        data_mat, labels_arr = adb.load_simple_data()
        classifies_arr, est_agg = adb.train(data_mat, labels_arr, 30)
        pred = adb.classify([[5, 5], [0, 0]], classifies_arr)
        res = np.matrix([[1.], [-1.]])
        self.assertEqual(True, (pred == res).all())

        print("Classify Loaded Data:")
        datArr, labelArr = adb.load_data_set('horseColicTraining2.txt')
        classiferArray, aggClassEst = adb.train(datArr, labelArr, 10)
        testArr, testLabelArr = adb.load_data_set('horseColicTest2.txt')
        prediction10 = adb.classify(testArr, classiferArray)
        errArr = np.mat(np.ones((67, 1)))
        err_rate = errArr[prediction10 != np.mat(testLabelArr).T].sum() / 67
        self.assertEqual(16.0 / 67, err_rate)
        print("Test Error: %f%%" % (err_rate * 100))
        # 绘制ROC和计算AUC
        val_auc = adb.plot_roc(aggClassEst, labelArr)
        self.assertLessEqual(0.8582969635, round(val_auc, 10))
import numpy as np
import adaboost

def createDataSet(filename):
    file = open(filename, 'r')
    lines = file.readlines()
    file.close()

    dataSet, labels = [], []
    for line in lines:
        data = line.split()
        dataSet.append(list(map(float, data[:-1])))
        labels.append(int(float(data[-1])))

    return np.array(dataSet), np.array(labels)

trainingSet, trainingLabels = createDataSet('data/horse/training.txt')
stumps = adaboost.train(trainingSet, trainingLabels)

testSet, testLabels = createDataSet('data/horse/test.txt')
res = adaboost.classify(testSet, stumps)
a, b = np.sum(res == testLabels), len(testLabels)
print('Correctness: %d/%d = %.2f%%' % (a, b, a/b * 100))
Example #5
0
	train_size = int(training_block.shape[0])
	att_size = int(len(attributes))
	forest_size = 100
	[ensemble_error, ensemble_pred] = rf.ensemble(test_block, test_label_block, rf.raise_forest(training_block,training_label_block, forest_size, train_size, att_size))
	error[0]+= (1.0/k) * ensemble_error

	#cross validation for decision tree
	print "Cross Validating Decision Tree..."
	dec_tree = id3.id3(train_examples, attributes)
	dec_tree_errors = 0
	for i in xrange(len(test_block)):
		if id3.classify(dec_tree, test_block[i]) != test_label_block[i]:
			dec_tree_errors += 1
	error[1] += (1.0/k) * (float(dec_tree_errors) / set_size)

	print "Cross Validating AdaBoost..."
	adaboost_classifier = adaboost.adaboost(train_examples, adaboost_rounds)
	adaboost_errors = 0
	for i in xrange(len(test_block)):
		if adaboost.classify(adaboost_classifier, test_block[i]) != test_label_block[i]:
			adaboost_errors += 1
	error[2] += (1.0/k) * (float(adaboost_errors) / set_size)



print (1-error[0]), (1-error[1])
print 'Estimated accuracy of Random Forest:', (1-error[0])
print 'Estimated accuracy of Decision Tree:', (1-error[1])
print 'Estimated accuracy of AdaBoost:', (1-error[2])