import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
# 資料集
def create_data():
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df["label"] = iris.target
df.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'label']
data = np.array(df.iloc[:100, [0, 1, -1]])
for i in range(len(data)):
if data[i, -1] == 0:
data[i, -1] = -1
return data[:, :2], data[:, -1]
X, y = create_data()
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.25)
# plt.scatter(X[:50, 0], X[:50, 1], label='0')
# plt.scatter(X[50:, 0], X[50:, 1], label="1")
# plt.legend()
# plt.show()
class SVM:
def __init__(self, max_iter=100, kernel="linear"):
self.max_iter = max_iter
self._kernel = kernel
def init_args(self, features, labels):
self.m, self.n = features.shape
self.X = features
self.Y = labels
self.b = 0.0
# 将Ei儲存在一個清單裡
self.alpha = np.ones(self.m)
self.E = [self._E(i) for i in range(self.m)]
# 松弛變量
self.C = 1.0
# 核函數
# 參考《統計學習方法》P122 公式(7.88)
def kernel(self, x1, x2):
if self._kernel == "linear": # 線性核函數
return sum([x1[k] * x2[k] for k in range(self.n)])
elif self._kernel == "poly": # 多項式核函數
return (sum([x1[k] * x2[k] for k in range(self.n)]) + 1) ** 2
return 0
# g(x)為預測,輸入xi(X[i])
# 參考《統計學習方法》P127 公式(7.105)
def _g(self, i):
r = self.b
for j in range(self.m):
r += self.alpha[j] * self.Y[j] * self.kernel(self.X[i], self.X[j])
return r
# KKT條件
# 參考《統計學習方法》P113
def _KKT(self, i):
y_g = self._g(i) * self.Y[i]
if self.alpha[i] == 0:
return y_g >= 1
elif 0 < self.alpha[i] < self.C:
return y_g == 1
else:
return y_g <= 1
# E(x)為g(x)對輸入x的預測值和真實輸出y的差
# 參考《統計學習方法》P127 公式(7.105)
def _E(self, i):
return self._g(i) - self.Y[i]
def _init_alpha(self):
# 外層循環首先周遊所有滿足0<a<C的樣本點,檢驗是否滿足KKT
index_list = [i for i in range(self.m) if 0 < self.alpha[i] < self.C]
# 否則周遊整個訓練集
# 選擇出不滿足KKT點的樣本
non_satisfy_list = [i for i in range(self.m) if i not in index_list]
index_list.extend(non_satisfy_list)
for i in index_list:
if self._KKT(i):
continue
E1 = self.E[i]
# 如果E1是+,選擇最小的;如果E2是負的,選擇最大的
if E1 >= 0:
j = min(range(self.m), key=lambda x:self.E[x])
else:
j = max(range(self.m), key=lambda x:self.E[x])
return i, j
# 參考《統計學習方法》P127 公式(7.108)
def _compare(self, _alpha, L, H):
if _alpha > H:
return H
elif _alpha < L:
return L
else:
return _alpha
def fit(self, features, labels):
self.init_args(features, labels)
for t in range(self.max_iter):
# 訓練
i1, i2 =self._init_alpha()
# 邊界--參考《統計學習方法》P126 下面
if self.Y[i1] == self.Y[i2]:
L = max(0, self.alpha[i1] + self.alpha[i2] - self.C)
H = min(self.C, self.alpha[i1] + self.alpha[i2])
else:
L = max(0, self.alpha[i2] - self.alpha[i1])
H = min(self.C, self.C + self.alpha[i2] - self.alpha[i1])
E1 = self.E[i1]
E2 = self.E[i2]
# eta=K11+K22-2K12
# 參考《統計學習方法》P127 公式(7.107)
eta = self.kernel(self.X[i1], self.X[i1]) + self.kernel(self.X[i2], self.X[i2]) - 2 * self.kernel(X[i1], X[i2])
if eta <= 0:
continue
# 此處有修改,根據書上應該是E1 - E2,書上130-131頁
# 參考《統計學習方法》P127 公式(7.106)
alpha2_new_unc = self.alpha[i2] + self.Y[i2] * (E1 - E2) / eta
# 參考《統計學習方法》P127 公式(7.108)
alpha2_new = self._compare(alpha2_new_unc, L, H)
# 參考《統計學習方法》P127 公式(7.109)
alpha1_new = self.alpha[i1] + self.Y[i1] * self.Y[i2] * (self.alpha[i2] - alpha2_new)
# 參考《統計學習方法》P130 公式(7.115)
b1_new = - E1 - self.Y[i1] * self.kernel(self.X[i1], self.X[i1]) * (
alpha1_new - self.alpha[i1]) - self.Y[i2] * self.kernel(self.X[i2], self.X[i1]) * (
alpha2_new - self.alpha[i2]) + self.b
# 參考《統計學習方法》P130 公式(7.116)
b2_new = - E2 - self.Y[i1] * self.kernel(self.X[i1], self.X[i2]) * (
alpha1_new - self.alpha[i1]) - self.Y[i2] * self.kernel(self.X[i2], self.X[i2]) * (
alpha2_new - self.alpha[i2]) + self.b
# 參考《統計學習方法》P130 中間部分
if 0 < alpha1_new < self.C:
b_new = b1_new
elif 0 < alpha2_new < self.C:
b_new = b2_new
else:
# 選擇中點
b_new = (b1_new + b2_new) / 2
# 更新參數
self.alpha[i1] = alpha1_new
self.alpha[i2] = alpha2_new
self.b = b_new
self.E[i1] = self._E(i1)
self.E[i2] = self._E(i2)
return "train done!"
def predict(self, data):
r = self.b
for i in range(self.m):
r += self.alpha[i] * self.Y[i] * self.kernel(data, self.X[i])
return 1 if r > 0 else -1
def score(self, X_test, y_test):
right_count = 0
for i in range(len(X_test)):
result = self.predict(X_test[i])
if result == y_test[i]:
right_count += 1
return right_count / len(X_test)
def _weight(self):
# linear model
yx = self.Y.reshape(-1, 1) * self.X
self.w = np.dot(yx.T, self.alpha)
return self.w
svm = SVM(max_iter=200)
svm.fit(X_train, y_train)
print(svm.score(X_test, y_test))