import math
import numpy as np
import random
import pylab as pl
def shuju1(num):
m1 = np.array([-5, 0])
s1 = np.array([[1, 0], [0, 1]])
m2 = np.array([0, 5])
s2 = np.array([[1, 0], [0, 1]])
X1 = np.empty((num, 2))
X2 = np.empty((num, 2))
y1 = np.ones((1, num))
y2 = -np.ones((1, num))
for i in range(num):
X1[i] = np.random.multivariate_normal(m1, s1)
X2[i] = np.random.multivariate_normal(m2, s2)
X = np.append(X1, X2, axis=0)
y0 = np.append(y1, y2)
x = np.zeros([int(num * 0.8 * 2), 2])
y = np.zeros(int(num * 0.8 * 2))
x_test = np.zeros([num * 2 - int(num * 0.8 * 2), 2])
y_test = np.zeros(num * 2 - int(num * 0.8 * 2))
st = np.random.get_state()
np.random.shuffle(X)
np.random.set_state(st)
np.random.shuffle(y0)
for i in range(num * 2):
if i < num * 0.8 * 2:
x[i] = X[i]
y[i] = y0[i]
else:
x_test[i - int(num * 0.8 * 2)] = X[i]
y_test[i - int(num * 0.8 * 2)] = y0[i]
return x, y, x_test, y_test
def shuju2(num):
m1 = np.array([1, 0])
s1 = np.array([[1, 0], [0, 1]])
m2 = np.array([0, 1])
s2 = np.array([[1, 0], [0, 1]])
X1 = np.empty((num, 2))
X2 = np.empty((num, 2))
y1 = np.ones((1, num))
y2 = -np.ones((1, num))
for i in range(num):
X1[i] = np.random.multivariate_normal(m1, s1)
X2[i] = np.random.multivariate_normal(m2, s2)
X = np.append(X1, X2, axis=0)
y0 = np.append(y1, y2)
x = np.zeros([int(num * 0.8 * 2), 2])
y = np.zeros(int(num * 0.8 * 2))
x_test = np.zeros([num * 2 - int(num * 0.8 * 2), 2])
y_test = np.zeros(num * 2 - int(num * 0.8 * 2))
st = np.random.get_state()
np.random.shuffle(X)
np.random.set_state(st)
np.random.shuffle(y0)
for i in range(num * 2):
if i < num * 0.8 * 2:
x[i] = X[i]
y[i] = y0[i]
else:
x_test[i - int(num * 0.8 * 2)] = X[i]
y_test[i - int(num * 0.8 * 2)] = y0[i]
return x, y, x_test, y_test
Draw.py
import matplotlib.pyplot as plt
import numpy as np
import pylab as pl
def huatu1(x, y, w):
xc1 = []
yc1 = []
xc2 = []
yc2 = []
x_num = x.shape[0]
for i in range(x_num):
if (y[i] == 1):
xc1.append(x[i, 0])
yc1.append(x[i, 1])
else:
xc2.append(x[i, 0])
yc2.append(x[i, 1])
plt.figure()
plt.scatter(xc1, yc1, s=40, c='red', marker='o')
plt.scatter(xc2, yc2, s=40, c='blue')
plt.xlabel('x1')
plt.ylabel('x2')
# 绘制分类线
x = np.arange(-10.0, 5.0, 0.1)
y = (-w[0] - w[1] * x) / w[2]
plt.plot(x, y)
plt.savefig('xunlian.png', dpi=75)
plt.show()
def huatu2(x, y, w):
xc1 = []
yc1 = []
xc2 = []
yc2 = []
x_num = x.shape[0]
for i in range(x_num):
if (y[i] == 1):
xc1.append(x[i, 0])
yc1.append(x[i, 1])
else:
xc2.append(x[i, 0])
yc2.append(x[i, 1])
plt.figure()
plt.scatter(xc1, yc1, s=40, c='red', marker='x')
plt.scatter(xc2, yc2, s=40, c='blue', marker='x')
plt.xlabel('x1')
plt.ylabel('x2')
# 绘制分类线
x = np.arange(-10.0, 5.0, 0.1)
y = (-w[0] - w[1] * x) / w[2]
plt.plot(x, y)
plt.savefig('ceshi.png', dpi=75)
plt.show()
guangyini.py
import numpy as np
import time
import Data
import Draw
def guangyini(x,y):
x_num = x.shape[0]
x_d = x.shape[1]
w = np.zeros(x_d + 1)
a = np.ones([x_num, 1])
x = np.concatenate((a, x), 1)
x_ = np.linalg.pinv(x)
w = np.matmul(x_, y)
return w
x,y,x_test,y_test=Data.shuju2(200)
w1=guangyini(x,y)
Draw.huatu1(x,y,w1)
Draw.huatu2(x_test,y_test,w1)
suijitidu.py
import numpy as np
import time
import Data
import Draw
def gardient_descent(x,y,t,u):
x_num = x.shape[0]
x_d = x.shape[1]
w = np.zeros(x_d + 1)
a = np.ones([x_num, 1])
x = np.concatenate((a, x), 1)
for i in range(t):
L_G = np.zeros(x_d + 1)
for j in range(x_num):
L_G = L_G + (np.dot(w, x[j]) - y[j]) * x[j]
w = w - u * L_G
if (abs(L_G.any()) <= 1e-6):
break
return w
def test(xt, yt, w):
xt_num = xt.shape[0]
xt_d = xt.shape[1]
a = np.ones([xt_num, 1])
xt = np.concatenate((a, xt), 1)
right = 0
for i in range(xt_num):
y_hat = np.sign(w @ xt[i])
if (y_hat == yt[i]):
right = right + 1
accuracy = right / xt_num
for i in range(xt_num):
Lout = (np.dot(w, xt[i]) - yt[i])**2
return Lout
x,y,xt,yt=Data.shuju2(200)
Mepoch = 50
Lout = np.zeros(Mepoch)
for t in range(Mepoch):
w1 =gardient_descent(x, y, t, 0.001)
Lout[t] = test(xt, yt, w1)
Draw.huatu1(x,y,w1)
Draw.huatu2(xt,yt,w1)