Beispiel #1
0
def main_adaboost():
    """
    Main function for testing Adaboost.
    """

    X_train, Y_train, X_test, Y_test = h_shaped_dataset()
    # X_train, Y_train, X_test, Y_test = gaussians_dataset(2, [100, 150], [[1, 3], [-4, 8]], [[2, 3], [4, 1]])
    # X_train, Y_train, X_test, Y_test = two_moon_dataset(n_samples=300, noise=0.2)

    # visualize dataset
    plot_2d_dataset(X_train, Y_train, 'Training')

    # train model and predict
    model = AdaBoostClassifier(n_learners=100)
    model.fit(X_train, Y_train, verbose=True)
    P = model.predict(X_test)

    # visualize the boundary!
    plot_boundary(X_train, Y_train, model)

    # evaluate and print error
    error = float(np.sum(P != Y_test)) / Y_test.size
    print('Error: {}, Accuracy (1-Error): {}'.format(error, 1 - error))
def main_adaboost():
    """
    Main function for fitting and testing Adaboost classifier.
    """
    X_train, Y_train, X_test, Y_test = gaussians_dataset(2, [300, 400], [[1, 3], [-4, 8]], [[2, 3], [4, 1]])
    # X_train, Y_train, X_test, Y_test = h_shaped_dataset()
    # X_train, Y_train, X_test, Y_test = two_moon_dataset(n_samples=500, noise=0.2)

    # Visualize dataset
    plot_2d_dataset(X_train, Y_train, 'Training', blocking=False)

    # Init model
    model = AdaBoostClassifier(n_learners=100)

    # Train
    model.fit(X_train, Y_train, verbose=True)

    # Predict
    y_preds = model.predict(X_test)
    print('Accuracy on test set: {}'.format(np.mean(y_preds == Y_test)))

    # Visualize the predicted boundary
    plot_boundary(X_train, Y_train, model)
Beispiel #3
0
def main():
    """
    分别训练不使用正则化的原始模型、使用L2正则化的模型、使用dropout的模型,输出cost值、预测准确率、边界预测结果等信息

    """

    # 读入数据集
    train_X, train_Y, test_X, test_Y = utils.load_dataset("data")

    # 训练不使用正则化的原始模型
    model_name = "_without_regularization"
    parameters = model(train_X, train_Y, model_name)
    # 在训练集和测试集上的进行预测并输出准确率
    print "On the training set:"
    predictions_train = utils.predict(train_X, train_Y, parameters)
    print "On the test set:"
    predictions_test = utils.predict(test_X, test_Y, parameters)
    # 绘制边界预测结果图
    plt.clf()
    plt.title("Model without regularization")
    axes = plt.gca()
    axes.set_xlim([-0.75, 0.40])
    axes.set_ylim([-0.75, 0.65])
    utils.plot_boundary(lambda x: utils.predict_decision(parameters, x.T),
                        train_X, train_Y,
                        "decision_boundary" + model_name + ".png")

    # 训练使用L2正则化的模型
    model_name = "_with_L2-regularization"
    parameters = model(train_X, train_Y, model_name, lambd=0.7)
    # 在训练集和测试集上的进行预测并输出准确率
    print "On the train set:"
    predictions_train = utils.predict(train_X, train_Y, parameters)
    print "On the test set:"
    predictions_test = utils.predict(test_X, test_Y, parameters)
    # 绘制边界预测结果图
    plt.clf()
    plt.title("Model with L2-regularization")
    axes = plt.gca()
    axes.set_xlim([-0.75, 0.40])
    axes.set_ylim([-0.75, 0.65])
    utils.plot_boundary(lambda x: utils.predict_decision(parameters, x.T),
                        train_X, train_Y,
                        "decision_boundary" + model_name + ".png")

    # 训练使用dropout的模型
    model_name = "_with_dropout"
    parameters = model(train_X, train_Y, model_name, keep_prob=0.86)
    # 在训练集和测试集上的进行预测并输出准确率
    print "On the train set:"
    predictions_train = utils.predict(train_X, train_Y, parameters)
    print "On the test set:"
    predictions_test = utils.predict(test_X, test_Y, parameters)
    # 绘制边界预测结果图
    plt.clf()
    plt.title("Model with dropout")
    axes = plt.gca()
    axes.set_xlim([-0.75, 0.40])
    axes.set_ylim([-0.75, 0.65])
    utils.plot_boundary(lambda x: utils.predict_decision(parameters, x.T),
                        train_X, train_Y,
                        "decision_boundary" + model_name + ".png")