Python アルゴリズム設計 - パーセプトロン

1.パーセプトロン

パーセプトロンは、分類のためのエレガントな統計学習アルゴリズムです。パーセプトロンは実装が非常に簡単で、オンライン アルゴリズムです。最も重要なのは、線形分離可能な集合に適用すると、数学関数、学習アルゴリズム、およびアルゴリズムの正しさの証明の組み合わせとなることです。
ここに画像の説明を挿入

2、Pythonアルゴリズムの実装


import numpy as np

X = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 1], [-1, 1, 1], [1, -1, 1]])
Y = np.array([1, 1, 1, 0, 0])
W = np.zeros(3)

def perceptron(x, w):
    return (x @ w >= 0).astype(int)
def train(x, y, w):
    for i in range(len(x)):
        # 估算感知机
        h = perceptron(x[i, :], w)

        #误分类
        if h != y[i]:
            if y[i] == 1: 
                w += x[i, :]
            else:         
                w -= x[i, :]
    
    return perceptron(x, w)

for i in range(5):
    h = train(X, Y, W)
    print('w=', W, 'acc=', np.mean(h == Y))


出力結果:
ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/qq_44000141/article/details/130413911