华科(狗都不读)自动化模式识别作业
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()
Data.py
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
if(__name__ == "__main__"):
x,y,x_test,y_test = shuju1(10)
print(x)
print(y)
Fisher线性判别
import numpy as np
import Draw
import Data
def cheshi(w1,w2,W_,S):
sum = 0
for x1 in w1:
if W_[0]*x1[0]+W_[1]*x1[1] < S:
sum+=1
for x2 in w2:
if W_[0]*x2[0]+W_[1]*x2[1] > S:
sum+=1
return sum
def Fisher(w1,w2):
E1 = np.empty((2,2))
E2 = np.empty((2,2))
e11,e12,e21,e22 = 0,0,0,0
sum1 = 0
sum2 = 0
for w in w1:
sum1 += w[0]
sum2 += w[1]
u1 = np.array([sum1/(len(w1)),sum2/(len(w1))])
sum11 = 0
sum22 = 0
for w in w2:
sum11 += w[0]
sum22 += w[1]
u2 = np.array([sum11 / (len(w2)), sum22 / (len(w2))])
for w in w1:
n = w - u1
e11 += (n[0])**2
e12 += n[0]*n[1]
e22 += (n[1])**2
E1[0][0],E1[0][1],E1[1][0],E1[1][1] = e11,e12,e12,e22
e11,e12,e21,e22 = 0,0,0,0
for w in w2:
n = w-u2
e11 += (n[0]) ** 2
e12 += n[0] * n[1]
e22 += (n[1]) ** 2
E2[0][0],E2[0][1],E2[1][0],E2[1][1] = e11,e12,e12,e22
E1 = E1+E2
E_ = np.linalg.inv(E1)
W_ = np.dot(E_,(u1-u2))#W*
S = (np.dot(W_,(u1+u2))/2)#判别门限
return W_,S
if __name__ =="__main__":
#数据处理:(均值向量分别为[0,5][-5,0])
x, y, x_test, y_test = Data.shuju1(200)
#(均值向量分别为[0,1][1,0])
# x, y, x_test, y_test = Data.shuju2(200)
j,z=0,0
for i in range(len(x)):
if y[i]>0:
j+=1
else:
z+=1
w1 = np.empty((j,2))
w2 = np.empty((z,2))
j, z = 0, 0
for i in range(len(x)):
if y[i] > 0:
w1[j] = x[i]
j += 1
else:
w2[z] = x[i]
z += 1
j,z = 0,0
for i in range(len(x_test)):
if y_test[i]>0:
j+=1
else:
z+=1
w1_test = np.empty((j, 2))
w2_test = np.empty((z, 2))
j, z = 0, 0
for i in range(len(x_test)):
if y_test[i]>0:
w1_test[j] = x_test[i]
j+=1
else:
w2_test[z] = x_test[i]
z+=1
W_,S = Fisher(w1, w2)
print(f"W_ = {W_}")
print(f"S = {S}")
w = np.array([-S,W_[0],W_[1]])
Draw.huatu1(x, y, w)
print(f"训练集有{cheshi(w1, w2, W_, S)}个错误")
print(f"测试集有{cheshi(w1_test, w2_test, W_, S)}个错误")
PLA算法
import numpy as np
import time
import Data
import Draw
# 1、导入数据集
# 训练感知机模型
class Perceptron:
def __init__(self, x, y, a=1):
self.x = x
self.y = y
self.w = np.zeros((x.shape[1], 1)) # 初始化权重,w1,w2均为0
self.b = 0
self.a = 0.1 # 学习率
self.numsamples = self.x.shape[0]
self.numfeatures = self.x.shape[1]
def sign(self, w, b, x):
y = np.dot(x, w) + b
return int(y)
def update(self, label_i, data_i):
tmp = label_i * self.a * data_i
tmp = tmp.reshape(self.w.shape)
# 更新w和b
self.w = tmp + self.w
self.b = self.b + label_i * self.a
def train(self):
isFind = False
num = 0
while not isFind:
count = 0
for i in range(self.numsamples):
tmpY = self.sign(self.w, self.b, self.x[i, :])
if tmpY * self.y[i] <= 0: # 如果是一个误分类实例点
count += 1
num += 1
self.update(self.y[i], self.x[i, :])
if count == 0:
print( '最终训练得到的w和b为:', self.w, self.b)
isFind = True
return self.w, self.b
def sign1(w, b, x):
y = np.dot(x, w) + b
return int(y)
def classify1(w, b,x,y):
mistakes = 0
for i in range(x.shape[0]):
tmpY = sign1(w, b, x[i, :])
if tmpY * y[i] <= 0: # 如果是误分类
mistakes += 1
return mistakes
if __name__ == '__main__':
x, y, x_test, y_test = Data.shuju1(200)
myperceptron = Perceptron(x=x, y=y)
start = time.time()
w0, b = myperceptron.train()
w = np.array([w0[0][0],w0[1][0]])
costtime = time.time() - start
w1 = np.array([b,w0[0][0],w0[1][0]])
Draw.huatu1(x, y, w1)
Draw.huatu2(x_test, y_test, w1)
print(f"训练集有{classify1(w, b, x, y)}个错误")
print(f"测试集有{classify1(w, b, x_test, y_test)}个错误")
print("Time used:", costtime)
import time
import numpy as np
import random
import Data
import Draw
# 感知机模型
class Pocket:
def __init__(self, x, y):
self.x = x
self.y = y
self.w = np.zeros(x.shape[1]) # 初始化权重,w1,w2均为0
self.best_w = np.zeros(x.shape[1]) # 最好
self.b = 0
self.best_b = 0
def sign(self, w, b, x):
y = np.dot(x, w) + b
return int(y)
def classify(self, w, b):
mistakes = []
for i in range(self.x.shape[0]):
tmpY = self.sign(w, b, self.x[i, :])
if tmpY * self.y[i] <= 0: # 如果是误分类
mistakes.append(i)
return mistakes
def update(self, label_i, data_i):
tmp = label_i * data_i
tmpw = tmp + self.w
tmpb = self.b + label_i
if (len(self.classify(self.best_w, self.best_b)) >= (len(self.classify(tmpw, tmpb)))):
self.best_w = tmp + self.w
self.best_b = self.b + label_i
self.w = tmp + self.w
self.b = self.b + label_i
def train(self, max_iters):
iters = 0
isFind = False
while not isFind:
mistakes = self.classify(self.w, self.b)
if (len(mistakes) == 0):
return self.best_w, self.best_b
n = mistakes[random.randint(0, len(mistakes) - 1)]
self.update(self.y[n], self.x[n, :])
iters += 1
if iters == max_iters:
isFind = True
return self.best_w, self.best_b
def sign1(w, b, x):
y = np.dot(x, w) + b
return int(y)
def classify1(w, b,x,y):
mistakes = 0
for i in range(x.shape[0]):
tmpY = sign1(w, b, x[i, :])
if tmpY * y[i] <= 0: # 如果是误分类
mistakes += 1
return mistakes
if __name__ == '__main__':
# 编程题输出(均值向量分别为[0,5][-5,0])
x, y, x_test, y_test = Data.shuju1(200)
# 编程题输出(均值向量分别为[0,1][1,0])
# x, y, x_test, y_test = Data.shuju2(200)
myPocket_PLA = Pocket(x, y)
time1 = time.time()
w, b = myPocket_PLA.train(50)
time2 = time.time()
w1 = np.array([b, w[0], w[1]])
# 计算测试时间
time_ = time2 - time1
print(f"所花时间时间为{time_}s")
print(f"训练集有{classify1(w,b,x,y)}个错误")
print(f"测试集有{classify1(w,b,x_test,y_test)}个错误")
# 画出数据集与分类面
Draw.huatu1(x, y, w1)
Draw.huatu2(x_test, y_test, w1)
广义逆
import numpy as np
from 模式识别 import Draw
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
if(__name__ == "__main__"):
#作业计算题
# x,y =date()
# w = guangyini(x,y)
# Draw.huatu1(x, y, w)
# print(w)
# 编程题输出(均值向量分别为[0,5][-5,0])
x, y, x_test, y_test = Data.shuju1(200)
# 编程题输出(均值向量分别为[0,1][1,0])
# 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)
print(w1)
梯度下降
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 sign1(w1, x):
x = np.array([1,x[0],x[1]])
y = np.dot(x, w1)
return y
def classify1(w1,x,y):
mistakes = 0
for i in range(x.shape[0]):
tmpY = sign1(w1, x[i, :])
if tmpY * y[i] <= 0: # 如果是误分类
mistakes += 1
return mistakes
# 编程题输出(均值向量分别为[0,5][-5,0])
x, y, x_test, y_test = Data.shuju1(200)
# 编程题输出(均值向量分别为[0,1][1,0])
# x, y, x_test, y_test = Data.shuju2(200)
Mepoch = 50
Lout = np.zeros(Mepoch)
for t in range(Mepoch):
w1 = gardient_descent(x, y, t, 0.0001)
Draw.huatu1(x, y, w1)
Draw.huatu2(x_test, y_test, w1)
print(f"训练集有{classify1(w1,x,y)}个错误")
print(f"测试集有{classify1(w1,x_test,y_test)}个错误")
逻辑斯特
import math
import numpy as np
import time
import Data
import Draw
def luojisite(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 + (-y[j]*x[j])/(1+math.exp(-np.dot(w, x[j])*y[j]))
w = w - u * L_G / x_num
if (abs(L_G.any()) <= 1e-6):
break
return w
def sign1(w1, x):
x = np.array([1,x[0],x[1]])
y = np.dot(x, w1)
return y
def classify1(w1,x,y):
mistakes = 0
for i in range(x.shape[0]):
tmpY = sign1(w1, x[i, :])
if tmpY * y[i] <= 0: # 如果是误分类
mistakes += 1
return mistakes
# 编程题输出(均值向量分别为[0,5][-5,0])
x, y, x_test, y_test = Data.shuju1(200)
# 编程题输出(均值向量分别为[0,1][1,0])
# x, y, x_test, y_test = Data.shuju2(200)
Mepoch = 50
Lout = np.zeros(Mepoch)
for t in range(Mepoch):
w1 = luojisite(x, y, t, 0.0001)
Draw.huatu1(x, y, w1)
Draw.huatu2(x_test, y_test, w1)
print(f"训练集有{classify1(w1,x,y)}个错误")
print(f"测试集有{classify1(w1,x_test,y_test)}个错误")
部分来自网上各大大佬的代码,我只是做了整合,如有侵权请私聊,立刻删除