Created
January 12, 2015 00:49
-
-
Save enakai00/88d0a745baf254bceb16 to your computer and use it in GitHub Desktop.
Perceptron Simulation Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
from pandas import Series, DataFrame | |
from numpy.random import randint, randn, rand, multivariate_normal | |
def run_perceptron(ax): | |
n1 = 60 | |
n2 = 40 | |
mu1 = [50,80] | |
mu2 = [80,60] | |
cov1 = np.array([[40,0],[0,20]]) | |
cov2 = np.array([[20,0],[0,40]]) | |
df1 = DataFrame(multivariate_normal(mu1,cov1,n1),columns=['x','y']) | |
df1['type']=1 | |
df2 = DataFrame(multivariate_normal(mu2,cov2,n2),columns=['x','y']) | |
df2['type']=-1 | |
df = pd.concat([df1,df2],ignore_index=True) | |
df = df.reindex(np.random.permutation(df.index)) | |
ax.clear() | |
ax.set_xlim(0,100) | |
ax.set_ylim(0,100) | |
ax.scatter(df1.x, df1.y, marker='o') | |
ax.scatter(df2.x, df2.y, marker='x') | |
bias = ((df.x + df.y) / 2).mean() | |
dx, dy, d0 = rand()*10-20, rand()*10-20, rand()*10-20 | |
for i in range(100): | |
err = 0 | |
for index, point in df.iterrows(): | |
x, y, type = point.x, point.y, point.type | |
if type * (x * dx + y * dy + bias * d0) < 0: | |
dx += type * x | |
dy += type * y | |
d0 += type * bias | |
err += 1 | |
if err == 0: break | |
print 'iter = %d, dx = %f, dy = %f, d0 = %f' % (i, dx, dy, d0) | |
linex = np.arange(0,101) | |
liney = - linex * dx / dy - (d0 * bias) / dy | |
ax.plot(linex, liney) | |
if __name__ == '__main__': | |
fig = plt.figure() | |
ax = fig.add_subplot(111) | |
run_perceptron(ax) | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment