Harigami
ログイン
anonymous タイトルなし
No License Python
コピー
"""
Spyder Editor

This is a temporary script file.
"""

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets


def phi3(x):
    return x[:, 0] * x[:, 1] * np.pi


def XI(x):
    return (np.sin(x[:, 1]) * (np.sin(phi3(x))) ** 2 + np.sin(x[:, 0]) * (np.cos(phi3(x))) ** 2 + np.cos(
        x[:, 0]) * np.cos(x[:, 1]) * np.sin(phi3(x))) * np.sin(x[:, 0]) / 4


def YI(x):
    return (-np.sin(x[:, 1]) * np.cos(x[:, 0]) * (np.sin(phi3(x))) ** 2 - np.cos(x[:, 0]) * np.sin(x[:, 0]) * (
        np.cos(phi3(x))) ** 2 + ((np.sin(x[:, 0])) ** 2) * np.cos(x[:, 1]) * np.sin(phi3(x))) / 4


def ZI(x):
    return np.cos(x[:, 0]) * np.cos(phi3(x)) / 4


def IX(x):
    return (np.sin(x[:, 0]) * (np.sin(phi3(x))) ** 2 + np.sin(x[:, 1]) * (np.cos(phi3(x))) ** 2 + np.cos(
        x[:, 0]) * np.cos(x[:, 1]) * np.sin(phi3(x))) * np.sin(x[:, 1]) / 4


def XX(x):
    return (((np.sin(x[:, 1])) ** 2) * (np.sin(phi3(x))) ** 2 + np.cos(x[:, 0]) * np.cos(x[:, 1]) * np.sin(phi3(x)) * (
            np.sin(x[:, 0]) + np.sin(x[:, 1]))) / 4


def YX(x):
    return (-np.sin(x[:, 0]) * np.cos(x[:, 0]) * (np.sin(x[:, 1])) ** 2 + np.sin(phi3(x)) * np.cos(x[:, 1]) * (
            np.sin(x[:, 0]) * np.sin(x[:, 1]) - np.cos(x[:, 0]) * np.cos(x[:, 0]))) / 4


def ZX(x):
    return (-np.sin(x[:, 0]) * np.cos(x[:, 1]) * np.sin(phi3(x)) + np.cos(x[:, 0]) * (np.sin(x[:, 1])) ** 2 + np.sin(
        x[:, 1]) * np.cos(x[:, 1]) * np.sin(phi3(x))) * np.cos(phi3(x)) / 4


def IY(x):
    return (-np.sin(x[:, 0]) * np.cos(x[:, 1]) * (np.sin(phi3(x))) ** 2 - np.sin(x[:, 1]) * np.cos(x[:, 1]) * (
        np.cos(phi3(x))) ** 2 + ((np.sin(x[:, 1])) ** 2) * np.cos(x[:, 0]) * np.sin(phi3(x))) / 4


def XY(x):
    return (-np.sin(x[:, 1]) * np.cos(x[:, 1]) * (np.sin(x[:, 0])) ** 2 + np.sin(phi3(x)) * np.cos(x[:, 0]) * (
            np.sin(x[:, 0]) * np.sin(x[:, 1]) - np.cos(x[:, 1]) * np.cos(x[:, 1]))) / 4


def YY(x):
    return (np.sin(x[:, 0]) * np.cos(x[:, 0]) * np.sin(x[:, 1]) * np.cos(x[:, 1]) - np.sin(phi3(x)) * (
            np.sin(x[:, 0]) * (np.cos(x[:, 1])) ** 2 + np.sin(x[:, 1]) * (np.cos(x[:, 0])) ** 2)) / 4


def ZY(x):
    return (-np.sin(x[:, 0]) * np.sin(phi3(x)) * np.cos(phi3(x)) - np.cos(x[:, 1]) * np.cos(x[:, 0]) * np.cos(
        phi3(x)) + np.sin(x[:, 1]) * np.cos(phi3(x)) * np.sin(phi3(x))) * np.sin(x[:, 1]) / 4


def IZ(x):
    return np.cos(x[:, 1]) * np.cos(phi3(x)) / 4


