Example #1
0
def ex_2_c(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 c)
    :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 RBF kernels for different values of the gamma
    ## and plot the variation of the test and training scores with gamma using 'plot_score_vs_gamma' function.
    ## Plot the decision boundary and support vectors for the best value of gamma
    ## using 'plot_svm_decision_boundary' function
    ###########

    # given gamma values
    gammas = np.arange(0.01, 2, 0.02)

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

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

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

    #
    print("top gamma: ", top_gamma)
    print("top score: ", temp_highscore)

    # plotting scores
    plot_score_vs_gamma(all_train_scores, all_test_scores, gammas)

    # recreate best svm and plotting it
    best_svm = svm.SVC(kernel=kernel_mode, gamma=top_gamma)
    best_svm.fit(x_train, y_train)
    plot_svm_decision_boundary(best_svm, x_train, y_train, x_test, y_test)
Example #2
0
def ex_2_c(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 c)
    :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 RBF kernels for different values of the gamma
    ## and plot the variation of the test and training scores with gamma using 'plot_score_vs_gamma' function.
    ## Plot the decision boundary and support vectors for the best value of gamma
    ## using 'plot_svm_decision_boundary' function
    ###########
    gammas = np.arange(0.01, 2, 0.02)
    machines = [svm.SVC(kernel='rbf', gamma=g, coef0=1.0) for g in gammas]

    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_gamma(trainScores, testScores, gammas)

    bestGamma = np.argmax(testScores)
    print('Score of best rbf gamma ({}): {}'.format(gammas[bestGamma],
                                                    testScores[bestGamma]))
    plot_svm_decision_boundary(machines[bestGamma], x_train, y_train, x_test,
                               y_test)
Example #3
0
def ex_2_c(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 c)
    :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 RBF kernels for different values of the gamma
    ## and plot the variation of the test and training scores with gamma using 'plot_score_vs_gamma' function.
    ## Plot the decision boundary and support vectors for the best value of gamma
    ## using 'plot_svm_decision_boundary' function
    ###########
    gammas = np.arange(0.01, 2, 0.02)

    train_scores = list()
    test_scores = list()
    for i in gammas:
        clf = svm.SVC(kernel='rbf', gamma=i)
        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_gamma(train_scores, test_scores, gammas)

    j = max(test_scores)
    best_gamma = test_scores.index(j)

    clf = svm.SVC(kernel='rbf', gamma=gammas[best_gamma])
    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(gammas[best_gamma])
