示例#1
0
def run_knn(name, x_train, x_test, y_train, y_test, tuned_parameters):
    print("Working on {} data...".format(name))

    img_name = "images/{}_knn_learning_curve.png".format(name)
    img_title = '{} kNN Learning Curve'.format(name)
    report_name = "reports/{}_knn_output.txt".format(name)

    sys.stdout = open(report_name, "w")

    clf = get_optimized_classifier(estimator=KNeighborsClassifier(),
                                   tuned_parameters=tuned_parameters,
                                   x_train=x_train,
                                   y_train=y_train)

    best_min_samples = clf.best_params_['n_neighbors']
    optimized_clf = KNeighborsClassifier(n_neighbors=best_min_samples)

    plot_learning_curve(
        optimized_clf,
        title=img_title,
        file_name=img_name,
        X=x_train,
        y=y_train,
    )

    run_optimized(optimized_clf, x_train, y_train, x_test, y_test)

    sys.stdout = sys.__stdout__
    print("Finished {} knn!".format(name))
    print()
def run_sa_nn(name, x_train, x_test, y_train, y_test):
    print ("Working on SA NN...")

    report_name = "reports/{}_nn_sa_output.txt".format(name)
    sys.stdout = open(report_name, "w")

    sa_nn = mlrose.NeuralNetwork(hidden_nodes = [11], algorithm = 'simulated_annealing', max_iters = 1000)

    run_optimized(sa_nn, x_train, y_train, x_test, y_test)

    sys.stdout = sys.__stdout__
def run_rhc_nn(name, x_train, x_test, y_train, y_test):
    print ("Working on RHC NN...")

    report_name = "reports/{}_nn_rhc_output.txt".format(name)
    sys.stdout = open(report_name, "w")

    rhc_nn = mlrose.NeuralNetwork(hidden_nodes = [11], algorithm = 'random_hill_climb', max_iters = 1000)

    run_optimized(rhc_nn, x_train, y_train, x_test, y_test)

    sys.stdout = sys.__stdout__
def run_nn(name, x_train, x_test, y_train, y_test):
    print ("Working on BackProp")

    report_name = "reports/{}_nn_output.txt".format(name)
    sys.stdout = open(report_name, "w")

    optimized_clf = MLPClassifier(
        max_iter=1000,
        alpha=0.001,
        hidden_layer_sizes=11,
        random_state=99
    )

    run_optimized(optimized_clf, x_train, y_train, x_test, y_test)

    sys.stdout = sys.__stdout__
def run_nn(name, x_train, x_test, y_train, y_test, tuned_parameters, iter_range):
    print ("Working on {} data...".format(name))

    img_name = "images/{}_nn_learning_curve.png".format(name)
    img_title = '{} Neural Net Learning Curve'.format(name)
    iter_title = '{} Neural Net Iteration Learning Curve'.format(name)
    iter_name = "iteration_curves/{}_nn.png".format(name)
    report_name = "reports/{}_nn_output.txt".format(name)
    
    sys.stdout = open(report_name, "w")

    clf = get_optimized_classifier(
        estimator=MLPClassifier(),
        tuned_parameters=tuned_parameters,
        x_train=x_train,
        y_train=y_train
        )

    best_params = clf.best_params_
    optimized_clf = MLPClassifier(**best_params)

    plot_learning_curve(
        optimized_clf,
        title=img_title,
        file_name=img_name,
        X=x_train,
        y=y_train,
        )

    run_optimized(optimized_clf, x_train, y_train, x_test, y_test)

    plot_iterations(
        optimized_clf,
        file_name=iter_name,
        title=iter_title,
        X=x_test,
        y=y_test,
        param_name="max_iter",
        param_range=iter_range
        )

    sys.stdout = sys.__stdout__
    print ("Finished {} Neural Net!".format(name))
    print()
示例#6
0
def run_nn(name, x_train, x_test, y_train, y_test):
    img_name = "images/{}_nn_learning_curve.png".format(name)
    img_title = '{} Neural Net Learning Curve'.format(name)
    iter_title = '{} Neural Net Iteration Learning Curve'.format(name)

    optimized_clf = MLPClassifier(max_iter=1000,
                                  alpha=0.001,
                                  hidden_layer_sizes=11,
                                  random_state=99)

    plot_learning_curve(
        optimized_clf,
        title=img_title,
        file_name=img_name,
        X=x_train,
        y=y_train,
    )

    run_optimized(optimized_clf, x_train, y_train, x_test, y_test, name)
def run_ga_nn(name, x_train, x_test, y_train, y_test):
    print ("Working on GA NN...")

    report_name = "reports/{}_nn_ga_output.txt".format(name)
    sys.stdout = open(report_name, "w")

    ga_nn = mlrose.NeuralNetwork(
        hidden_nodes = [11],
        algorithm = 'genetic_alg',
        max_iters = 1000,
        early_stopping = True,
        clip_max = 10,
        max_attempts = 10,
        mutation_prob=0.15
        )

    run_optimized(ga_nn, x_train, y_train, x_test, y_test)

    sys.stdout = sys.__stdout__
def run_dt(name, x_train, x_test, y_train, y_test):
    print("Working on {} data...".format(name))

    img_name = "images/{}_decision_tree_learning_curve.png".format(name)
    img_title = '{} Decision Tree Learning Curve'.format(name)
    report_name = "reports/{}_decision_tree_output.txt".format(name)

    sys.stdout = open(report_name, "w")

    tuned_parameters = [{
        'min_samples_split': list(range(40, 70)),
        "min_samples_leaf": list(range(1, 10)),
    }]

    clf = get_optimized_classifier(
        estimator=DecisionTreeClassifier(random_state=99),
        tuned_parameters=tuned_parameters,
        x_train=x_train,
        y_train=y_train)

    best_min_samples = clf.best_params_['min_samples_split']
    optimized_clf = DecisionTreeClassifier(min_samples_split=best_min_samples,
                                           random_state=99)

    plot_learning_curve(
        optimized_clf,
        title=img_title,
        file_name=img_name,
        X=x_train,
        y=y_train,
    )

    run_optimized(optimized_clf, x_train, y_train, x_test, y_test)

    sys.stdout = sys.__stdout__
    print("Finished {} decision tree!".format(name))
    print()
示例#9
0
def run_boosting(name, x_train, x_test, y_train, y_test, tuned_parameters):
    print("Working on {} data...".format(name))

    img_name = "images/{}_boosting_learning_curve.png".format(name)
    iter_name = "iteration_curves/{}_boosting.png".format(name)
    img_title = '{} Boosting Learning Curve'.format(name)
    iter_title = '{} Boosting Iterations Learning Curve'.format(name)
    report_name = "reports/{}_boosting_output.txt".format(name)

    sys.stdout = open(report_name, "w")

    clf = get_optimized_classifier(
        estimator=GradientBoostingClassifier(random_state=99),
        tuned_parameters=tuned_parameters,
        x_train=x_train,
        y_train=y_train)

    best_params = clf.best_params_
    optimized_clf = GradientBoostingClassifier(**best_params, random_state=99)

    plot_learning_curve(
        optimized_clf,
        title=img_title,
        file_name=img_name,
        X=x_train,
        y=y_train,
    )

    run_optimized(optimized_clf, x_train, y_train, x_test, y_test)

    plot_boosting_iteration_curve(optimized_clf, iter_name, iter_title, x_test,
                                  y_test)

    sys.stdout = sys.__stdout__
    print("Finished {} boosting!".format(name))
    print()