《数据挖掘》第三课笔记
一、感知器算法
二、代码实现
1.引入库
import numpy as np
import matplotlib.pyplot as plt
2.感知器_ init _
class Perceptron():
def __init__(self,n_samples,w,b):
self.alpha = 0.001
self.flag=0
self.sum_epochs = 0
self.w=w
self.b=b
self.n_samples=n_samples
3.生成两类随机数据
def get_data(self):
data1 = np.random.randn(50, 2)
label1 = np.zeros((50, 1))
data2 = np.random.randn(50, 2) + 4
label2 = np.ones((50, 1))
# 将两个数据合并
data = np.vstack([data1, data2])
label = np.vstack([label1, label2])
return data,label
4.预测函数
def predict(self,x):
predict=self.step_function(np.dot(self.w,x)+self.b)
return predict
5.update
def update(self,x_train,label):
update=self.alpha*(self.predict(x_train)-label)
self.w =self.w-update*x_train
self.b =self.b-update
6. 激活函数(阶跃函数)
def step_function(self, x):
return np.where(x > 0, 1, 0)
7 training
def fit(self, X, y,epochs):
self.sum_epochs += epochs
for epoch_1 in range(epochs):
accuracy=0
for i in range(self.n_samples):
if y[i]==self.predict(X[i]):
accuracy+=1
else:
self.update(X[i],y[i])
if accuracy == self.n_samples:
print("Training completed after {} epochs. Accuracy_num={}".format(self.sum_epochs-epoch_1-1,accuracy))
self.flag=1
self.show(X, y,epoch_1+1)
break
if epoch_1 == epochs-1:
print("{} epochs reached. Training stopped. Accuracy_num={}".format(self.sum_epochs,accuracy))
self.show(X, y,epochs)
print("Final weight:", self.w)
print("Final bias:", self.b)
return self.flag
8.show
# 图形显示结果
def show(self, X,y,n):
x1 = np.linspace(min(X[:, 0]), max(X[:, 0]), 100)
x2 = (- self.w[0] * x1-self.b ) / self.w[1]
plt.plot(x1, x2.reshape(-1))
# 画出数据集的散点图
plt.title("epoch:"+str(n))
colors = ['blue' if label == 1 else 'red' for label in y]
plt.scatter(X[:, 0], X[:, 1], c=colors)
plt.show()
9.code 整合
import numpy as np
import matplotlib.pyplot as plt
# 生成两类随机数据
def get_data():
data1 = np.random.randn(50, 2)
label1 = np.zeros((50, 1))
data2 = np.random.randn(50, 2) + 4
label2 = np.ones((50, 1))
# 将两个数据合并
data = np.vstack([data1, data2])
label = np.vstack([label1, label2])
return data, label
class Perceptron():
def __init__(self,n_samples,w,b):
self.alpha = 0.001
self.flag=0
self.sum_epochs = 0
self.w=w
self.b=b
self.n_samples=n_samples
#预测函数
def predict(self,x):
predict=self.step_function(np.dot(self.w,x)+self.b)
return predict
#update
def update(self,x_train,label):
update=self.alpha*(self.predict(x_train)-label)
self.w =self.w-update*x_train
self.b =self.b-update
#激活函数
def step_function(self, x):
return np.where(x > 0, 1, 0)
#training
def fit(self, X, y,epochs):
self.sum_epochs += epochs
for epoch_1 in range(epochs):
accuracy=0
for i in range(self.n_samples):
if y[i]==self.predict(X[i]):
accuracy+=1
else:
self.update(X[i],y[i])
if accuracy == self.n_samples:
print("Training completed after {} epochs. Accuracy_num={}".format(self.sum_epochs-epoch_1-1,accuracy))
self.flag=1
self.show(X, y,epoch_1+1)
break
if epoch_1 == epochs-1:
print("{} epochs reached. Training stopped. Accuracy_num={}".format(self.sum_epochs,accuracy))
self.show(X, y,epochs)
print("Final weight:", self.w)
print("Final bias:", self.b)
return self.flag
# 图形显示结果
def show(self, X,y,n):
x1 = np.linspace(min(X[:, 0]), max(X[:, 0]), 100)
x2 = (- self.w[0] * x1-self.b ) / self.w[1]
plt.plot(x1, x2.reshape(-1))
# 画出数据集的散点图
plt.title("epoch:"+str(n))
colors = ['blue' if label == 1 else 'red' for label in y]
plt.scatter(X[:, 0], X[:, 1], c=colors)
plt.show()
if __name__ == '__main__':
epochs=eval(input("请输入要迭代的次数:"))
data, label=get_data()
n_samples, n_features = data.shape # (100,2)
w = np.zeros(n_features)
b = np.zeros(1)
classify=Perceptron(n_samples,w,b)
flag=classify.fit(data,label,epochs)
while flag==0:
yorn=eval(input("是否继续训练:\n1、继续\n2、退出\n"))
if yorn==1:
epoch=eval(input("请输入要继续迭代的次数:"))
flag=classify.fit(data, label,epoch)
else:
break
10.结果展示
三、梯度下降优化算法、批量梯度下降SGD、随机梯度下降法、小批量梯度下降
见往期修改篇:链接: 《数据挖掘》第二课笔记