Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-02-20T12:55:25.150873Z",
- "start_time": "2019-02-20T12:55:24.620811Z"
- }
- },
- "outputs": [],
- "source": [
- "import numpy as np\n",
- "from random import sample\n",
- "import matplotlib.pyplot as plt\n",
- "%matplotlib inline"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## exercise 1.4"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-02-20T12:55:26.077169Z",
- "start_time": "2019-02-20T12:55:26.067921Z"
- }
- },
- "outputs": [],
- "source": [
- "def fx(x): # target line\n",
- " return(-0.7 * x + 1)\n",
- "# build random data x_i in [0,1] with bias term\n",
- "xi = np.column_stack((np.ones(20), np.random.rand(20,2)))\n",
- "yi = (fx(xi[:,1]) > xi[:,2]) # T/F vector\n",
- "# xi"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-02-20T12:55:26.761259Z",
- "start_time": "2019-02-20T12:55:26.755219Z"
- }
- },
- "outputs": [],
- "source": [
- "yp = np.zeros(20) # build target vec in {-1, 1}\n",
- "yp[yi] = 1\n",
- "yp[np.logical_not(yi)] = -1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-02-20T12:55:27.619844Z",
- "start_time": "2019-02-20T12:55:27.284779Z"
- }
- },
- "outputs": [],
- "source": [
- "# plot data with f(x)\n",
- "plt.scatter(xi[:,1], xi[:,2], c=yp)\n",
- "plt.plot(np.arange(0,1,.1), fx(np.arange(0,1,.1)))\n",
- "plt.savefig(\"aml_e1.4_fx.png\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-02-20T12:55:38.064284Z",
- "start_time": "2019-02-20T12:55:38.056503Z"
- },
- "code_folding": [
- 0
- ]
- },
- "outputs": [],
- "source": [
- "def fwd(x, y, w):\n",
- " ''' predict function \n",
- " in: x, y, current weights \n",
- " out: vector w/ True for misses '''\n",
- " yh = x @ w\n",
- " yh[yh > 0] = 1\n",
- " yh[yh < 0] = -1\n",
- " \n",
- " return(yh.T != y)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-02-20T12:55:46.651852Z",
- "start_time": "2019-02-20T12:55:46.631850Z"
- }
- },
- "outputs": [],
- "source": [
- "w = np.random.rand(xi.shape[1],1) * 2 - 1 #random init weights [-1,1]\n",
- "neq = fwd(xi, yp, w)\n",
- "while np.sum(neq): # while some misses\n",
- " # randomly choose a datapoint where x misclassified\n",
- " ud = np.random.choice(np.where(neq==True)[1], 1) \n",
- " w = w + yp[ud] * xi[ud].T # update w\n",
- " neq = fwd(xi, yp, w) # get new miss vector\n",
- " print(np.sum(neq)) # show improvement\n",
- "print(w) # final weights"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-02-20T12:59:46.858343Z",
- "start_time": "2019-02-20T12:59:46.850840Z"
- }
- },
- "outputs": [],
- "source": [
- "print(w[1] / w[2]) # get slope\n",
- "print(w[0] / w[2]) # get bias"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-02-20T13:00:51.666033Z",
- "start_time": "2019-02-20T13:00:51.661232Z"
- }
- },
- "outputs": [],
- "source": [
- "## b + w1x1 + w2x2 = 0 ##\n",
- "wfxn = -((np.arange(0,1,.1) * (w[1])) + w[0])/w[2]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-02-20T13:00:52.706379Z",
- "start_time": "2019-02-20T13:00:52.433112Z"
- }
- },
- "outputs": [],
- "source": [
- "\n",
- "plt.scatter(x=xi[:,1], y = xi[:,2], c=yp)\n",
- "plt.plot(np.arange(0,1,.1), wfxn)\n",
- "plt.savefig(\"aml_e1.4_gx.png\")"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.7"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement