Implemented in Python PRML Chapter 4 Classification by Perceptron Algorithm

Pattern recognition and machine learning, a reproduction of Figure 4.7 from Chapter 4, “Linear Discriminative Models”.

This figure shows how the classification by the perceptron algorithm converges, and the figure on the lower right shows that the convergence is complete. Actually, it is a story on the feature space $ (\ phi_1, \ phi_2) $, but for convenience of drawing, we decided to call it an identity function such that $ \ phi ({\ bf x}) = {\ bf x} $. I have.

Rough flow of implementation

① The identification boundary you want to find is (4.52)

  y({\bf x}) = f ({\bf x}^{\bf T} \phi({\bf x}))(4.52)

(2) Find the parameter [tex: w] that minimizes the error of (4.54) while updating (4.55) by the stochastic steepest descent method.

  E_p({\bf w}) = -\sum_{n=1}^N {\bf w}^{\bf T} \phi({\bf x}_n){\bf t}_n(4.54)
{\bf w}^{\rm \tau+1} = {\bf w}^{\rm \tau} - \mu \nabla  E_p({\bf{w}}) = {\bf w}^{\rm \tau}-  \mu \phi({\bf x}_n) {\bf t}_n (4.55)

code

import matplotlib.pyplot as plt
from pylab import *
import numpy as np
import random
%matplotlib inline

def func(train_data, k):
    def perceptron(train_data, k):
        x = 0
        w = array([1.0,1.0, 1.0])
        eta = 0.1
        converged = False
    
        while not converged:
            x += 1
            if x == k:
                converged = True
                
            for i in range(N):
                w_t_phi_x = np.dot(w, [1,train_data[i,0], train_data[i,1]])
                
                if w_t_phi_x > 0:
                    d = 1
                else:
                    d = 2
             
                if d == train_data[i,2]:
                    continue
                else:
                    if d == 1:
                        t = 1
                        w += -eta * array([1, train_data[i,0], train_data[i,1]]) * t
                    else:
                        t = -1
                        w += -eta * array([1, train_data[i,0], train_data[i,1]]) * t
        return w
    
    w = perceptron(train_data, k)

    #Plot
    plt.scatter(x_red,y_red,color='r',marker='x')
    plt.scatter(x_blue,y_blue,color='b',marker='x')
    x_line = linspace(-1.0, 1.0, 1000)
    y_line = -w[1]/w[2] * x_line - w[0]/w[2]
    plt.plot(x_line, y_line, 'k')
    xlim(-1.0, 1.0)
    ylim(-1.0, 1.0)
    title("Figure 7.2")
    show()

if __name__ == "__main__":
    #Generate sumple data
    #Mean
    mu_blue = [-0.5,0.3]
    mu_red = [0.5,-0.3]
    #Covariance
    cov = [[0.08,0.06], [0.06,0.08]]

    #Data Blue
    x_blue = [-0.85, -0.8, -0.5, -0.4, -0.1]
    y_blue = [0.6,0.95, 0.62, 0.7, -0.1]
    blue_label = np.ones(5)

    #Data Red
    x_red = [0.2, 0.4, 0.45, 0.6, 0.95]
    y_red = [0.7, -0.5, 0.4, -0.9, 0.97]
    red_label = np.ones(5)*2

    Data_blue = vstack((x_blue, y_blue, blue_label)).T
    Data_red = vstack((x_red, y_red, red_label)).T
    Data_marge = vstack((Data_blue,Data_red))
    
    N = len(Data_marge)
    for k in (N-9, N-6,N-3, N):
        func(Data_marge, k)

result

Screen Shot 2015-09-27 at 00.59.17.png

Screen Shot 2015-09-27 at 00.59.34.png

Screen Shot 2015-09-27 at 00.59.53.png

Screen Shot 2015-09-27 at 01.00.02.png

Recommended Posts

Implemented in Python PRML Chapter 4 Classification by Perceptron Algorithm
Implemented in Python PRML Chapter 7 Nonlinear SVM
Implemented in Python PRML Chapter 5 Neural Networks
Implemented in Python PRML Chapter 1 Bayesian Inference
Implemented in Python PRML Chapter 3 Bayesian Linear Regression
Implemented in Python PRML Chapter 1 Polynomial Curve Fitting
Implemented Perceptron learning rules in Python
PRML Chapter 8 Product Sum Algorithm Python Implementation
Alignment algorithm by insertion method in Python
Implement PRML algorithm in Python (almost Numpy only)
Implemented SimRank in Python
Genetic algorithm in python
Algorithm in Python (Bellman-Ford)
Implemented Shiritori in Python
Algorithm in Python (Dijkstra's algorithm)
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
Algorithm in Python (primality test)
Reproduce Euclidean algorithm in Python
Algorithm in Python (binary search)
Sudoku solver implemented in Python 3
Implement Dijkstra's Algorithm in python
6 Ball puzzle implemented in python
Sort by date in python
Python Machine Learning Programming Chapter 2 Classification Problems-Machine Learning Algorithm Training Summary
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)
I implemented Donald Knuth's unbiased sequential calculation algorithm in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)
Algorithm in Python (breadth-first search, bfs)
Implemented image segmentation in python (Union-Find)
Sorting algorithm and implementation in Python
100 Language Processing Knock Chapter 1 in Python
PRML Chapter 5 Neural Network Python Implementation
Write A * (A-star) algorithm in Python
Develop an investment algorithm in Python 2
Widrow-Hoff learning rules implemented in Python
Algorithm in Python (depth-first search, dfs)
Implemented label propagation method in Python
PRML Chapter 3 Evidence Approximation Python Implementation
Implementing a simple algorithm in Python 2
Algorithm (segment tree) in Python (practice)
Run a simple algorithm in Python
100 Language Processing Knock Chapter 1 by Python
Implemented in 1 minute! LINE Notify in Python
Python vs Ruby "Deep Learning from scratch" Chapter 2 Logic circuit by Perceptron
PRML Chapter 4 Bayesian Logistic Regression Python Implementation
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
CIFAR-10 classification implemented in virtually 60 lines in PyTorch
PRML Chapter 5 Mixed Density Network Python Implementation
PRML Chapter 9 Mixed Gaussian Distribution Python Implementation
Ant book in python: Sec.2-5 Dijkstra's algorithm
Algorithm in Python (ABC 146 C Binary Search
PRML Chapter 14 Conditional Mixed Model Python Implementation
PRML Chapter 10 Variational Gaussian Distribution Python Implementation
PRML Chapter 6 Gaussian Process Regression Python Implementation
PRML Chapter 2 Student's t Distribution Python Implementation
PRML Chapter 1 Bayesian Curve Fitting Python Implementation
Read the file line by line in Python
Automate jobs by manipulating files in Python
Read the file line by line in Python
I implemented Cousera's logistic regression in Python
Write a simple greedy algorithm in Python