"Python Machine Learning": Implementing Adaptive Linear Neuron Algorithms

This article uses python to implement the adaptive linear neuron (Adaline) algorithm, named myAdaline.py, which will call the module of the previous perceptron algorithm.

import numpy as np
import matplotlib.pyplot as plt
import myPerceptron as mP # Import the specified function module from the custom myPerceptron file


class AdalineGD(object):
    def __init__(self, eta=0.01, n_iter=50):
        self.eta = eta
        self.n_iter = n_iter

    def fit(self, X, y):
        self.w_ = np.zeros(1 + X.shape[1])
        self.cost_ = []

        for i in range(self.n_iter):
            output = self.net_input(X)
            errors = (y - output) # This is an array of consecutive values
            self.w_[1:] += self.eta * XTdot(errors) # Calculate the weight value of w_[1:]
            self.w_[0] += self.eta * errors.sum() # Calculate the weight value of w_[0]
            cost = (errors ** 2).sum() / 2.0 # errors is an array, each value in the array is squared, summed, and divided by 2
            self.cost_.append(cost) # Determine whether the optimal solution is obtained according to the value of the cost function
        return self

    # This should be different from the net_input of the perceptron. The perceptron inputs xi and returns a value, and here it returns an array
    def net_input(self, X):
        return np.dot(X, self.w_[1:]) + self.w_[0]

    # This function is activated
    def activation(self, X):
        return self.net_input(X)

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


X, y = mP.getXy()
# nrows=1, ncols=2 in subplots means that there are 1*2 area blocks, figsize=(8,4) means to create a canvas of 800*400 pixels
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))
ada1 = AdalineGD (n_iter = 10, eta = 0.01) .fit (X, y)
ax[0].plot(range(1, len(ada1.cost_) + 1), np.log10(ada1.cost_), marker='o') # np.log10 means taking the logarithm in base 10
ax[0].set_xlabel('Epochs')
ax[0].set_ylabel('log(sum-squared-error)')
ax[0].set_title('Adaline-learning rate 0.01')
ada2 = AdalineGD (n_iter = 10, eta = 0.0001) .fit (X, y)
ax[1].plot(range(1, len(ada2.cost_) + 1), ada2.cost_, marker='x') # logarithm is not taken here
ax[1].set_xlabel('Epochs')
ax[1].set_ylabel('sum-squared-error')
ax[1].set_title('Adaline-learning rate 0.0001')
plt.show()

# Standardize the value of the jth feature to make the corresponding data have the characteristics of standard normal distribution
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()

# Train Adaline again
there = AdalineGD(n_iter=15, eta=0.01)
ada.fit(X_std, y)
mP.plot_decision_regions(X_std, y, ada)
plt.title('Adaline - gradient descent')
plt.xlabel('sepal length[standardized]')
plt.ylabel('petal length[standardized]')
plt.legend(loc='upper left')
plt.show()

plt.plot (range (1, len (ada.cost_) + 1), ada.cost_, marker = 'o')
plt.xlabel('Epochs')
plt.ylabel('sum-squared-error')
plt.show()


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325589763&siteId=291194637