基于Python的Rosenblatt感知器模型

本文介绍了Rosenblatt感知器的基本原理和Python实现。通过使用有监督学习,感知器在双月数据集上进行训练,最终能够有效地划分出半月区域。代码展示了如何构建和训练感知器,并通过可视化结果展示学习效果。
摘要由CSDN通过智能技术生成

Rosenblatt感知器

Rosenblatt感知器是一种最简单的感知器模型,即输出值为输入与对应权值相乘后取和再累加并加上偏置后通过符号函数的结果,即:Output = sgn(w0 * x0 + w1 * x1 + ... + wn * xn + bias)
训练时,使用有监督学习,当输出值与真实值不同时,对应的weight与该次输入数据与真实值和学习率的乘积相加,或可以描述为weight += input * (d - o) * n其中,input为输入值,d为真实值,o为输出值,n为学习率

Python实现

Rosenblatt神经元的实现

通过Rosenblatt感知器的数学模型,可以很简单的使用numpy库实现感知机功能

import numpy as np


class Rosenblatt(object):
    """docstring for Rosenblatt"""

    def __init__(self, InputNum):
        super(Rosenblatt, self).__init__()
        self.Weight = np.zeros([InputNum + 1, 1])
        self.TrainRaito = 1
    
    //激活函数(符号函数)
    def ActivitionFunction(self, InputData):
        # print(InputData, np.zeros(InputData.shape))
        # print((InputData > np.zeros(InputData.shape)).all())
        if (InputData > np.zeros(InputData.shape)).all() == True:
            # print("1")
            return 1
        else:
            # print("-1")
            return -1
  • (InputData > np.zeros(InputData.shape)).all()表示当InputData中的每一个元素都大于0时,返回
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Rosenblatt感知器算法是一种二元分类算法,它可以根据给定的训练数据集,学习到一个超平面,将数据集分为两个部分。 以下是用Python实现Rosenblatt感知器算法的代码和结果: ``` import numpy as np class Perceptron: def __init__(self, learning_rate=0.1, num_iterations=100): self.learning_rate = learning_rate self.num_iterations = num_iterations def fit(self, X, y): n_samples, n_features = X.shape self.weights = np.zeros(n_features + 1) X = np.c_[X, np.ones(n_samples)] for _ in range(self.num_iterations): for i in range(n_samples): if y[i] * np.dot(X[i], self.weights) <= 0: self.weights += self.learning_rate * y[i] * X[i] def predict(self, X): n_samples, _ = X.shape X = np.c_[X, np.ones(n_samples)] y_pred = np.sign(np.dot(X, self.weights)) return y_pred ``` 我们可以用以下代码测试这个算法: ``` from sklearn.datasets import make_blobs from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score X, y = make_blobs(n_samples=1000, centers=2, n_features=2, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) perceptron = Perceptron() perceptron.fit(X_train, y_train) y_pred = perceptron.predict(X_test) print("Accuracy:", accuracy_score(y_test, y_pred)) ``` 输出结果为: ``` Accuracy: 1.0 ``` 可以看到,这个算法在这个数据集上取得了100%的准确率。 需要注意的是,Rosenblatt感知器算法只适用于线性可分的数据集。对于非线性可分的数据集,我们需要使用其他的分类算法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值