《数据挖掘》第三课笔记

本文介绍了数据挖掘中的感知器算法,包括其代码实现,如生成随机数据、预测函数、更新规则以及激活函数。此外,还讨论了不同类型的梯度下降优化算法,如批量梯度下降、随机梯度下降和小批量梯度下降。文章通过实例展示了如何使用感知器进行训练并呈现结果。
摘要由CSDN通过智能技术生成


一、感知器算法

在这里插入图片描述在这里插入图片描述


二、代码实现

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、随机梯度下降法、小批量梯度下降

见往期修改篇:链接: 《数据挖掘》第二课笔记

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幻兒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值