Example #4
0
File: svm.py Project: sebobo233/CI
def ex_2_c(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 c)
    :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 RBF kernels for different values of the gamma
    ## and plot the variation of the test and training scores with gamma using 'plot_score_vs_gamma' function.
    ## Plot the decision boundary and support vectors for the best value of gamma
    ## using 'plot_svm_decision_boundary' function
    ###########
    gammas = np.arange(0.01, 2, 0.02)

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

    for j in range(np.size(gammas)):
        svc = svm.SVC(kernel='rbf', gamma=gammas[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_gamma(train_scores, test_scores, gammas)

    acc_max = test_scores.argmax()
    svc = svm.SVC(kernel='rbf', gamma=gammas[acc_max]).fit(x_train, y_train)
    plot_svm_decision_boundary(svc, x_train, y_train, x_test, y_test)
Example #5
0
def ex_3_a(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 3 a)
    :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 multi-class SVMs with one-versus-rest strategy with
    ## - linear kernel
    ## - rbf kernel with gamma going from 10**-5 to 10**-3
    ## - plot the scores with varying gamma using the function plot_score_versus_gamma
    ## - Mind that the chance level is not .5 anymore and add the score obtained with the linear kernel as optional argument of this function
    ###########
    C = 3e-4
    linSVM = svm.SVC(kernel="linear", decision_function_shape='ovr', C=C)

    linSVM.fit(x_train, y_train)

    lin_score_train = linSVM.score(x_train, y_train)
    lin_score_test = linSVM.score(x_test, y_test)

    print("best_train_score for linear kernel: ", lin_score_train)
    print("best_test_score for linear kernel: ", lin_score_test)

    number_of_classes = linSVM.decision_function(x_test).shape[1]

    train_scores_rbf = []
    test_scores_rbf = []
    rbfSVMs = []

    gammas = np.linspace(1e-5, 1e-3, 12)
    for gamma in gammas:
        rbfSVM = svm.SVC(kernel="rbf", decision_function_shape='ovr', C=C)
        rbfSVM.set_params(gamma=gamma)
        rbfSVM.fit(x_train, y_train)

        train_scores_rbf.append(rbfSVM.score(x_train, y_train))
        test_scores_rbf.append(rbfSVM.score(x_test, y_test))
        rbfSVMs.append(rbfSVM)

    best_test_score_rbf_index = np.argmax(test_scores_rbf)
    print("gamma of best_test_rbf_score: ", gammas[best_test_score_rbf_index])
    print("best_test_score for rbf kernel: ",
          test_scores_rbf[best_test_score_rbf_index])

    # chance level depens on number of classes
    plot_score_vs_gamma(train_scores_rbf, test_scores_rbf, gammas,
                        lin_score_train, lin_score_test, 1 / number_of_classes)
Example #6
0
def ex_3_a(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 3 a)
    :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 multi-class SVMs with one-versus-rest strategy with
    # - linear kernel
    # - rbf kernel with gamma going from 10**-5 to 10**5
    # - plot the scores with varying gamma using the function plot_score_versus_gamma
    # - Mind that the chance level is not .5 anymore and add the score obtained
    #   with the linear kernel as optional argument of this function
    ###########

    gammas = [pow(10, i) for i in range(-5, 6)]

    svc = svm.SVC(C=10, kernel=RBF, decision_function_shape='ovr')
    train_scores = []
    test_scores = []

    for gamma in gammas:
        svc.gamma = gamma
        svc.fit(x_train, y_train)
        train_scores.append(svc.score(x_train, y_train))
        test_scores.append(svc.score(x_test, y_test))

    svc.kernel = LINEAR
    svc.gamma = 'auto'
    svc.fit(x_train, y_train)

    lin_score_test = svc.score(x_test, y_test)
    lin_score_train = svc.score(x_train, y_train)

    plot_score_vs_gamma(train_scores,
                        test_scores,
                        gammas,
                        lin_score_train=lin_score_train,
                        lin_score_test=lin_score_test,
                        baseline=.2)
    highest_error_rate = np.max(test_scores)
    print("Highest error rate: " + str(highest_error_rate))
Example #7
0
def ex_3_a(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 3 a)
    :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 multi-class SVMs with one-versus-rest strategy with
    ## - linear kernel
    ## - rbf kernel with gamma going from 10**-5 to 10**5
    ## - plot the scores with varying gamma using the function plot_score_versus_gamma
    ## - Note that the chance level is not .5 anymore and add the score obtained with the linear kernel as optional argument of this function (parameter baseline)
    ###########

    gammas = [1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 10, 1e2, 1e3, 1e4, 1e5]
    decision_function_shape = 'ovr'
    train_scores = list()
    test_scores = list()

    clf_lin = svm.SVC(kernel='linear', C=10)
    clf_lin.fit(x_train, y_train)
    test_scores_lin = (clf_lin.score(x_test, y_test))
    train_scores_lin = (clf_lin.score(x_train, y_train))
    for j in range(0, 11):

        clf = svm.SVC(decision_function_shape='ovr',
                      kernel='rbf',
                      gamma=gammas[j],
                      C=10)
        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_gamma(train_scores,
                        test_scores,
                        gammas,
                        lin_score_train=train_scores_lin,
                        lin_score_test=test_scores_lin,
                        baseline=.2)
Example #8
0
def ex_2_c(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 c)
    :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
    gammas = np.arange(0.01, 2, 0.02)
    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 g in gammas:
        clf = svm.SVC(C=C, kernel='rbf', gamma=g)
        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 g value: {}, with an accuracy of {}".format(
        gammas[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 g
    plot_score_vs_gamma(train_scores, test_scores, gammas)
Example #9
0
def ex_3_a(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 3 a)
    :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 multi-class SVMs with one-versus-rest strategy with
    ## - linear kernel
    ## - rbf kernel with gamma going from 10**-5 to 10**5
    ## - plot the scores with varying gamma using the function plot_score_versus_gamma
    ## - Mind that the chance level is not .5 anymore and add the score obtained with the linear kernel as optional argument of this function
    ###########
    SVMlin = svm.SVC(decision_function_shape='ovr', C=10, kernel='linear')
    SVMlin.fit(x_train, y_train)
    scorelin_train = SVMlin.score(x_train, y_train)
    scorelin_test = SVMlin.score(x_test, y_test)
    gammas = np.array(
        [1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 10, 1e2, 1e2, 1e3, 1e4, 1e5])
    # gammas2 = np.linspace(1e-5, 1e5, 11)
    scorerbf_train = np.zeros(np.array(gammas).shape[0])
    scorerbf_test = np.zeros(np.array(gammas).shape[0])

    for i in range(np.array(gammas).shape[0]):
        SVMrbf = svm.SVC(decision_function_shape='ovr',
                         C=10,
                         kernel='rbf',
                         gamma=gammas[i])
        SVMrbf.fit(x_train, y_train)
        scorerbf_train[i] = SVMrbf.score(x_train, y_train)
        scorerbf_test[i] = SVMrbf.score(x_test, y_test)

    plot_score_vs_gamma(scorerbf_train,
                        scorerbf_test,
                        gamma_list=gammas,
                        lin_score_train=scorelin_train,
                        lin_score_test=scorelin_test)
Example #10
0
def ex_3_a(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 3 a)
    :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 multi-class SVMs with one-versus-rest strategy with
    ## - linear kernel
    ## - rbf kernel with gamma going from 10**-5 to 10**-3
    ## - plot the scores with varying gamma using the function plot_score_versus_gamma
    ## - Mind that the chance level is not .5 anymore and add the score obtained with the linear kernel as optional argument of this function
    ###########

    clf = svm.SVC(kernel="linear", C=3e-4, decision_function_shape='ovr')
    clf.fit(x_train, y_train)
    lin_score_train = clf.score(x_train, y_train)
    lin_score_test = clf.score(x_test, y_test)
    print("Score (linear): " + str(lin_score_test))
    gamma_list = np.arange(10e-5, 10e-3, (10e-3 - 10e-5) / 10.0)
    train_scores = np.zeros(gamma_list.shape[0])
    test_scores = np.zeros(gamma_list.shape[0])
    for i, gamma in np.ndenumerate(gamma_list):
        clf = svm.SVC(kernel="rbf",
                      gamma=gamma,
                      decision_function_shape='ovr',
                      C=3e-4)
        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("Best score is " + str(np.max(test_scores)) + " at gamma = " +
          str(gamma_list[np.argmax(test_scores)]))
    plot_score_vs_gamma(train_scores, test_scores, gamma_list, lin_score_train,
                        lin_score_test, np.mean(test_scores))
Example #11
0
def ex_2_c(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 c)
    :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 RBF kernels for different values of the gamma
    ## and plot the variation of the test and training scores with gamma using 'plot_score_vs_gamma' function.
    ## Plot the decision boundary and support vectors for the best value of gamma
    ## using 'plot_svm_decision_boundary' function
    ###########
    gammas = np.arange(0.01, 2, 0.02)

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

    for gamma in gammas:
        clf = svm.SVC(kernel='rbf', gamma=gamma)
        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_gamma(train_scores, test_scores, gammas)

    plot_svm_decision_boundary(clf, x_train, y_train, x_test, y_test)
Example #12
0
File: svm.py Project: sebobo233/CI
def ex_3_a(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 3 a)
    :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 multi-class SVMs with one-versus-rest strategy with
    ## - linear kernel
    ## - rbf kernel with gamma going from 10**-5 to 10**-3
    ## - plot the scores with varying gamma using the function plot_score_versus_gamma
    ## - Mind that the chance level is not .5 anymore and add the score obtained with the linear kernel as optional argument of this function
    ###########

    gammas = np.power(10, np.linspace(5, -5, 11, endpoint=True))

    train_scores2 = np.zeros(np.size(gammas))
    test_scores2 = np.zeros(np.size(gammas))

    svc1 = svm.SVC(kernel='linear', decision_function_shape='ovr',
                   C=10).fit(x_train, y_train)
    test_scores1 = svc1.score(x_test, y_test)
    train_scores1 = svc1.score(x_train, y_train)

    for j in range(np.size(gammas)):
        svc2 = svm.SVC(kernel='rbf',
                       decision_function_shape='ovr',
                       gamma=gammas[j],
                       C=10).fit(x_train, y_train)
        test_scores2[j] = svc2.score(x_test, y_test)
        train_scores2[j] = svc2.score(x_train, y_train)

    plot_score_vs_gamma(train_scores2, test_scores2, gammas, train_scores1,
                        test_scores1, 0.2)
Example #13
0
def ex_2_c(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 c)
    :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 RBF kernels for different values of the gamma
    ## and plot the variation of the test and training scores with gamma using 'plot_score_vs_gamma' function.
    ## Plot the decision boundary and support vectors for the best value of gamma
    ## using 'plot_svm_decision_boundary' function
    ###########
    gammas = np.arange(0.01, 2, 0.02)

    train_scores = []
    test_scores = []
    rbfSVMs = []
    for gamma in gammas:
        rbfSVM = svm.SVC(kernel="rbf")
        rbfSVM.set_params(gamma=gamma)
        rbfSVM.fit(x_train, y_train)

        train_scores.append(rbfSVM.score(x_train, y_train))
        test_scores.append(rbfSVM.score(x_test, y_test))
        rbfSVMs.append(rbfSVM)

    best_test_score_index = np.argmax(test_scores)
    print("gamma of best_test_score: ", gammas[best_test_score_index])
    print("best_test_score for rbf kernel: ",
          test_scores[best_test_score_index])
    print("rbfSVm nSV: ", rbfSVM.support_vectors_.shape)
    plot_score_vs_gamma(train_scores, test_scores, gammas)
    plot_svm_decision_boundary(rbfSVMs[best_test_score_index], x_train,
                               y_train, x_test, y_test)
Example #14
0
def ex_2_c(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 c)
    :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: - done
    # Train SVMs with RBF kernels for different values of the gamma
    # and plot the variation of the test and training scores with gamma using 'plot_score_vs_gamma' function.
    # Plot the decision boundary and support vectors for the best value of gamma
    # using 'plot_svm_decision_boundary' function
    ###########
    gammas = np.arange(0.01, 2, 0.02)
    test_score = []
    train_score = []

    svc = svm.SVC(kernel=RBF)

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

    plot_score_vs_gamma(train_score, test_score, gammas)

    highest_test_score = max(test_score)
    highest_testscore_index = np.argmax(test_score)
    gamma = gammas[highest_testscore_index]
    print("A Gamma value of " + str(gamma) +
          " produces the Highest Test Score " + str(highest_test_score))
    svc.gamma = gamma
    svc.fit(x_train, y_train)
    plot_svm_decision_boundary(svc, x_train, y_train, x_test, y_test)
Example #15
0
def ex_3_a(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 3 a)
    :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:
    """
    ###########
    ## Train multi-class SVMs with one-versus-rest strategy with
    ## - linear kernel
    ## - rbf kernel with gamma going from 10**-5 to 10**5
    ## - plot the scores with varying gamma using the function plot_score_versus_gamma
    ## - Mind that the chance level is not .5 anymore and add the score obtained with the linear kernel as optional argument of this function
    ###########
    dfs = 'ovr'
    c = 10

    linear = svm.SVC(kernel='linear', C=c, decision_function_shape=dfs)
    linear.fit(x_train, y_train)
    lin_score_train = linear.score(x_train, y_train)
    lin_score_test = linear.score(x_test, y_test)

    gammas = [pow(10, i) for i in range(-5, 6)]
    rbfs = [
        svm.SVC(kernel='rbf', gamma=gamma, C=c, decision_function_shape=dfs)
        for gamma in gammas
    ]
    for rbf in rbfs:
        rbf.fit(x_train, y_train)

    train_scores = [rbf.score(x_train, y_train) for rbf in rbfs]
    test_scores = [rbf.score(x_test, y_test) for rbf in rbfs]

    plot_score_vs_gamma(train_scores, test_scores, gammas, lin_score_train,
                        lin_score_test, .2)
Example #16
0
def ex_3_a(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 3 a)
    :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 multi-class SVMs with one-versus-rest strategy with
    ## - linear kernel
    ## - rbf kernel with gamma going from 10**-5 to 10**5
    ## - plot the scores with varying gamma using the function plot_score_versus_gamma
    ## - Note that the chance level is not .5 anymore and add the score obtained with the linear kernel as optional argument of this function (parameter baseline)
    ###########

    gamma_range = [10**-5, 10**-4, 10**-3, 10**-2, 10**-1, 10**0, 10**1, 10**2, 10**3, 10**4, 10**5]

    lin = svm.SVC(decision_function_shape='ovr', kernel='linear', C=10)
    lin.fit(x_train, y_train)

    score_train = lin.score(x_train, y_train)
    score_test = lin.score(x_test, y_test)

    gam_score_train = []
    gam_score_test = []
    for gamma_value in gamma_range:
        gam = svm.SVC(decision_function_shape='ovr', kernel='rbf', gamma=gamma_value, C=10)
        gam.fit(x_train, y_train)

        gam_score_train.append(gam.score(x_train, y_train))
        gam_score_test.append(gam.score(x_test, y_test))

    plot_score_vs_gamma(gam_score_train, gam_score_test, gamma_range, score_train, score_test, baseline=0.2)
Example #17
0
def ex_2_c(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 c)
    :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 RBF kernels for different values of the gamma
    ## and plot the variation of the test and training scores with gamma using 'plot_score_vs_gamma' function.
    ## Plot the decision boundary and support vectors for the best value of gamma
    ## using 'plot_svm_decision_boundary' function
    ###########
    gammas = np.arange(0.01, 2, 0.02)

    test_score = np.zeros(np.array(gammas).shape[0])
    train_score = np.zeros(np.array(gammas).shape[0])

    for i in range(np.array(gammas).shape[0]):
        SVMrbf = svm.SVC(kernel="rbf", gamma=gammas[i])
        SVMrbf.fit(x_train, y_train)
        test_score[i] = SVMrbf.score(x_test, y_test)
        train_score[i] = SVMrbf.score(x_train, y_train)

    max_score_index = np.argmax(test_score)
    opt_gamma = gammas[max_score_index]

    SVMrbf_opt = svm.SVC(kernel="rbf", gamma=opt_gamma)
    SVMrbf_opt.fit(x_train, y_train)
    plot_svm_decision_boundary(SVMrbf_opt, x_train, y_train, x_test, y_test)
    print("Optimal gamma", opt_gamma, "Optimal score",
          test_score[max_score_index])
    plot_score_vs_gamma(train_score, test_score, gammas)
Example #18
0
def svm_rbf(x_train, y_train, x_test, y_test):
    # RBF kernel
    #    svc = svm.SVC()
    #    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
    #
    gammas = np.arange(0.01, 2, 0.02)
    training_scores = list()
    test_scores = list()
    for g in gammas:
        svc = svm.SVC(kernel='rbf', gamma=g)
        svc.fit(x_train, y_train)
        training_scores.append(svc.score(x_train, y_train))
        test_scores.append(svc.score(x_test, y_test))
    test_best = test_scores.index(max(test_scores))
    best_gamma = gammas[test_best]
    plot_score_vs_gamma(training_scores, test_scores, gammas)
    print("RBF kernel:")
    print("Best gamma is ", best_gamma, " with testing score=",
          max(test_scores))
    rbf_best = svm.SVC(gamma=best_gamma)
    rbf_best.fit(x_train, y_train)
Example #19
0
def ex_2_c(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 2 c)
    :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 RBF kernels for different values of the gamma
    ## and plot the variation of the test and training scores with gamma using 'plot_score_vs_gamma' function.
    ## Plot the decision boundary and support vectors for the best value of gamma
    ## using 'plot_svm_decision_boundary' function
    ###########
    gammas = np.arange(0.01, 2, 0.02)
    best_score = -1
    train_scores = np.zeros(gammas.shape[0])
    test_scores = np.zeros(gammas.shape[0])
    for i, gamma in np.ndenumerate(gammas):
        clf = svm.SVC(kernel="rbf", gamma=gamma)
        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 gamma = " +
          str(gammas[np.argmax(test_scores)]))

    plot_score_vs_gamma(train_scores, test_scores, gammas)
    plot_svm_decision_boundary(best_clf, x_train, y_train, x_test, y_test)
Example #20
0
def ex_3_a(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 3 a)
    :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 multi-class SVMs with one-versus-rest strategy with
    ## - linear kernel
    ## - rbf kernel with gamma going from 10**-5 to 10**5
    ## - plot the scores with varying gamma using the function plot_score_versus_gamma
    ## - Mind that the chance level is not .5 anymore and add the score obtained with the linear kernel as optional argument of this function
    ###########

    # helper variables
    c = 10
    kernel_mode_lin = 'linear'
    kernel_mode_rbf = 'rbf'
    df_shape = 'ovr'
    #gammas = np.arange(10**(-5), 10**(5), 20000)
    gammas = [
        10**(-5), 10**(-4), 10**(-3), 10**(-2), 10**(-1), 10**(0), 10**(1),
        10**(2), 10**(3), 10**(4), 10**(5)
    ]
    #gammas2 = np.linspace(10**(-5), 10**(5), 10)

    # init linear svm and train
    lin_svm = svm.SVC(kernel=kernel_mode_lin,
                      C=c,
                      decision_function_shape=df_shape)
    lin_svm.fit(x_train, y_train)

    # calc lin scores
    lin_trainscore = lin_svm.score(x_train, y_train)
    lin_testscore = lin_svm.score(x_test, y_test)

    print("LINEAR: \n", "trainscore: ", lin_trainscore, "testscore: ",
          lin_testscore)

    # init rbf svm and train it looping over gammas

    rbf_svm = svm.SVC(kernel=kernel_mode_rbf,
                      C=c,
                      decision_function_shape=df_shape)
    rbf_trainscore = []
    temp_train = 0
    rbf_testscore = []
    temp_test = 0

    for m in gammas:
        # setting current gamma
        rbf_svm.gamma = m

        # training
        rbf_svm.fit(x_train, y_train)

        # calc scores
        temp_train = rbf_svm.score(x_train, y_train)
        temp_test = rbf_svm.score(x_test, y_test)

        # save scores
        rbf_trainscore.append(temp_train)
        rbf_testscore.append(temp_test)

    print("RBF: \n", "trainscore: ", max(rbf_trainscore), "testscore: ",
          max(rbf_testscore))

    plot_score_vs_gamma(rbf_trainscore, rbf_testscore, gammas, lin_trainscore,
                        lin_testscore, 0.2)
Example #21
0
def ex_3_a(x_train, y_train, x_test, y_test):
    """
    Solution for exercise 3 a)
    :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

    C = 0.0003

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

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

    #first the linear and then the rbf
    clf = svm.SVC(C=C, kernel='linear', decision_function_shape='ovr')
    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))

    # gammas = np.linspace(0.00001, 0.001, 10)
    # for g in gammas:
    #     clf = svm.SVC(C=C, kernel='rbf', gamma=g,decision_function_shape='ovr')
    #     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))
    #
    # # plot the score depending of g , with linear score
    # plot_score_vs_gamma(train_scores[1:], test_scores[1:], gammas,train_scores[0],test_scores[0])

    gammas = np.linspace(0.00001, 0.001, 10)
    for g in gammas:
        clf = svm.SVC(C=C,
                      kernel='rbf',
                      gamma=g,
                      decision_function_shape='ovr')
        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))

    # plot the score depending of g , with linear score
    plot_score_vs_gamma(train_scores[1:], test_scores[1:], gammas,
                        train_scores[0], test_scores[0])