下面的代码仅仅作为伪代码来看,因为拟合效果不好,思考是优化求解的问题,
仅有w_bar_2_1的解是收敛的,但是拟合效果很差。
svm实现要进一步思考。
import numpy as np
from sklearn import svm
from scipy.optimize import minimize
from numpy.linalg import norm
from sklearn import datasets
from functools import partial
import copy
iris = datasets.load_iris()
print iris.data.shape, iris.target.shape
#w_bar is [b ,w]
def func(w_bar):
return norm(w_bar[1:]) ** 2 / 2
def strain(data, target, w_bar):
return target * np.dot(data, w_bar) - 1
#print strain(np.array([[0, 1], [2, 3]]), np.array([0, 1]), np.array([0, 1]))
#print np.unique(iris.target, return_counts = True)
data = np.append(iris.data, iris.target.reshape(iris.target.shape[0], 1), axis = 1)
data_0 = np.ndarray(shape = (0, 5))
data_1 = np.ndarray(shape = (0, 5))
data_2 = np.ndarray(shape = (0, 5))
for i in range(data.shape[0]):
data_for_append = data[i,:].reshape(1, data[i,:].shape[0])
if data[i,-1] == 0:
data_0 = np.append(data_0, data_for_append, axis = 0)
elif data[i,-1] == 1:
data_1 = np.append(data_1, data_for_append, axis = 0)
else:
data_2 = np.append(data_2, data_for_append, axis = 0)
def generate_one_svm(X_y):
#print X_y
X = np.ones([X_y.shape[0], X_y.shape[1]])
X[:,1:] = X_y[:,:-1]
y = X_y[:,-1].reshape(X_y.shape[0])
strain_0 = partial(strain, X, y)
cons = ({
'type': 'ineq',
'fun': strain_0
},)
res = minimize(func, np.ones([X.shape[1]]), constraints = cons ,\
method = 'S