예제 #1
0
def run_grid_search(dataDicts, ada=False):
    depths = [
        1, 2, 3, 5, 10, 12, 13, 15, 16, 17, 18, 19, 20, 21, 23, 25, 27, 29, 30,
        35
    ]
    accuracies_beach = []
    accuracies_finger = []
    accuracies_rest = []

    for depth in depths:
        if ada == False:
            clf_tree = tree.DecisionTreeClassifier(max_depth=depth,
                                                   random_state=0)
            accuracy, _, _ = utils.cross_val(clf_tree, dataDicts)
            accuracies_beach.append(accuracy["beach"])
            accuracies_finger.append(accuracy["finger"])
            accuracies_rest.append(accuracy["rest"])
        else:
            if depth > 15:
                accuracies_beach.append(0)
                accuracies_finger.append(0)
                accuracies_rest.append(0)
            else:
                clf_tree = tree.DecisionTreeClassifier(max_depth=depth,
                                                       random_state=0)
                accuracy, _, _ = utils.cross_val(clf_tree, dataDicts, ada=True)
                accuracies_beach.append(accuracy["beach"])
                accuracies_finger.append(accuracy["finger"])
                accuracies_rest.append(accuracy["rest"])

    keys = ["beach", "finger", "rest"]
    accuracies = {
        "beach": accuracies_beach,
        "finger": accuracies_finger,
        "rest": accuracies_rest,
        "depths": depths
    }
    for key in keys:
        data = accuracies[key]
        ind = np.argmax(data)
        print("dataset: " + key)
        if ada == False:
            print("optimal maximum depth value for Decision Tree: ")
            print("depth value: " + str(depths[ind]) + ", accuracy: " +
                  str(data[ind]))
        else:
            print(
                "optimal maximum depth value for Decision Tree with AdaBoost: "
            )
            print("depth value: " + str(depths[ind]) + ", accuracy: " +
                  str(data[ind]))
    print("depths")
    print(depths)
    print("beach data")
    print(accuracies_beach)
    print("finger data")
    print(accuracies_finger)
    print("rest data")
    print(accuracies_rest)
예제 #2
0
def compareCV(dataDict):

    svm = SVC(kernel='linear')
    acc_, _, _ = utils.cross_val(svm, dataDict)

    print(acc_)

    ada = AdaBoostClassifier(svm, algorithm='SAMME')
    acc_, _, _ = utils.cross_val(ada, dataDict)
    print(acc_)
예제 #3
0
def run(dataDicts, ada=False, max_depth=1):

    if ada == False:
        clf_tree = tree.DecisionTreeClassifier(max_depth=max_depth,
                                               random_state=0)
        accuracy, precision, recall = utils.cross_val(clf_tree, dataDicts)
    else:
        clf_tree = tree.DecisionTreeClassifier(max_depth=max_depth,
                                               random_state=0)
        accuracy, precision, recall = utils.cross_val(clf_tree,
                                                      dataDicts,
                                                      ada=True)

    keys = ["beach", "finger", "rest"]
    for key in keys:
        print("dataset: " + key)
        if ada == False:
            print("Decision Tree")
            print("Accuracy: " + str(accuracy[key]) + ", Recall: " +
                  str(recall[key]) + ", Precision: " + str(precision[key]))
        else:
            print("Decision Tree with AdaBoost")
            print("Accuracy: " + str(accuracy[key]) + ", Recall: " +
                  str(recall[key]) + ", Precision: " + str(precision[key]))
예제 #4
0
def run(dataDicts):

    clf = GaussianNB()
    a, p, r = utils.cross_val(clf, dataDicts)
    print(a)
예제 #5
0
def run(dataDicts):

    clf = MLPClassifier(solver='lbfgs', alpha=0.01, random_state=1)
    A, B, C = utils.cross_val(clf, dataDicts)
    print(A)

    # for a in Alpha:
    #     print(a)
    #     clf = MLPClassifier(solver='lbfgs', alpha=a, random_state=1)
    #     A, B, C = utils.cross_val(clf, dataDicts)
    #     print(A)

    # for layer in HiddenLayerSize:
    #     print(layer)
    #     clf = MLPClassifier(solver='lbfgs', alpha=0.01, hidden_layer_sizes = (layer,), random_state=1)
    #     A, B, C = utils.cross_val(clf, dataDicts)
    #     print(A)

    # for iter in MaxIter:
    #     print(iter)
    #     clf = MLPClassifier(solver='lbfgs', alpha=0.01, max_iter=iter, random_state=1)
    #     A, B, C = utils.cross_val(clf, dataDicts)
    #     print(A)

    # for act in Activation:
    #     print(act)
    #     clf = MLPClassifier(solver='lbfgs', alpha = 0.01, activation = act, random_state=1)
    #     A, B, C = utils.cross_val(clf, dataDicts)
    #     print(A)

    # for learn in LearningRate:
    #     print(learn)
    #     clf = MLPClassifier(solver='sgd', alpha=0.01, learning_rate_init = learn, random_state=1)
    #     A, B, C = utils.cross_val(clf, dataDicts)
    #     print(A)

    warnings.filterwarnings('ignore')

    for key in dataDicts[4]:
        print(">> Dataset: " + key.upper())
        [train_x, train_y, test_x, test_y] = dataDicts[4][key]

        param_g = {
            "alpha": [0.001, 0.01, 0.1, 1],
            "hidden_layer_sizes": [(50, 50, 50), (100, 50), (100)],
            "max_iter": [100, 250, 500, 1000],
            "activation": ['identity', 'logistic', 'relu'],
            "solver": ['adam'],
            "random_state": [0, 1, 2, 3, 4],
            "learning_rate_init": [0.001, 0.005, 0.01]
        }

        # inData = ""
        # gs = RandomizedSearchCV(MLPClassifier(), param_distributions=param_g, scoring="accuracy", n_jobs=-1, n_iter=100)

        # inData = "ARM"
        # gs = GridSearchCV(MLPClassifier(random_state=1), param_grid=param_g, scoring="accuracy", n_jobs=-1)

        # inData = "FARM"
        # gs = GridSearchCV(MLPClassifier(random_state=1), param_grid=param_g, scoring="accuracy", n_jobs=-1)

        # inData = "WARM"
        # gs = GridSearchCV(MLPClassifier(random_state=1), param_grid=param_g, scoring="accuracy", n_jobs=-1)

        # inData = "WARM" - test best random seed
        gs = GridSearchCV(MLPClassifier(),
                          param_grid=param_g,
                          scoring="accuracy",
                          n_jobs=-1)

        # inData = "NWARM"
        # gs = GridSearchCV(MLPClassifier(random_state=1), param_grid=param_g, scoring="accuracy", n_jobs=-1)

        gs.fit(train_x, train_y)

        bestParam = gs.best_params_
        print(bestParam)

        bestScore = gs.best_score_
        print(bestScore)

        utils.printToTxt(modelName, "mlp_" + key + "_best", bestParam)
        utils.printToTxt(modelName, "mlp_" + key + "_best_score", bestScore)
        utils.printToTxt(modelName, "mlp_" + key + "_cv_results",
                         gs.cv_results_)