感知机算法代码python
时间: 2023-10-04 09:09:54 浏览: 83
以下是感知机算法的Python代码示例:
```
import numpy as np
class Perceptron:
def __init__(self, num_inputs, learning_rate=0.1):
self.weights = np.zeros(num_inputs + 1)
self.lr = learning_rate
def predict(self, inputs):
summation = np.dot(inputs, self.weights[1:]) + self.weights[0]
return 1 if summation > 0 else 0
def train(self, training_inputs, labels, epochs):
for _ in range(epochs):
for inputs, label in zip(training_inputs, labels):
prediction = self.predict(inputs)
self.weights[1:] += self.lr * (label - prediction) * inputs
self.weights[0] += self.lr * (label - prediction)
```
希望这可以帮助你了解感知机算法的实现。
阅读全文