python Python v.1中的Perceptron

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python v.1中的Perceptron相关的知识,希望对你有一定的参考价值。

import numpy as np

class Perceptron(object):

    def __init__(self, no_of_inputs, threshold=100, learning_rate=0.01):
        self.threshold = threshold
        self.learning_rate = learning_rate
        self.weights = np.zeros(no_of_inputs + 1)
           
    def predict(self, inputs):
        summation = np.dot(inputs, self.weights[1:]) + self.weights[0]
        if summation > 0:
          activiation = 1
        else:
          activation = 0            
        return activation

    def train(self, training_inputs, labels):
        for _ in range(self.threshold):
            for inputs, label in zip(training_inputs, labels):
                prediction = self.predict(inputs)
                self.weights[1:] += self.learning_rate * (label - prediction) * inputs
                self.weights[0] += self.learning_rate * (label - prediction)

以上是关于python Python v.1中的Perceptron的主要内容,如果未能解决你的问题,请参考以下文章

Python里的sort语句

python 测试Python Perceptron v.1

python 在Python中使用Perceptron v.1

python第十天(函数二)

Python学习

Python 到 C++ 的字典列表