Guest User

Untitled

a guest
Apr 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 24,
  6. "metadata": {
  7. "collapsed": true
  8. },
  9. "outputs": [],
  10. "source": [
  11. "from random import random\n",
  12. "import numpy as np"
  13. ]
  14. },
  15. {
  16. "cell_type": "code",
  17. "execution_count": 73,
  18. "metadata": {},
  19. "outputs": [],
  20. "source": [
  21. "def generate_separator():\n",
  22. " p1 = np.array([random(), random()])\n",
  23. " p2 = np.array([random(), random()])\n",
  24. " \n",
  25. " #v is a vector orthogonal to p1->p2\n",
  26. " d = (p2[1]-p1[1],p1[0]-p2[0]) \n",
  27. "\n",
  28. " #if angle between d and p1->p2 is less than 90,\n",
  29. " #then it is on one side of the line containing p1 and p2.\n",
  30. " #Otherwise, it is on the other side.\n",
  31. " def sep(p):\n",
  32. " v = p-p1\n",
  33. " if np.dot(v,d) > 0:\n",
  34. " return 1\n",
  35. " else:\n",
  36. " return -1\n",
  37. " \n",
  38. " return sep\n",
  39. "\n",
  40. "def generate_data(n, separator):\n",
  41. " \n",
  42. " data = []\n",
  43. " Y = []\n",
  44. " for _ in range(n):\n",
  45. " x = np.array([random(), random()])\n",
  46. " data.append(np.array([1,x[0],x[1]]))\n",
  47. " \n",
  48. " Y.append(separator(x))\n",
  49. " \n",
  50. " return data, Y\n",
  51. " \n",
  52. "\n"
  53. ]
  54. },
  55. {
  56. "cell_type": "code",
  57. "execution_count": 75,
  58. "metadata": {},
  59. "outputs": [
  60. {
  61. "data": {
  62. "text/plain": [
  63. "([array([ 1. , 0.35013412, 0.04465456]),\n",
  64. " array([ 1. , 0.23817193, 0.14952078]),\n",
  65. " array([ 1. , 0.41158375, 0.0374665 ]),\n",
  66. " array([ 1. , 0.87463945, 0.93374851]),\n",
  67. " array([ 1. , 0.97438633, 0.97100039]),\n",
  68. " array([ 1. , 0.58135 , 0.49672657]),\n",
  69. " array([ 1. , 0.75894212, 0.57618606]),\n",
  70. " array([ 1. , 0.10616376, 0.67048477]),\n",
  71. " array([ 1. , 0.21371601, 0.81160833]),\n",
  72. " array([ 1. , 0.12049503, 0.47699181])],\n",
  73. " [1, 1, 1, -1, -1, 1, 1, 1, 1, 1])"
  74. ]
  75. },
  76. "execution_count": 75,
  77. "metadata": {},
  78. "output_type": "execute_result"
  79. }
  80. ],
  81. "source": [
  82. "sep = generate_separator()\n",
  83. "generate_data(10, sep)"
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "execution_count": 82,
  89. "metadata": {
  90. "collapsed": true
  91. },
  92. "outputs": [],
  93. "source": [
  94. "def lpa(data,Y):\n",
  95. " missed = True\n",
  96. " w = np.array([0,0,0])\n",
  97. " n = 0\n",
  98. " while missed:\n",
  99. " missed = False\n",
  100. " \n",
  101. " for x,y in zip(data,Y):\n",
  102. " \n",
  103. " c = np.sign(np.dot(w, x))\n",
  104. " if c != y:\n",
  105. " missed = True\n",
  106. " w = w + y*x\n",
  107. " n += 1\n",
  108. " return w, n\n",
  109. "\n",
  110. "def classify(w, x):\n",
  111. " return np.sign(np.dot(w, x))\n",
  112. " \n",
  113. "def estimate_E_out(w, sep):\n",
  114. " e = 0\n",
  115. " for _ in range(500):\n",
  116. " x = np.array([random(), random()])\n",
  117. " x_ = np.array([1, x[0], x[1]])\n",
  118. " if sep(x) != classify(w, x_):\n",
  119. " e += 1\n",
  120. " return e / 500.\n",
  121. " "
  122. ]
  123. },
  124. {
  125. "cell_type": "code",
  126. "execution_count": 85,
  127. "metadata": {},
  128. "outputs": [
  129. {
  130. "data": {
  131. "text/plain": [
  132. "(192.395, 0.013767999999999926)"
  133. ]
  134. },
  135. "execution_count": 85,
  136. "metadata": {},
  137. "output_type": "execute_result"
  138. }
  139. ],
  140. "source": [
  141. "count = 0\n",
  142. "sum_es = 0\n",
  143. "for _ in range(1000):\n",
  144. " \n",
  145. " sep = generate_separator()\n",
  146. " w, n = lpa(*generate_data(100, sep))\n",
  147. " \n",
  148. " sum_es += estimate_E_out(w, sep)\n",
  149. " count += n\n",
  150. " \n",
  151. "(count / 1000., sum_es / 1000.)"
  152. ]
  153. },
  154. {
  155. "cell_type": "code",
  156. "execution_count": null,
  157. "metadata": {
  158. "collapsed": true
  159. },
  160. "outputs": [],
  161. "source": []
  162. }
  163. ],
  164. "metadata": {
  165. "kernelspec": {
  166. "display_name": "Python 3",
  167. "language": "python",
  168. "name": "python3"
  169. },
  170. "language_info": {
  171. "codemirror_mode": {
  172. "name": "ipython",
  173. "version": 3
  174. },
  175. "file_extension": ".py",
  176. "mimetype": "text/x-python",
  177. "name": "python",
  178. "nbconvert_exporter": "python",
  179. "pygments_lexer": "ipython3",
  180. "version": "3.5.1"
  181. }
  182. },
  183. "nbformat": 4,
  184. "nbformat_minor": 2
  185. }
Add Comment
Please, Sign In to add comment