数据挖掘——感知器算法处理墨渍问题

本文介绍了一种使用感知器算法解决平面墨渍分类问题的方法。通过从Sklearn数据库获取墨渍数据,利用坐标作为特征,颜色作为标签,训练感知器模型进行分类。文章详细展示了生成墨渍数据、训练模型、测试模型并评估准确率的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、问题描述

        平面上有两摊墨渍,它们的颜色分别是黄色和蓝色,墨渍分类问题就是是根据点的坐标,判断其染上的颜色。从Sklearn的数据库中获取墨渍数据,每条数据是平面上的一个点,特征组(即特征向量)为该点的坐标,标签为该点的颜色,0表示黄色,1表示蓝色。

二、解题步骤

1. 首先生成墨迹

2. 生成训练模型,训练数据

(1)训练模型

(2)调用训练模型,训练数据

(3)训练数据

3. 生成测试数据、测试模型

4. 得出结论

三、完整代码

机器学习GitHub:https://github.com/wanglei18/machine_learning

Perceptron.py
import numpy as np
class Perceptron:
    def fit(self, X, y): #训练
        m, n = X.shape                    # X  m*n矩阵
        w = np.zeros((n, 1))              # w  n*1列向量
        b = 0
        done = False
        while not done:                  #done==True 结束
            done = True
            for i in range(m):
                x = X[i].reshape(1, -1)        # x  1*n行向量
                if y[i] * (x.dot(w) + b) <= 0: #样本(x[i],y[i])位于直线错误一侧
                    w = w + y[i] * x.T         # 更新 w  
                    b = b + y[i]               # 更新 b
                    done = False
        self.w = w    #得到模型的参数 w
        self.b = b    #模型参数 b
    def predict(self, X):     #预测x的标签
        return np.sign(X.dot(self.w) + self.b)  #预测标签 +1或-1
Ink.py
#墨渍数据从Sklearn获取
from sklearn.datasets.samples_generator import make_blobs
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from perceptron import Perceptron #感知器类

#生成墨迹
plt.figure(1)
X, y = make_blobs(n_samples= 100, centers=2, n_features=2, cluster_std=0.6, random_state=0)
plt.plot(X[:, 0][y == 1], X [:, 1][y == 1], "bs", ms=3)
plt.plot(X[:, 0][y == 0], X [:, 1][y == 0], "yo", ms=3)
plt.show()

y = 2 * y - 1 #标签  -1或+1
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=5) #训练集、测试集划分

model = Perceptron()                #声明Perceptron实例
model.fit(X_train, y_train)         #训练
w = model.w                         #model 的参数 w
b = model.b                         #model 的参数 b
print('w=', w)
print('b=', b)

#生成训练数据
x0 = np.linspace(-1, 5, 200)         #在[-1,5]均匀取200个点
line = -w[0]/w[1] * x0 - b/w[1]     #分离线 已知w,b,x0,求x1,  w0x0+w1x1+b=0 ,  x1即line

#draw 对训练数据集进行分类,两类分离线
plt.figure(2)
plt.plot(X_train[:, 0][y_train == 1], X_train[:, 1][y_train == 1] + 0.1, "bs", ms=3)
plt.plot(X_train[:, 0][y_train == -1], X_train[:, 1][y_train == -1] - 0.1, "yo", ms=3)
plt.plot(x0, line)
plt.show()

#draw 对测试数据集进行分类,两类分离线
plt.figure(3)
plt.plot(X_test[:, 0][y_test == 1], X_test[:, 1][y_test == 1], "bs", ms=3)    #正例蓝色点
plt.plot(X_test[:, 0][y_test == -1], X_test[:, 1][y_test == -1], "yo", ms=3)  #负例黄色点
plt.plot(x0, line)
plt.show()

y_pred = model.predict(X_test)              #测试
accuracy = accuracy_score(y_test, y_pred)   #测试结果的评价 sklearn.metrics.accuracy_score
print("accuracy= {}".format(accuracy))      #输出正确率

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

李逍遥~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值