예제 #1
0
def example(num_samples=10, num_features=2, grid_size=20, filename="svm.pdf"):
    samples = np.array(
        np.random.normal(size=num_samples * num_features).reshape(
            num_samples, num_features))
    labels = 2 * (samples.sum(axis=1) > 0) - 1.0
    data_dict = build_data_dict(samples, labels)
    svm = SVM(data=data_dict, kernel=Kernel.linear(), c=0.1)
    svm.fit()

    plot(svm, samples, labels, grid_size, filename)
예제 #2
0
def custom_SVM_test_function(X_train, y_train, X_test):
    data_dict = dataset_reader.build_data_dict(X_train, y_train)

    svm = custom_svm.SVM(data=data_dict, kernel=Kernel.linear(), c=1)
    svm.fit()

    y_pred = []
    print(X_test[0])

    for x in X_test:
        y_pred.append(svm.predict(x))

    return y_pred
예제 #3
0
# positives = [[4,4], [5,5]]
positives = [[5, 1], [6, -1], [7, 3]]

# positives = [[-0.23530383,  1.70585848],
#             [ 0.8157926 ,  0.04591391],
#             [ 0.03237168,  1.36243792]]

# negatives = [[2,1], [1,2]]
negatives = [[1, 7], [2, 8], [3, 8]]

# negatives = [[-0.07810244, -0.65502153],
#             [ 0.25648505, -0.79438534],
#             [-0.83531028, -0.18554141],
#             [ 0.41896733, -0.73003242],
#             [-1.00007796,  0.00366544],
#             [-1.58005843,  0.83875439],
#             [ 0.77187267, -1.67242829]]

data_dict = {-1: np.array(negatives), 1: np.array(positives)}

num_samples = len(positives) + len(negatives)
samples = np.append(np.array(positives),
                    np.array(negatives)).reshape(num_samples, 2)
labels = np.append(np.ones(len(positives)), np.ones(len(negatives)) * -1)
svm = SVM(data=data_dict, kernel=Kernel.linear(), c=0.1)
svm.fit()

plot(svm, samples, labels, 20, "svm_run.pdf")

#print(svm.predict(test_data))
예제 #4
0
import matplotlib.pyplot as plt
import svm
from kernels import Kernel

iris = datasets.load_iris()
y = iris.target
y = np.asarray([-1 if x == 0 else x for x in y])
sel = [y != 2]
y = y[sel]
X = iris.data[:, :2]
X = X[sel]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)

C = 0.1
trainer = svm.SVMTrainer(kernel=Kernel.linear(), c=C)
model = trainer.train(X_train, y_train)

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
h = 0.02
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

xxyy = np.stack((xx.ravel(), yy.ravel()), axis=-1)

result = []
for i in range(len(xxyy)):
    result.append(model.predict(xxyy[i]))

Z = np.array(result).reshape(xx.shape)