Adaline Classifier


In [1]:
%load_ext watermark
%watermark -a 'Sebastian Raschka' -d -v -p matplotlib


Sebastian Raschka 02/04/2015 

CPython 3.4.3
IPython 3.0.0

matplotlib 1.4.3

In [2]:
import sys
sys.path = ['/Users/sebastian/github/mlxtend'] + sys.path

import mlxtend
from mlxtend.data import iris_data
from mlxtend.evaluate import plot_decision_regions
from mlxtend.classifier import Adaline
mlxtend.__version__


Out[2]:
'0.2.3'

Loading Iris Data


In [3]:
import numpy as np

X, y = iris_data()
X = X[:, [0, 3]] # sepal length and petal width
X = X[0:100] # class 0 and class 1

y = np.where(y[0:100] == 0, -1, 1) # class -1 and class 1

# standardize
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()

Gradient Descent 2D


In [4]:
%matplotlib inline
import matplotlib.pyplot as plt


## Standardized

ada = Adaline(epochs=30, eta=0.01, learning='gd', random_seed=1)
ada.fit(X_std, y)
print(ada.w_)
plot_decision_regions(X_std, y, clf=ada)
plt.title('Adaline - Gradient Descent')
plt.show()

plt.plot(range(len(ada.cost_)), ada.cost_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()

## Non Standardized

ada = Adaline(epochs=500, eta=0.0001, learning='gd', random_seed=1)
ada.fit(X, y)
print(ada.w_)
plot_decision_regions(X, y, clf=ada)
plt.title('Adaline - Gradient Descent')
plt.show()

plt.plot(range(len(ada.cost_)), ada.cost_, )
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()


[ -5.20694599e-16  -7.86462587e-02   1.02193041e+00]
[-0.01896251 -0.18106272  1.32749215]

Stochastic Gradient Descent 2D


In [5]:
%matplotlib inline
import matplotlib.pyplot as plt


## Standardized

ada = Adaline(epochs=30, eta=0.01, learning='sgd', random_seed=1)
ada.fit(X_std, y)
print(ada.w_)
plot_decision_regions(X_std, y, clf=ada)
plt.title('Adaline - Stochastic Gradient Descent')
plt.show()

plt.plot(range(len(ada.cost_)), ada.cost_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()

## Non Standardized

ada = Adaline(epochs=500, eta=0.0001, learning='sgd', random_seed=1)
ada.fit(X, y)
print(ada.w_)
plot_decision_regions(X, y, clf=ada)
plt.title('Adaline - Stochastic Gradient Descent')
plt.show()

plt.plot(range(len(ada.cost_)), ada.cost_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()


[ 0.02703854 -0.08923447  1.01748196]
[-0.01990321 -0.17596167  1.32960221]

Convergence


In [6]:
X, y = iris_data()
X = X[:, [0, 1]] # sepal length and sepal width
X = X[50:150] # class 0 and class 1

y = np.where(y[50:150] == 2, -1, 1) # class -1 and class 1

# standardize
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()

In [11]:
%matplotlib inline
import matplotlib.pyplot as plt


## Standardized

ppn = Adaline(epochs=30, eta=0.01, learning='gd', random_seed=1)
ppn.fit(X_std, y)
print(ppn.w_)
plot_decision_regions(X_std, y, clf=ppn)
plt.title('Adaline - Standardized, y {-1, 1}')
plt.show()

plt.plot(range(len(ppn.cost_)), ppn.cost_,)
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()

## Non Standardized

ada = Adaline(epochs=500, eta=0.0001, learning='gd', random_seed=1)
ada.fit(X, y)
print(ada.w_)
plot_decision_regions(X, y, clf=ada)
plt.title('Adaline - Non-Standardized, y {-1, 1}')
plt.show()

plt.plot(range(len(ada.cost_)), ada.cost_,)
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()


[ -2.26485497e-16  -4.66896289e-01  -4.94869934e-02]
[ 0.5384049  -0.06046144 -0.07053866]

In [ ]: