예제 #1
0
def ex_2_b(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 b)
    :param x_train: Training samples (2-dimensional)
    :param y_train: Training labels
    :param x_test: Testing samples (2-dimensional)
    :param y_test: Testing labels
    :return:
    """
    ###########
    ## TODO:
    ## Train SVMs with polynomial kernels for different values of the degree
    ## (Remember to set the 'coef0' parameter to 1)
    ## and plot the variation of the test and training scores with polynomial degree using 'plot_score_vs_degree' func.
    ## Plot the decision boundary and support vectors for the best value of degree
    ## using 'plot_svm_decision_boundary' function
    ###########

    degrees = range(1, 21)
    machines = [svm.SVC(kernel='poly', degree=d, coef0=1.0) for d in degrees]

    for machine in machines:
        machine.fit(x_train, y_train)

    trainScores = [machine.score(x_train, y_train) for machine in machines]
    testScores = [machine.score(x_test, y_test) for machine in machines]

    plot_score_vs_degree(trainScores, testScores, degrees)

    bestDegree = testScores.index(max(testScores))
    print('Score of best polynomial degree ({}): {}'.format(
        bestDegree + 1, testScores[bestDegree]))
    plot_svm_decision_boundary(machines[bestDegree], x_train, y_train, x_test,
                               y_test)
예제 #2
0
파일: svm.py 프로젝트: sebobo233/CI
def ex_2_b(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 b)
    :param x_train: Training samples (2-dimensional)
    :param y_train: Training labels
    :param x_test: Testing samples (2-dimensional)
    :param y_test: Testing labels
    :return:
    """
    ###########
    ## TODO:
    ## Train SVMs with polynomial kernels for different values of the degree
    ## (Remember to set the 'coef0' parameter to 1)
    ## and plot the variation of the test and training scores with polynomial degree using 'plot_score_vs_degree' func.
    ## Plot the decision boundary and support vectors for the best value of degree
    ## using 'plot_svm_decision_boundary' function
    ###########

    degrees = range(1, 20)

    train_scores = np.zeros(np.size(degrees))
    test_scores = np.zeros(np.size(degrees))

    for j in range(np.size(degrees)):
        svc = svm.SVC(kernel='poly', C=1, coef0=1,
                      degree=degrees[j]).fit(x_train, y_train)
        test_scores[j] = svc.score(x_test, y_test)
        train_scores[j] = svc.score(x_train, y_train)

    plot_score_vs_degree(train_scores, test_scores, degrees)

    acc_max = test_scores.argmax()
    svc = svm.SVC(kernel='poly', C=1, coef0=1,
                  degree=degrees[acc_max]).fit(x_train, y_train)
    plot_svm_decision_boundary(svc, x_train, y_train, x_test, y_test)
예제 #3
0
def ex_2_b(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 b)
    :param x_train: Training samples (2-dimensional)
    :param y_train: Training labels
    :param x_test: Testing samples (2-dimensional)
    :param y_test: Testing labels
    :return:
    """
    ###########
    ## TODO:
    ## Train SVMs with polynomial kernels for different values of the degree
    ## (Remember to set the 'coef0' parameter to 1)
    ## and plot the variation of the test and training scores with polynomial degree using 'plot_score_vs_degree' func.
    ## Plot the decision boundary and support vectors for the best value of degree
    ## using 'plot_svm_decision_boundary' function
    ###########
    degrees = range(1, 20)
    scorepolylist_test = np.zeros(np.array(degrees).shape[0])
    scorepolylist_train = np.zeros(np.array(degrees).shape[0])
    for deg in degrees:
        SVMpoly = svm.SVC(kernel='poly', coef0=1, degree=deg)
        SVMpoly.fit(x_train, y_train)
        scorepolylist_test[deg - 1] = SVMpoly.score(x_test, y_test)
        scorepolylist_train[deg - 1] = SVMpoly.score(x_train, y_train)

    max_score_index = np.argmax(scorepolylist_test)
    optimal_deg = max_score_index + 1
    SVMpolyopt = svm.SVC(kernel='poly', coef0=1, degree=optimal_deg)
    SVMpolyopt.fit(x_train, y_train)
    plot_score_vs_degree(scorepolylist_train, scorepolylist_test, degrees)
    plot_svm_decision_boundary(SVMpolyopt, x_train, y_train, x_test, y_test)
    print("Optimal degree", optimal_deg, "Optimal score",
          scorepolylist_test[max_score_index])
예제 #4
0
def ex_2_b(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 b)
    :param x_train: Training samples (2-dimensional)
    :param y_train: Training labels
    :param x_test: Testing samples (2-dimensional)
    :param y_test: Testing labels
    :return:
    """
    ###########
    ## TODO:
    ## Train SVMs with polynomial kernels for different values of the degree
    ## (Remember to set the 'coef0' parameter to 1)
    ## and plot the variation of the test and training scores with polynomial degree using 'plot_score_vs_degree' func.
    ## Plot the decision boundary and support vectors for the best value of degree
    ## using 'plot_svm_decision_boundary' function
    ###########
    degrees = range(1, 21)

    train_scores = []
    test_scores = []
    polySVMs = []

    for degree in degrees:
        polySVM = svm.SVC(kernel="poly", coef0=1)
        polySVM.set_params(degree=degree)
        polySVM.fit(x_train, y_train)

        train_scores.append(polySVM.score(x_train, y_train))
        test_scores.append(polySVM.score(x_test, y_test))
        polySVMs.append(polySVM)

    best_test_score_index = np.argmax(test_scores)
    print("best_train_score for poly kernel: ",
          train_scores[best_test_score_index])
    print("best_test_score for poly kernel: ",
          test_scores[best_test_score_index])
    print("degree for best test_score: ", degrees[best_test_score_index])
    plot_score_vs_degree(train_scores, test_scores, degrees)
    print("polySVm nSV: ", polySVM.support_vectors_.shape)
    plot_svm_decision_boundary(polySVMs[best_test_score_index], x_train,
                               y_train, x_test, y_test)
예제 #5
0
def ex_2_b(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 b)
    :param x_train: Training samples (2-dimensional)
    :param y_train: Training labels
    :param x_test: Testing samples (2-dimensional)
    :param y_test: Testing labels
    :return:
    """
    # parameters
    r_value = 1
    degrees = range(1, 21)
    C = 1

    # store the scores
    train_scores = []
    test_scores = []

    # store the created svm so we don't have to train the best twice.
    clfs = []

    # create and train the svm with a polynomial kernel for every d value
    for d in degrees:
        clf = svm.SVC(C=C, kernel='poly', degree=d, coef0=r_value)
        clf.fit(x_train, y_train)
        clfs.append(clf)
        # compute the scores
        train_scores.append(clf.score(x_train, y_train))
        test_scores.append(clf.score(x_test, y_test))

    # find the svm with the better test score
    max_index = test_scores.index(max(test_scores))
    clf = clfs[max_index]
    a = clf.support_vectors_
    print("best d value: {}, with an accuracy of {}".format(
        degrees[max_index], test_scores[max_index]))
    print("number of SV:", len(a))
    # plot the decision boundary on both datasets for the best svm
    plot_svm_decision_boundary(clf, x_train, y_train, x_test, y_test)

    # plot the score depending of d
    plot_score_vs_degree(train_scores, test_scores, degrees)
예제 #6
0
def ex_2_b(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 b)
    :param x_train: Training samples (2-dimensional)
    :param y_train: Training labels
    :param x_test: Testing samples (2-dimensional)
    :param y_test: Testing labels
    :return:
    """
    ###########
    ## TODO:
    ## Train SVMs with polynomial kernels for different values of the degree
    ## (Remember to set the 'coef0' parameter to 1)
    ## and plot the variation of the training and test scores with polynomial degree using 'plot_score_vs_degree' func.
    ## Plot the decision boundary and support vectors for the best value of degree
    ## using 'plot_svm_decision_boundary' function
    ###########
    degrees = range(1, 21)

    test_scores = np.array([])
    train_scores = np.array([])
    best_svm = None
    best_test_score = 0

    for deg in degrees:
        clf = svm.SVC(kernel='poly', degree=deg, coef0=1)
        clf.fit(x_train, y_train)

        test_score = clf.score(x_test, y_test)

        if test_score > best_test_score:
            best_test_score = test_score
            best_svm = clf

        test_scores  = np.append(test_scores, test_score)
        train_scores = np.append(train_scores, clf.score(x_train, y_train))

    plot_score_vs_degree(train_scores, test_scores, degrees)

    plot_svm_decision_boundary(clf, x_train, y_train, x_test, y_test)
예제 #7
0
def ex_2_b(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 b)
    :param x_train: Training samples (2-dimensional)
    :param y_train: Training labels
    :param x_test: Testing samples (2-dimensional)
    :param y_test: Testing labels
    :return:
    """
    ###########
    # TODO:
    # Train SVMs with polynomial kernels for different values of the degree
    # (Remember to set the 'coef0' parameter to 1)
    # and plot the variation of the test and training scores with polynomial degree using 'plot_score_vs_degree' func.
    # Plot the decision boundary and support vectors for the best value of degree
    # using 'plot_svm_decision_boundary' function
    ###########
    degrees = list(range(1, 21))
    train_score = []
    test_score = []

    svc = svm.SVC(kernel='poly', coef0=1)

    for degree in degrees:
        svc.degree = degree
        svc.fit(x_train, y_train)
        test_score.append(svc.score(x_test, y_test))
        train_score.append(svc.score(x_train, y_train))

    plot_score_vs_degree(train_score, test_score, degrees)

    highest_test_score = max(test_score)
    degree = test_score.index(highest_test_score) + 1
    print("Degree " + str(degree) + " produces the Highest Test Score " +
          str(highest_test_score))
    svc.degree = degree
    svc.fit(x_train, y_train)
    plot_svm_decision_boundary(svc, x_train, y_train, x_test, y_test)
예제 #8
0
def svm_poly(x_train, y_train, x_test, y_test):
    #Polynomial kernel
    #    svc = svm.SVC(kernel='poly')
    #    svc.fit(x_train,y_train)
    #    train_score = svc.score(x_train, y_train)
    #    test_score = svc.score(x_test, y_test)
    #    return train_score, test_score

    degrees = range(1, 8)
    training_scores = list()
    test_scores = list()
    for i in degrees:
        svc = svm.SVC(kernel="poly", degree=i)
        svc.fit(x_train, y_train)
        training_scores.append(svc.score(x_train, y_train))
        test_scores.append(svc.score(x_test, y_test))
    degree_best = test_scores.index(max(test_scores)) + 1
    plot_score_vs_degree(training_scores, test_scores, degrees)
    print("Polynomial kernel:")
    print("Best degree is ", degree_best, " with testing score=",
          max(test_scores))
    poly_best = svm.SVC(kernel="poly", degree=degree_best, coef0=1)
    poly_best.fit(x_train, y_train)
예제 #9
0
def ex_2_b(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 b)
    :param x_train: Training samples (2-dimensional)
    :param y_train: Training labels
    :param x_test: Testing samples (2-dimensional)
    :param y_test: Testing labels
    :return:
    """
    ###########
    ## TODO:
    ## Train SVMs with polynomial kernels for different values of the degree
    ## (Remember to set the 'coef0' parameter to 1)
    ## and plot the variation of the training and test scores with polynomial degree using 'plot_score_vs_degree' func.
    ## Plot the decision boundary and support vectors for the best value of degree
    ## using 'plot_svm_decision_boundary' function
    ###########
    degrees = range(1, 21)
    train_scores = list()
    test_scores = list()
    for i in degrees:
        clf = svm.SVC(kernel='poly', degree=i, coef0=1)
        clf.fit(x_train, y_train)
        test_scores.append(clf.score(x_test, y_test))
        train_scores.append(clf.score(x_train, y_train))
    plot_score_vs_degree(train_scores, test_scores, degrees)
    j = max(test_scores)
    best_degree = 0
    for i in range(0, 21):
        if test_scores[i] == j:
            best_degree = i
            clf = svm.SVC(kernel='poly', degree=i, coef0=1)
            clf.fit(x_train, y_train)
            plot_svm_decision_boundary(clf, x_train, y_train, x_test, y_test)
            print(clf.score(x_test, y_test))
            print(best_degree)
            break
예제 #10
0
파일: svm.py 프로젝트: sugano-nu/uni17ci
def ex_2_b(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 b)
    :param x_train: Training samples (2-dimensional)
    :param y_train: Training labels
    :param x_test: Testing samples (2-dimensional)
    :param y_test: Testing labels
    :return:
    """
    ###########
    ## TODO:
    ## Train SVMs with polynomial kernels for different values of the degree
    ## (Remember to set the 'coef0' parameter to 1)
    ## and plot the variation of the test and training scores with polynomial degree using 'plot_score_vs_degree' func.
    ## Plot the decision boundary and support vectors for the best value of degree
    ## using 'plot_svm_decision_boundary' function
    ###########
    degrees = np.array(range(1, 20))
    best_score = -1
    train_scores = np.zeros(degrees.shape[0])
    test_scores = np.zeros(degrees.shape[0])
    for i, d in np.ndenumerate(degrees):
        clf = svm.SVC(kernel="poly", degree=d, coef0=1)
        clf.fit(x_train, y_train)
        train_scores[i] = clf.score(x_train, y_train)
        score = clf.score(x_test, y_test)
        test_scores[i] = score
        # print("Score (degree: " + str(d) + "): " + str(score))
        if score > best_score:
            best_score = score
            best_clf = clf

    print("Best score is " + str(np.max(test_scores)) + " at degree = " +
          str(degrees[np.argmax(test_scores)]))
    plot_score_vs_degree(train_scores, test_scores, degrees)
    plot_svm_decision_boundary(best_clf, x_train, y_train, x_test, y_test)
예제 #11
0
파일: svm.py 프로젝트: RHohensinner/ci2018
def ex_2_b(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 b)
    :param x_train: Training samples (2-dimensional)
    :param y_train: Training labels
    :param x_test: Testing samples (2-dimensional)
    :param y_test: Testing labels
    :return:
    """
    ###########
    ## TODO:
    ## Train SVMs with polynomial kernels for different values of the degree
    ## (Remember to set the 'coef0' parameter to 1)
    ## and plot the variation of the test and training scores with polynomial degree using 'plot_score_vs_degree' func.
    ## Plot the decision boundary and support vectors for the best value of degree
    ## using 'plot_svm_decision_boundary' function
    ###########

    # given degree values
    degrees = range(1, 20)

    # helper variables
    m = 0
    coef_value = 1
    kernel_mode = 'poly'

    all_train_scores = []
    temp_train = 0
    all_test_scores = []
    temp_test = 0
    temp_highscore = 0
    top_degree = 0

    # for loop over all 20 degrees, saving all results inbetween
    for m in degrees:
        # init non-linear svm
        nonlin_svm = svm.SVC(kernel=kernel_mode, coef0=coef_value, degree=m)
        # train non-linear svm
        nonlin_svm.fit(x_train, y_train)
        # calc scores
        temp_train = nonlin_svm.score(x_train, y_train)
        temp_test = nonlin_svm.score(x_test, y_test)
        # update highscore
        if (temp_test > temp_highscore):
            temp_highscore = temp_test
            top_degree = m
        # save scores
        all_train_scores.append(temp_train)
        all_test_scores.append(temp_test)

    #
    print("top degree: ", top_degree)
    print("top score: ", temp_highscore)

    # plotting scores
    plot_score_vs_degree(all_train_scores, all_test_scores, degrees)

    # recreate best svm and plotting it
    best_svm = svm.SVC(kernel=kernel_mode, coef0=coef_value, degree=top_degree)
    best_svm.fit(x_train, y_train)
    plot_svm_decision_boundary(best_svm, x_train, y_train, x_test, y_test)