Harigami
ログイン
anonymous タイトルなし
No License Python
コピー
# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

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

def phi3(x):
    return x[0]*x[1]*math.pi
def XI(x):
    return (math.sin(x[1])*(math.sin(phi3(x)))**2+math.sin(x[0])*(math.cos(phi3(x)))**2+math.cos(x[0])*math.cos(x[1])*math.sin(phi3(x)))*math.sin(x[0])/4
def YI(x):
    return (-math.sin(x[1])*math.cos(x[0])*(math.sin(phi3(x)))**2-math.cos(x[0])*math.sin(x[0])*(math.cos(phi3(x)))**2+((math.sin(x[0]))**2)*math.cos(x[1])*math.sin(phi3(x)))/4
def ZI(x):
    return math.cos(x[0])*math.cos(phi3(x))/4
def IX(x):
    return (math.sin(x[0])*(math.sin(phi3(x)))**2+math.sin(x[1])*(math.cos(phi3(x)))**2+math.cos(x[0])*math.cos(x[1])*math.sin(phi3(x)))*math.sin(x[1])/4
def XX(x):
    return (((math.sin(x[1]))**2)*(math.sin(phi3(x)))**2+math.cos(x[0])*math.cos(x[1])*math.sin(phi3(x))*(math.sin(x[0])+math.sin(x[1])))/4
def YX(x):
    return (-math.sin(x[0])*math.cos(x[0])*(math.sin(x[1]))**2+math.sin(phi3(x))*math.cos(x[1])*(math.sin(x[0])*math.sin(x[1])-math.cos(x[0])*math.cos(x[0])))/4
def ZX(x):
    return (-math.sin(x[0])*math.cos(x[1])*math.sin(phi3(x))+math.cos(x[0])*(math.sin(x[1]))**2+math.sin(x[1])*math.cos(x[1])*math.sin(phi3(x)))*math.cos(phi3(x))/4
def IY(x):
    return (-math.sin(x[0])*math.cos(x[1])*(math.sin(phi3(x)))**2-math.sin(x[1])*math.cos(x[1])*(math.cos(phi3(x)))**2+((math.sin(x[1]))**2)*math.cos(x[0])*math.sin(phi3(x)))/4
def XY(x):
    return (-math.sin(x[1])*math.cos(x[1])*(math.sin(x[0]))**2+math.sin(phi3(x))*math.cos(x[0])*(math.sin(x[0])*math.sin(x[1])-math.cos(x[1])*math.cos(x[1])))/4
def YY(x):
    return (math.sin(x[0])*math.cos(x[0])*math.sin(x[1])*math.cos(x[1])-math.sin(phi3(x))*(math.sin(x[0])*(math.cos(x[1]))**2+math.sin(x[1])*(math.cos(x[0]))**2))/4
def ZY(x):
    return (-math.sin(x[0])*math.sin(phi3(x))*math.cos(phi3(x))-math.cos(x[1])*math.cos(x[0])*math.cos(phi3(x))+math.sin(x[1])*math.cos(phi3(x))*math.sin(phi3(x)))*math.sin(x[1])/4
def IZ(x):
    return math.cos(x[1])*math.cos(phi3(x))/4
def XZ(x):
    return (-math.sin(x[1])*math.cos(x[0])*math.sin(phi3(x))+math.cos(x[1])*(math.sin(x[0]))**2+math.sin(x[0])*math.cos(x[0])*math.sin(phi3(x)))*math.cos(phi3(x))/4
def YZ(x):
    return (-math.sin(x[1])*math.sin(phi3(x))*math.cos(phi3(x))-math.cos(x[1])*math.cos(x[0])*math.cos(phi3(x))+math.sin(x[0])*math.cos(phi3(x))*math.sin(phi3(x)))*math.sin(x[0])/4
def ZZ(x):
    return math.cos(x[1])*math.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]
W = iris[1]
Y = W+W-1


def my_kernel(A, B):
    b = np.zeros((A.shape[0], B.shape[0]))
    for i in range(0,A.shape[0]):
        for j in range(0,B.shape[0]):
            x=A[i]
            y=B[j]
            PX=np.array([1/4,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)])
            PY=np.array([1/4,XI(y),YI(y),ZI(y),IX(y),XX(y),YX(y),ZX(y),IY(y),XY(y),YY(y),ZY(y),IZ(y),XZ(y),YZ(y),ZZ(y)])
            b[i][j] += np.dot(PX,PY)
    return b

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()
# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

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

def phi3(x):
    return x[0]*x[1]*math.pi
def XI(x):
    return (math.sin(x[1])*(math.sin(phi3(x)))**2+math.sin(x[0])*(math.cos(phi3(x)))**2+math.cos(x[0])*math.cos(x[1])*math.sin(phi3(x)))*math.sin(x[0])/4
def YI(x):
    return (-math.sin(x[1])*math.cos(x[0])*(math.sin(phi3(x)))**2-math.cos(x[0])*math.sin(x[0])*(math.cos(phi3(x)))**2+((math.sin(x[0]))**2)*math.cos(x[1])*math.sin(phi3(x)))/4
def ZI(x):
    return math.cos(x[0])*math.cos(phi3(x))/4
def IX(x):
    return (math.sin(x[0])*(math.sin(phi3(x)))**2+math.sin(x[1])*(math.cos(phi3(x)))**2+math.cos(x[0])*math.cos(x[1])*math.sin(phi3(x)))*math.sin(x[1])/4
def XX(x):
    return (((math.sin(x[1]))**2)*(math.sin(phi3(x)))**2+math.cos(x[0])*math.cos(x[1])*math.sin(phi3(x))*(math.sin(x[0])+math.sin(x[1])))/4
def YX(x):
    return (-math.sin(x[0])*math.cos(x[0])*(math.sin(x[1]))**2+math.sin(phi3(x))*math.cos(x[1])*(math.sin(x[0])*math.sin(x[1])-math.cos(x[0])*math.cos(x[0])))/4
def ZX(x):
    return (-math.sin(x[0])*math.cos(x[1])*math.sin(phi3(x))+math.cos(x[0])*(math.sin(x[1]))**2+math.sin(x[1])*math.cos(x[1])*math.sin(phi3(x)))*math.cos(phi3(x))/4
def IY(x):
    return (-math.sin(x[0])*math.cos(x[1])*(math.sin(phi3(x)))**2-math.sin(x[1])*math.cos(x[1])*(math.cos(phi3(x)))**2+((math.sin(x[1]))**2)*math.cos(x[0])*math.sin(phi3(x)))/4
def XY(x):
    return (-math.sin(x[1])*math.cos(x[1])*(math.sin(x[0]))**2+math.sin(phi3(x))*math.cos(x[0])*(math.sin(x[0])*math.sin(x[1])-math.cos(x[1])*math.cos(x[1])))/4
def YY(x):
    return (math.sin(x[0])*math.cos(x[0])*math.sin(x[1])*math.cos(x[1])-math.sin(phi3(x))*(math.sin(x[0])*(math.cos(x[1]))**2+math.sin(x[1])*(math.cos(x[0]))**2))/4
def ZY(x):
    return (-math.sin(x[0])*math.sin(phi3(x))*math.cos(phi3(x))-math.cos(x[1])*math.cos(x[0])*math.cos(phi3(x))+math.sin(x[1])*math.cos(phi3(x))*math.sin(phi3(x)))*math.sin(x[1])/4
def IZ(x):
    return math.cos(x[1])*math.cos(phi3(x))/4
def XZ(x):
    return (-math.sin(x[1])*math.cos(x[0])*math.sin(phi3(x))+math.cos(x[1])*(math.sin(x[0]))**2+math.sin(x[0])*math.cos(x[0])*math.sin(phi3(x)))*math.cos(phi3(x))/4
def YZ(x):
    return (-math.sin(x[1])*math.sin(phi3(x))*math.cos(phi3(x))-math.cos(x[1])*math.cos(x[0])*math.cos(phi3(x))+math.sin(x[0])*math.cos(phi3(x))*math.sin(phi3(x)))*math.sin(x[0])/4
def ZZ(x):
    return math.cos(x[1])*math.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]
W = iris[1]
Y = W+W-1


def my_kernel(A, B):
    b = np.zeros((A.shape[0], B.shape[0]))
    for i in range(0,A.shape[0]):
        for j in range(0,B.shape[0]):
            x=A[i]
            y=B[j]
            PX=np.array([1/4,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)])
            PY=np.array([1/4,XI(y),YI(y),ZI(y),IX(y),XX(y),YX(y),ZX(y),IY(y),XY(y),YY(y),ZY(y),IZ(y),XZ(y),YZ(y),ZZ(y)])
            b[i][j] += np.dot(PX,PY)
    return b

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()
コンソール
現在、コメントはありません。
他の人よりも先にコメントしましょう。