[PYTHON] Machine Learning_Nonlinearize Simple Perceptron

Consider binarizing the input vector $ \ textbf {x} = (x_1, x_2, \ cdots, x_N) $ using the function $ f (\ textbf {x}) $ defined by the following equation. ..


f(\textbf{x}) = \phi(\sum_{p=1}^{P} \sum_{n=1}^{N} w_{n,p} \, {x_n} ^p + w_{0})\\

\\

 \phi(y) = 

  \left\{
    \begin{array}{l}
      1 \quad (y \geq 0) \\
      -1 \quad (otherwise)
    \end{array}
  \right.

Where $ w_ {n, p} $ is the weight parameter, $ N $ is the dimension of the input vector $ \ textbf {x} $, and $ P $ is the degree of the polynomial. The weight parameter $ w_ {n, p} $ is Mean Squared Error


l(\textbf{W}) = \sum_{i=1}^{I} (y_i - f(\textbf{x}_i))^2

[Stochastic Gradient Descent](https://ja.wikipedia.org/wiki/%E7%A2%BA%E7%8E%87%E7%9A%84%E5%8B%BE%E9%85% It is obtained by minimizing with 8D% E9% 99% 8D% E4% B8% 8B% E6% B3% 95). An example of the weight parameter $ w_ {n, p} $ update formula at the time of learning is shown below.


 w_{0} \leftarrow w_{0} + \eta \sum_{i=1}^{I} (y_i - f(\textbf{x}_i)) \\
 w_{n,p} \leftarrow w_{n,p} + \eta \sum_{i=1}^{I} ((y_i - f(\textbf{x}_i)) ( \sum_{n=1}^{N} {x_{n,i}} \, ^{p} ))

$ \ Eta $ is the learning rate, $ (\ textbf {x} _i, y_i) $ is the $ i $ th teacher data, and $ I $ is the batch size.

When $ P = 1 $, the function $ f (\ textbf {x}) $ matches the well-known simple perceptron and the function $ What is entered in \ phi $ is a linear function of $ \ textbf {x} $. On the other hand, when $ P> 1 $, the polynomial of $ \ textbf {x} $ is input to the function $ \ phi $, not a linear function. By increasing the order $ P $, the non-linearity of the function $ f (\ textbf {x}) $ can be strengthened. The function $ f (\ textbf {x}) $ is considered to be a non-linear version of Simple Perceptron.

In order to see the effect of order $ P $, we trained using the Iris dataset and visualized the classification results. The experimental results are shown below.

The linear is the result when the order $ P = 1 $ (simple perceptron), and the poly2 and poly3 are the results when the degree $ P = 2 $ and $ P = 3 $. It can be seen that by increasing the degree $ P $, it becomes possible to handle complicated classifications.

Below is the source code used in the experiment.

MyClass.py


import numpy as np
from numpy.random import *


class MyClass(object):

    def __init__(self, eta=0.01, n_iter=10, shuffle=True, random_state=None, model='linear'):
        self.eta = eta
        self.n_iter = n_iter
        self.w_initialized = False
        self.shuffle = shuffle
        self.model=model
        if random_state:
            seed(random_state)
        
    def fit(self, X, y):
        self._initialize_weights(X.shape[1])
        self.cost_ = []
        for i in range(self.n_iter):
            if self.shuffle:
                X, y = self._shuffle(X, y)
            cost = []
            for xi, target in zip(X, y):
                cost.append(self._update_weights(xi, target))
            avg_cost = sum(cost) / len(y)
            self.cost_.append(avg_cost)
        return self

    def _shuffle(self, X, y):
        r = np.random.permutation(len(y))
        return X[r], y[r]
    
    def _initialize_weights(self, m):
        self.w1 = randn(m)
        self.w2 = randn(m)
        self.w3 = randn(m)
        self.b = randn(1)
        self.w_initialized = True
        
    def _update_weights(self, xi, target):
        output = self.activation(xi)
        error = (target - output)
        if self.model == 'linear':
            self.w1 += self.eta * xi * error
        elif self.model == 'poly2':
            self.w1 += self.eta * xi * error
            self.w2 += self.eta * (xi**2) * error
        elif self.model == 'poly3':
            self.w1 += self.eta * xi * error
            self.w2 += self.eta * (xi**2) * error
            self.w3 += self.eta * (xi**3) * error
        self.b += self.eta * error
        cost = 0.5 * error**2
        return cost

    def activation(self, X):
        if self.model == 'linear':
            return np.dot(X, self.w1) + self.b
        elif self.model == 'poly2':
            return np.dot(X, self.w1) + np.dot((X**2), self.w2) + self.b
        elif self.model == 'poly3':
            return np.dot(X, self.w1) + np.dot((X**2), self.w2) + np.dot((X**3), self.w3)  + self.b

    def predict(self, X):
        return np.where(self.activation(X) >= 0.0, 1, -1)

plot_decision_regions.py


import numpy as np
import matplotlib.pyplot as plt


def plot_decision_regions(X, y, classifier, resolution=0.02):

    '''
    https://github.com/rasbt/python-machine-learning-book/tree/master/code/ch02
    '''
    
    # setup marker generator and color map
    markers = ('o', 's', 'x', '^', 'v')
    colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
    cmap = ListedColormap(colors[:len(np.unique(y))])

    # plot the decision surface
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                           np.arange(x2_min, x2_max, resolution))
    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    Z = Z.reshape(xx1.shape)
    plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim(xx2.min(), xx2.max())

    # plot class samples
    for idx, cl in enumerate(np.unique(y)):
        plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
                    alpha=0.9, c=cmap(idx), s=100,
                    edgecolor='black',
                    marker=markers[idx], 
                    label=cl)

main.py


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from numpy.random import *
from MyClass import MyClass
from plot_decision_regions import plot_decision_regions


# load dataset
df = pd.read_csv('https://archive.ics.uci.edu/ml/'
                 'machine-learning-databases/iris/iris.data', header=None)
df.tail()

# select setosa and versicolor
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)

# extract x and y
X = df.iloc[0:100, [0,1]].values

# normarize X
X_std = np.copy(X)
X_std[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()
X_std[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()

model_names = ['linear', 'poly2', 'poly3']
for model in model_names:
    
    # import model
    ada = MyClass(n_iter=50, eta=0.01, random_state=1, model=model)
    
    # fitting
    ada.fit(X_std, y)

    # predict & plot
    plot_decision_regions(X_std, y, classifier=ada)
    plt.title(model)
    plt.xlabel('x [normarized]')
    plt.ylabel('y [normarized]')
    plt.legend(loc='upper left')
    plt.tight_layout()
    plt.savefig(model + '.png', dpi=300)
    plt.show()

    del ada
    

reference:

https://github.com/rasbt/python-machine-learning-book

Do ya!

Recommended Posts

Machine Learning_Nonlinearize Simple Perceptron
Machine learning algorithm (simple perceptron)
An introduction to machine learning from a simple perceptron
Machine learning algorithm (simple regression analysis)
Explanation and implementation of simple perceptron
perceptron
Machine learning with python (2) Simple regression analysis
Linear Discriminator-Fisher's Linear Discriminant Function, Simple Perceptron, IRLS
[Super Introduction] Machine learning using Python-From environment construction to implementation of simple perceptron-