Skip to content

Instantly share code, notes, and snippets.

@jose-roberto-abreu
Created July 31, 2015 20:01
Show Gist options
  • Save jose-roberto-abreu/7af98275f5e3877d3a2b to your computer and use it in GitHub Desktop.
Save jose-roberto-abreu/7af98275f5e3877d3a2b to your computer and use it in GitHub Desktop.
Percetron Simple Model
{"cells": [{"cell_type": "code", "execution_count": null, "source": "import numpy as np\nimport matplotlib.pyplot as plt\n\ndef loadDataSet():\n X = np.array([[1,1],[0,1],[1,0],[0,0]])\n y = np.array([1,-1,-1,-1])\n return X,y\n\nX,y = loadDataSet()\nn_observations,n_features = X.shape\nweights = np.zeros((1,n_features))\nbias = 0\n\nhasConverged = False\nwhile not hasConverged:\n hasConverged = True\n for n in range(n_observations):\n #activation\n a = X[n].dot(weights.T) + bias\n if y[n]*a <= 0:\n hasConverged = False\n #print(\"Classification error\")\n weights += y[n]*X[n]\n bias += y[n]\n \nprint(\"Weights : %s and Bias %d\" % (weights,bias))\n\n#Scatter Plot\nx_pos = X[y==1]\nx_neg = X[y==-1]\n\nplt.scatter(x_pos[:,0],x_pos[:,1],c=\"blue\")\nplt.scatter(x_neg[:,0],x_neg[:,1],c=\"red\")\n\n#show portion of decision boundary (2 dimension)\nX_line = np.linspace(-3,3,100)\nY_line = (-weights[0][0]/weights[0][1])*X_line - bias/(weights[0][1])\nplt.plot(X_line,Y_line)\nplt.show()", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}], "nbformat_minor": 0, "nbformat": 4, "metadata": {"language_info": {"mimetype": "text/x-python", "codemirror_mode": {"name": "ipython", "version": 3}, "pygments_lexer": "ipython3", "name": "python", "nbconvert_exporter": "python", "version": "3.4.3", "file_extension": ".py"}, "kernelspec": {"display_name": "Python 3", "name": "python3", "language": "python"}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment