感知机算法
依赖
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
人工数据集
n = 100
X = np.random.multivariate_normal((1, 1), [[0.16, 0], [0, 0.16]], size = int(n/2))
X = np.insert(X, 50, np.random.multivariate_normal((3, 3), [[0.16, 0], [0,0.16]], size = int(n/2)),0)
X = np.insert(X, 0, 1, 1)
m = X.shape[1]
y = np.array([1]*50+[-1]*50).reshape(-1,1)
plt.scatter(X[:50, -2], X[:50, -1])
plt.scatter(X[50:, -2], X[50:, -1], c = "#ff4400")
<matplotlib.collections.PathCollection at 0x7fc34e108fd0>
损失函数
def compterCost(X, theta, y):
return -sum([e if e < 0 else 0 for e in y * X @ theta])
compterCost(X, np.array([[0],[1],[1]]), y)
array([296.74151517])
梯度
def getGradient(X, theta, y):
return np.array([e if e@theta < 0 else np.zeros(e.shape) for e in y*X]).sum(0).reshape([-1,1])
getGradient(X, np.array([[0],[1],[1]]), y)
array([[ -50. ],
[-148.12026406],
[-148.62125111]])
梯度下降法
def gradientDescent(X, theta, y, alpha, iters):
cost = np.zeros(iters + 1)
cost[0] = compterCost(X, theta, y)
print(f"loop {0}'s cost is {cost[0]}")
for i in range(iters):
theta = theta + getGradient(X, theta, y)*alpha
cost[i+1] = compterCost(X, theta, y)
print(f"loop {i+1}'s cost is {cost[i+1]}")
if(cost[i+1]==0):
break
plt.plot(range(iters+1), cost)
#print(cost)
return theta
初始换参数
theta_init = np.random.normal(0, 1, size = [m, 1])
调参
theta = theta_init
iters = 70
rate = 0.003
Theta = np.zeros([m,iters])
theta = gradientDescent(X, theta, y, rate, iters)
loop 0's cost is 146.8614585417101
loop 1's cost is 44.73090421627507
loop 2's cost is 48.70371494672389
loop 3's cost is 34.64400407600265
loop 4's cost is 40.484995297839994
loop 5's cost is 29.71544907927535
loop 6's cost is 38.247842867274045
loop 7's cost is 19.50704497597027
loop 8's cost is 16.876667028290644
loop 9's cost is 21.50354274044174
loop 10's cost is 40.567269089977934
loop 11's cost is 17.772994071927197
loop 12's cost is 15.939289855995966
loop 13's cost is 41.826814316008445
loop 14's cost is 19.032539297957697
loop 15's cost is 1.404799658922513
loop 16's cost is 4.591165427094092
loop 17's cost is 13.801246238442143
loop 18's cost is 37.97009525496931
loop 19's cost is 15.210772260291817
loop 20's cost is 0.1879831222878148
loop 21's cost is 0.04139715239780852
loop 22's cost is 0.0058499407125103
loop 23's cost is 0.0
边界
plt.scatter(X[:50, -2], X[:50, -1])
plt.scatter(X[50:, -2], X[50:, -1], c = "#ff4400")
#x = np.arange(5,23,0.5)
#plt.plot(x, getMy(x.reshape(-1,1)))
plt.axline([1, -(theta[1][0]*1+theta[0][0])/theta[2][0]], xy2 = [2, -(theta[1][0]*2+theta[0][0])/theta[2][0]])
#plt.plot(x, np.insert(x.reshape(-1,1), 0, 1, 1)@true_theta)
<matplotlib.lines._AxLine at 0x7fc34d497070>
标签:plt,50,感知机,算法,cost,np,theta,loop
From: https://www.cnblogs.com/RanX2018/p/16742914.html