Skip to content

Instantly share code, notes, and snippets.

@enakai00
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enakai00/e45a5b13daf95b1adf73 to your computer and use it in GitHub Desktop.
Save enakai00/e45a5b13daf95b1adf73 to your computer and use it in GitHub Desktop.
Perceptron emulation
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas import Series, DataFrame
import pandas.io.data as web
from numpy.random import randint, randn, rand, multivariate_normal
def run_perceptron(axes):
n1 = randint(40,80)
n2 = randint(40,80)
mu1 = [rand()*10-5,rand()*10-5]
mu2 = [rand()*10-5,rand()*10-5]
cov1 = np.array([[rand()*3+1,0],[0,rand()*3+1]])
theta = rand() * np.pi
rot = np.array([[np.cos(theta),-np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
cov1 = np.dot(rot.T,np.dot(cov1,rot))
cov2 = np.array([[rand()*3+1,0],[0,rand()*3+1]])
theta = rand() * np.pi
rot = np.array([[np.cos(theta),-np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
cov2 = np.dot(rot.T,np.dot(cov2,rot))
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))
axes.clear()
ymin, ymax = df.y.min(), df.y.max()
xmin, xmax = df.x.min(), df.x.max()
axes.set_ylim([ymin-1, ymax+1])
axes.set_xlim([xmin-1, xmax+1])
axes.scatter(df1.x, df1.y, marker='o')
axes.scatter(df2.x, df2.y, marker='x')
best = {'err_rate': 100}
for c in range(5):
dx, dy, d0 = rand()*2-1, rand()*2-1, rand()*2-1
for i in range(40):
for index, point in df.iterrows():
x, y, type = point.x, point.y, point.type
if type * (x * dx + y * dy + d0) >= 0:
dx -= type * x
dy -= type * y
d0 -= type * 1
err = 0
for index, point in df.iterrows():
x, y, type = point.x, point.y, point.type
if type * (x * dx + y * dy + d0) >= 0:
err += 1
err_rate = err * 100 / len(df)
if err_rate < best['err_rate']:
best['err_rate'] = err_rate
best['dx'] = dx
best['dy'] = dy
best['d0'] = d0
if err_rate == 0:
break
dx, dy, d0 = best['dx'], best['dy'], best['d0']
linex = np.arange(xmin-5, xmax+5)
liney = - linex * dx / dy - d0 / dy
label = "ERR: %.2f%%" % best['err_rate']
axes.plot(linex,liney,label=label)
axes.legend(loc='best')
if __name__ == '__main__':
fig, axes = plt.subplots(2,2)
positions = [[0,0],[0,1],[1,0],[1,1]]
for pos in positions:
run_perceptron(axes[pos[0],pos[1]])
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment