使用sklearn SVC的SVM

时间:2019-01-09 10:30:36

标签: python

我有一个问题,而不是代码,但是我正在尝试根据Schölkopf“支持向量机作为概率模型”中提到的内容来实现SVM的概率“版本”。起初我以为sk.learn包就是这样做的,但是我开始怀疑我现在拥有的代码,我意识到这确实很糟糕,但我希望你能理解这个概念:

import numpy as np
import random
from sklearn.svm import SVC
import math
from scipy.optimize import minimize
import matplotlib.pyplot as plt
from sklearn import decomposition
from sklearn.model_selection import GridSearchCV

#numpy.random.seed(100)

def training_set(name):
    pos = []
    neg = []
    file = open(name, "r")

    for line in file:
        inputs = line.split()
        numbers = []
        for i in range(7):
            numbers.append(float(inputs[i]))

        if inputs[7] == 'Yes':
            pos.append(numbers)
        else:
            neg.append(numbers)

    pos = np.array(pos)
    neg = np.array(neg)
    file.close()

    return pos, neg


pos, neg = training_set("data.txt")
inputs = np.concatenate((pos, neg))
targets = np.concatenate((np.ones(pos.shape[0]), -np.ones(neg.shape[0])))
N = inputs.shape[0]  # Number of rows ( samples )
#test
pos1, neg1 = training_set("test.txt")
inputs_test = np.concatenate((pos1, neg1))
targets_test = np.concatenate((np.ones(pos1.shape[0]), -np.ones(neg1.shape[0])))
permute = list(range(N))
random.shuffle(permute)
inputs_test = inputs_test[permute, :]
targets_test = targets_test[permute]
#test

permute = list(range(N))
random.shuffle(permute)
inputs = inputs[permute, :]
targets = targets[permute]
clf = SVC(gamma='auto')
param_grid = {'C': [0.01, 0.1, 1, 10, 100], 'kernel': ['rbf', 'linear']}
clf = GridSearchCV(SVC(class_weight='balanced'), param_grid)
clf.fit(inputs, targets)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma='[0.01, 0.0001]', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)
#print(clf.predict([inputs[0]]))

def checking_targets():
    stored_predictions = []
    for i in range(len(targets)):
        stored_predictions.append(int(clf.predict([inputs_test[i]])[0]))
    return stored_predictions

stored_predictions = checking_targets()


def checking_probs():
    list_of = []
    for i in range(len(targets)):
        if stored_predictions[i] == targets_test[i]:
            list_of.append(1)
    total = sum(list_of)
    prob = total/len(targets_test)
    return prob

print(checking_probs())

任何人都可以帮助我理解这个概念吗,sklearn-package实际上是Schölkopf上的一个实现,还是真正计算出了什么?预先感谢

0 个答案:

没有答案