挖个坑先
参考书籍为机器学习实践,之后会做详细解释
SGD算法import numpy as np
import matplotlib.pyplot as plt
import sklearn.datasets as datasets
def relu(x):
if x>0:
return x
else:
return 0
def gradient_descent(loss,w,b,exp_loss,x,y,lr=0.0001,lambda0=0.1):#这里没有加lambda
C=1000
if exp_loss==0:
b=b-lr*0
w=w-lr*0#2*w*lambda0
else:
b=b-lr*(-y)*C
w=w-lr*(-y*x)*C
return w,b
def Hingeloss_gradient_descent(y_pre,y_label,weight,x,b,lr=0.000001):
lam=1
loss=relu(1-y_label * y_pre)+np.linalg.norm(weight)*lam
weight,b=gradient_descent(loss,weight,b,relu(1-y_label*(y_pre)),x,y_label,lr=lr,lambda0=lam)
return weight,b
def svm(data,label,epoch):
w=np.zeros((np.shape(data)[1],1)).T
b=0
for i in range(epoch):
for j in range(np.shape(data)[0]):
y_pre=np.matmul(w,data[j,:])+b
w,b=Hingeloss_gradient_descent(y_pre,label[j],w,data[j],b)
return w,b
def get_data():
fig = plt.figure()
x, y1 =datasets.make_circles(n_samples=100, factor=0.1, noise=0.1)
plt.scatter(x[:, 0], x[:, 1], marker='o', c=y1)
plt.pause(2)
for i in range(len(y1)):
if y1[i]==0:
y1[i]=-1
return x,y1
def process(_x):
'''
映射到高维核空间
:param data_point:
:param data_noise:
:return:
'''
Z = np.zeros([100, 3])
# 二项式映射
# X[:,0] = _x[:,0]**2
# X[:, 1] = math.sqrt(2)*_x[:,0]*_x[:,1]
# X[:,2] = _x[:,1]**2
# 高斯核映射
Z[:, 0] = np.exp(-(_x[:, 0] ** 2)) * np.exp(-(_x[:, 1] ** 2))
Z[:, 1] = 2 * _x[:, 0] * _x[:, 1] * np.exp(-(_x[:, 0] ** 2)) * np.exp(-(_x[:, 1] ** 2))
Z[:, 2] = 2 * _x[:, 0] ** 2 * _x[:, 1] ** 2 * np.exp(-(_x[:, 0] ** 2)) * np.exp(-(_x[:, 1] ** 2))
return Z
#
# sample_size=100
# np.random.seed(0)
# x=np.random.random((2*sample_size,2))
# x[:sample_size,0]+=0.2
# x[sample_size:,1]-=5
#
# y=np.array([0]*2*sample_size)
# y[:sample_size]+=1
# y[sample_size:]-=1
# print('y{}'.format(y))
# plt.scatter(x[:sample_size,0],x[:sample_size,1],c='r')
# plt.scatter(x[sample_size:,0],x[sample_size:,1],c='y')
# plt.pause(2)
# w,b=svm(x,y,700)
# for i in range(2*sample_size):
# if (np.matmul(w,x[i])+b)>0:
# plt.scatter(x[i,0],x[i,1],c='blue')
# elif (np.matmul(w,x[i])+b)<0:
# plt.scatter(x[i, 0], x[i, 1], c='red')
# plt.pause(5)
X,y=get_data()
x=process(X)
# x=X
print(X,y)
w,b=svm(x,y,700)
for i in range(np.shape(X)[0]):
if np.matmul(w,x[i])+b > 0:
plt.scatter(X[i,0],X[i,1],c='red')
print(np.matmul(w,x[i])+b)
elif np.matmul(w,x[i])+b < 0:
plt.scatter(X[i, 0], X[i, 1], c='blue')
plt.pause(8)
print(b,w)
# w=alpha*y*x.T[[0.07103269 0.06852622]] -2.439454888092385e-19
SMO算法
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
import torch
import time
iris =datasets.load_iris()
def get_data():
fig = plt.figure()
x, y1 =datasets.make_circles(n_samples=100, factor=0.1, noise=0.1)
plt.scatter(x[:, 0], x[:, 1], marker='o', c=y1)
plt.pause(2)
return x,y1
def process(_x):
'''
映射到高维核空间
:param data_point:
:param data_noise:
:return:
'''
Z = np.zeros([100, 3])
# 二项式映射
# X[:,0] = _x[:,0]**2
# X[:, 1] = math.sqrt(2)*_x[:,0]*_x[:,1]
# X[:,2] = _x[:,1]**2
# 高斯核映射
Z[:, 0] = np.exp(-(_x[:, 0] ** 2)) * np.exp(-(_x[:, 1] ** 2))
Z[:, 1] = 2 * _x[:, 0] * _x[:, 1] * np.exp(-(_x[:, 0] ** 2)) * np.exp(-(_x[:, 1] ** 2))
Z[:, 2] = 2 * _x[:, 0] ** 2 * _x[:, 1] ** 2 * np.exp(-(_x[:, 0] ** 2)) * np.exp(-(_x[:, 1] ** 2))
return Z
def selectJ(i,samples_num):
j=i
while j==i:
j=int(np.random.uniform(0,samples_num))
return j
def clipAlpha(L,H,alpha):
if alpha>H:
alpha= H
if alpha toler and alpha[i] > 0): # 和书上kkt不一样为什么?
j = selectJ(i, m)
gx_j = float(np.multiply(alpha, labelMat).T * (dataMat * dataMat[j, :].T)) + b
Ej = gx_j - float(labelMat[j])
alpha_i_old = alpha[i].copy()
alpha_j_old = alpha[j].copy()
print(i, itera, gx_i, gx_j,b,np.sum(alpha))
time.sleep(0.01)
if labelMat[i] != labelMat[j]:
L = max(0, alpha[j] - alpha[i])
H = min(C, C + alpha[j] - alpha[i])
else:
L = max(0, alpha[j] + alpha[i] - C)
H = min(C, alpha[j] + alpha[i])
if L==H:
print('L==H')
continue
eta=2.0*dataMat[i,:]*dataMat[j,:].T-dataMat[i,:]*dataMat[i,:].T-dataMat[j,:]*dataMat[j,:].T#什么东西这是?epsilon
print(eta)
if eta>=0:
print('eta>=0')#why????????????
continue
alpha[j] -= labelMat[j] * (Ei - Ej) / eta # +or->>>>
alpha[j] = clipAlpha(L, H, alpha[j])
if abs(alpha[j] - alpha_j_old) < 0.00001:
print('j not moving enough')
continue
alpha[i] += labelMat[j] * labelMat[i] * (alpha_j_old - alpha[j])
b1 = b - Ei - labelMat[i] * (alpha[i] - alpha_i_old) * dataMat[i, :] * dataMat[i, :].T - labelMat[
j] * (alpha[j] - alpha_j_old) * dataMat[i, :] * dataMat[j, :].T
b2 = b - Ej - labelMat[i] * (alpha[i] - alpha_i_old) * dataMat[i, :] * dataMat[j, :].T - labelMat[
j] * (alpha[j] - alpha_j_old) * dataMat[j, :] * dataMat[j, :].T
if alpha[i] > 0 and alpha[i] < C:
b = b1
elif alpha[j] > 0 and alpha[j] < C:
b=b2
else:
b=(b1+b2)/2.0
alphaPairChange+=1
print(("iter{},i:{},pairchange{}".format(itera,i,alphaPairChange)))
if (alphaPairChange==0):
itera+=1
else:
itera=0
print('iteration{},m{}'.format(itera,i))
return b,alpha
#
# x0,y0=get_data()
# x=process(x0)
# print(x)
##线性可分
np.random.seed(0)
x=np.random.random((100,2))
x[:50,:]+=0.8
x[50:,:]-=0.8
print
y=np.array([0]*100)
y[:50]+=1
y[50:]-=1
print('y{}'.format(y))
plt.scatter(x[:50,0],x[:50,1],c='r')
plt.scatter(x[50:,0],x[50:,1],c='g')
# plt.pause(3)
b,alpha=smo(dataIn=x,classLabel=y,C=0.6,toler=0.001,maxIter=40)
print(b,alpha)
# w=alpha*y*x.T
#画出分割面
for i in range(100):
if alpha[i]>0.0:
print(x[i],y[i])
plt.scatter(x[i,0], x[i, 1], c='y')
plt.pause(10)
后记:一开始写博客是因为自己学的时候感觉合适的参考资料实在太少,自己写出来之后想让其他同学学习时少一点困难,不知道对大家有帮助吗,或者还有什么没写清楚的地方?
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)