def XZ(x):
    return (-np.sin(x[:, 1]) * np.cos(x[:, 0]) * np.sin(phi3(x)) + np.cos(x[:, 1]) * (np.sin(x[:, 0])) ** 2 + np.sin(
        x[:, 0]) * np.cos(x[:, 0]) * np.sin(phi3(x))) * np.cos(phi3(x)) / 4


def YZ(x):
    return (-np.sin(x[:, 1]) * np.sin(phi3(x)) * np.cos(phi3(x)) - np.cos(x[:, 1]) * np.cos(x[:, 0]) * np.cos(
        phi3(x)) + np.sin(x[:, 0]) * np.cos(phi3(x)) * np.sin(phi3(x))) * np.sin(x[:, 0]) / 4


def ZZ(x):
    return np.cos(x[:, 1]) * np.cos(x[:, 0]) / 4


# import some data to play with
iris = datasets.make_circles(n_samples=100, shuffle=True, noise=None, random_state=None, factor=0.8)

x = iris[0]  # circle point data
W = iris[1]
Y = 2 * W - 1  # label data


def my_kernel(x1, x2):
    p1 = get_p_array(x1)
    p2 = get_p_array(x2)
    return np.dot(p1.T, p2)


def get_p_array(x):
    const = np.full(x.shape[0], 1 / 4)
    return np.array(
        [const, XI(x), YI(x), ZI(x), IX(x), XX(x), YX(x), ZX(x), IY(x), XY(x), YY(x), ZY(x), IZ(x), XZ(x), YZ(x),
         ZZ(x)])


h = .02  # step size in the mesh

# we create an instance of SVM and fit out data.
clf = svm.SVC(kernel=my_kernel)
clf.fit(x, Y)

# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1
y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)

# Plot also the training points
plt.scatter(x[:, 0], x[:, 1], c=Y, cmap=plt.cm.Paired, edgecolors='k')
plt.title('3-Class classification using Support Vector Machine with custom'
          ' kernel')
plt.axis('tight')
plt.show()
"""
Spyder Editor

This is a temporary script file.
"""

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets


def phi3(x):
    return x[:, 0] * x[:, 1] * np.pi


def XI(x):
    return (np.sin(x[:, 1]) * (np.sin(phi3(x))) ** 2 + np.sin(x[:, 0]) * (np.cos(phi3(x))) ** 2 + np.cos(
        x[:, 0]) * np.cos(x[:, 1]) * np.sin(phi3(x))) * np.sin(x[:, 0]) / 4


def YI(x):
    return (-np.sin(x[:, 1]) * np.cos(x[:, 0]) * (np.sin(phi3(x))) ** 2 - np.cos(x[:, 0]) * np.sin(x[:, 0]) * (
        np.cos(phi3(x))) ** 2 + ((np.sin(x[:, 0])) ** 2) * np.cos(x[:, 1]) * np.sin(phi3(x))) / 4


def ZI(x):
    return np.cos(x[:, 0]) * np.cos(phi3(x)) / 4


def IX(x):
    return (np.sin(x[:, 0]) * (np.sin(phi3(x))) ** 2 + np.sin(x[:, 1]) * (np.cos(phi3(x))) ** 2 + np.cos(
        x[:, 0]) * np.cos(x[:, 1]) * np.sin(phi3(x))) * np.sin(x[:, 1]) / 4


def XX(x):
    return (((np.sin(x[:, 1])) ** 2) * (np.sin(phi3(x))) ** 2 + np.cos(x[:, 0]) * np.cos(x[:, 1]) * np.sin(phi3(x)) * (
            np.sin(x[:, 0]) + np.sin(x[:, 1]))) / 4


def YX(x):
    return (-np.sin(x[:, 0]) * np.cos(x[:, 0]) * (np.sin(x[:, 1])) ** 2 + np.sin(phi3(x)) * np.cos(x[:, 1]) * (
            np.sin(x[:, 0]) * np.sin(x[:, 1]) - np.cos(x[:, 0]) * np.cos(x[:, 0]))) / 4


def ZX(x):
    return (-np.sin(x[:, 0]) * np.cos(x[:, 1]) * np.sin(phi3(x)) + np.cos(x[:, 0]) * (np.sin(x[:, 1])) ** 2 + np.sin(
        x[:, 1]) * np.cos(x[:, 1]) * np.sin(phi3(x))) * np.cos(phi3(x)) / 4


def IY(x):
    return (-np.sin(x[:, 0]) * np.cos(x[:, 1]) * (np.sin(phi3(x))) ** 2 - np.sin(x[:, 1]) * np.cos(x[:, 1]) * (
        np.cos(phi3(x))) ** 2 + ((np.sin(x[:, 1])) ** 2) * np.cos(x[:, 0]) * np.sin(phi3(x))) / 4


def XY(x):
    return (-np.sin(x[:, 1]) * np.cos(x[:, 1]) * (np.sin(x[:, 0])) ** 2 + np.sin(phi3(x)) * np.cos(x[:, 0]) * (
            np.sin(x[:, 0]) * np.sin(x[:, 1]) - np.cos(x[:, 1]) * np.cos(x[:, 1]))) / 4


def YY(x):
    return (np.sin(x[:, 0]) * np.cos(x[:, 0]) * np.sin(x[:, 1]) * np.cos(x[:, 1]) - np.sin(phi3(x)) * (
            np.sin(x[:, 0]) * (np.cos(x[:, 1])) ** 2 + np.sin(x[:, 1]) * (np.cos(x[:, 0])) ** 2)) / 4


def ZY(x):
    return (-np.sin(x[:, 0]) * np.sin(phi3(x)) * np.cos(phi3(x)) - np.cos(x[:, 1]) * np.cos(x[:, 0]) * np.cos(
        phi3(x)) + np.sin(x[:, 1]) * np.cos(phi3(x)) * np.sin(phi3(x))) * np.sin(x[:, 1]) / 4


def IZ(x):
    return np.cos(x[:, 1]) * np.cos(phi3(x)) / 4


def XZ(x):
    return (-np.sin(x[:, 1]) * np.cos(x[:, 0]) * np.sin(phi3(x)) + np.cos(x[:, 1]) * (np.sin(x[:, 0])) ** 2 + np.sin(
        x[:, 0]) * np.cos(x[:, 0]) * np.sin(phi3(x))) * np.cos(phi3(x)) / 4


def YZ(x):
    return (-np.sin(x[:, 1]) * np.sin(phi3(x)) * np.cos(phi3(x)) - np.cos(x[:, 1]) * np.cos(x[:, 0]) * np.cos(
        phi3(x)) + np.sin(x[:, 0]) * np.cos(phi3(x)) * np.sin(phi3(x))) * np.sin(x[:, 0]) / 4


def ZZ(x):
    return np.cos(x[:, 1]) * np.cos(x[:, 0]) / 4


# import some data to play with
iris = datasets.make_circles(n_samples=100, shuffle=True, noise=None, random_state=None, factor=0.8)

x = iris[0]  # circle point data
W = iris[1]
Y = 2 * W - 1  # label data


def my_kernel(x1, x2):
    p1 = get_p_array(x1)
    p2 = get_p_array(x2)
    return np.dot(p1.T, p2)


def get_p_array(x):
    const = np.full(x.shape[0], 1 / 4)
    return np.array(
        [const, XI(x), YI(x), ZI(x), IX(x), XX(x), YX(x), ZX(x), IY(x), XY(x), YY(x), ZY(x), IZ(x), XZ(x), YZ(x),
         ZZ(x)])


h = .02  # step size in the mesh

# we create an instance of SVM and fit out data.
clf = svm.SVC(kernel=my_kernel)
clf.fit(x, Y)

# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1
y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)

# Plot also the training points
plt.scatter(x[:, 0], x[:, 1], c=Y, cmap=plt.cm.Paired, edgecolors='k')
plt.title('3-Class classification using Support Vector Machine with custom'
          ' kernel')
plt.axis('tight')
plt.show()
コンソール
現在、コメントはありません。
他の人よりも先にコメントしましょう。