Training_and_Testing.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. ---
  2. redirect_from:
  3. - "/chapters/17/2/training-and-testing"
  4. interact_link: content/chapters/17/2/Training_and_Testing.ipynb
  5. kernel_name: python3
  6. has_widgets: false
  7. title: |-
  8. Training and Testing
  9. prev_page:
  10. url: /chapters/17/1/Nearest_Neighbors.html
  11. title: |-
  12. Nearest Neighbors
  13. next_page:
  14. url: /chapters/17/3/Rows_of_Tables.html
  15. title: |-
  16. Rows of Tables
  17. comment: "***PROGRAMMATICALLY GENERATED, DO NOT EDIT. SEE ORIGINAL FILES IN /content***"
  18. ---
  19. <div class="jb_cell tag_remove_input">
  20. <div class="cell border-box-sizing code_cell rendered">
  21. </div>
  22. </div>
  23. <div class="jb_cell tag_remove_input">
  24. <div class="cell border-box-sizing code_cell rendered">
  25. </div>
  26. </div>
  27. <div class="jb_cell tag_remove_input">
  28. <div class="cell border-box-sizing code_cell rendered">
  29. </div>
  30. </div>
  31. <div class="jb_cell tag_remove_input">
  32. <div class="cell border-box-sizing code_cell rendered">
  33. </div>
  34. </div>
  35. <div class="jb_cell">
  36. <div class="cell border-box-sizing code_cell rendered">
  37. <div class="input">
  38. <div class="inner_cell">
  39. <div class="input_area">
  40. <div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># HIDDEN </span>
  41. <span class="n">ckd</span> <span class="o">=</span> <span class="n">Table</span><span class="o">.</span><span class="n">read_table</span><span class="p">(</span><span class="n">path_data</span> <span class="o">+</span> <span class="s1">&#39;ckd.csv&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">relabeled</span><span class="p">(</span><span class="s1">&#39;Blood Glucose Random&#39;</span><span class="p">,</span> <span class="s1">&#39;Glucose&#39;</span><span class="p">)</span>
  42. <span class="n">ckd</span> <span class="o">=</span> <span class="n">Table</span><span class="p">()</span><span class="o">.</span><span class="n">with_columns</span><span class="p">(</span>
  43. <span class="s1">&#39;Hemoglobin&#39;</span><span class="p">,</span> <span class="n">standard_units</span><span class="p">(</span><span class="n">ckd</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="s1">&#39;Hemoglobin&#39;</span><span class="p">)),</span>
  44. <span class="s1">&#39;Glucose&#39;</span><span class="p">,</span> <span class="n">standard_units</span><span class="p">(</span><span class="n">ckd</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="s1">&#39;Glucose&#39;</span><span class="p">)),</span>
  45. <span class="s1">&#39;White Blood Cell Count&#39;</span><span class="p">,</span> <span class="n">standard_units</span><span class="p">(</span><span class="n">ckd</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="s1">&#39;White Blood Cell Count&#39;</span><span class="p">)),</span>
  46. <span class="s1">&#39;Class&#39;</span><span class="p">,</span> <span class="n">ckd</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="s1">&#39;Class&#39;</span><span class="p">)</span>
  47. <span class="p">)</span>
  48. <span class="n">color_table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">()</span><span class="o">.</span><span class="n">with_columns</span><span class="p">(</span>
  49. <span class="s1">&#39;Class&#39;</span><span class="p">,</span> <span class="n">make_array</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
  50. <span class="s1">&#39;Color&#39;</span><span class="p">,</span> <span class="n">make_array</span><span class="p">(</span><span class="s1">&#39;darkblue&#39;</span><span class="p">,</span> <span class="s1">&#39;gold&#39;</span><span class="p">)</span>
  51. <span class="p">)</span>
  52. <span class="n">ckd</span> <span class="o">=</span> <span class="n">ckd</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39;Class&#39;</span><span class="p">,</span> <span class="n">color_table</span><span class="p">)</span>
  53. </pre></div>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. <div class="jb_cell">
  60. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  61. <div class="text_cell_render border-box-sizing rendered_html">
  62. <h3 id="Training-and-Testing">Training and Testing<a class="anchor-link" href="#Training-and-Testing"> </a></h3><p>How good is our nearest neighbor classifier? To answer this we'll need to find out how frequently our classifications are correct. If a patient has chronic kidney disease, how likely is our classifier to pick that up?</p>
  63. <p>If the patient is in our training set, we can find out immediately. We already know what class the patient is in. So we can just compare our prediction and the patient's true class.</p>
  64. <p>But the point of the classifier is to make predictions for <em>new</em> patients not in our training set. We don't know what class these patients are in but we can make a prediction based on our classifier. How to find out whether the prediction is correct?</p>
  65. <p>One way is to wait for further medical tests on the patient and then check whether or not our prediction agrees with the test results. With that approach, by the time we can say how likely our prediction is to be accurate, it is no longer useful for helping the patient.</p>
  66. <p>Instead, we will try our classifier on some patients whose true classes are known. Then, we will compute the proportion of the time our classifier was correct. This proportion will serve as an estimate of the proportion of all new patients whose class our classifier will accurately predict. This is called <em>testing</em>.</p>
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71. <div class="jb_cell">
  72. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  73. <div class="text_cell_render border-box-sizing rendered_html">
  74. <h3 id="Overly-Optimistic-&quot;Testing&quot;">Overly Optimistic "Testing"<a class="anchor-link" href="#Overly-Optimistic-&quot;Testing&quot;"> </a></h3><p>The training set offers a very tempting set of patients on whom to test out our classifier, because we know the class of each patient in the training set.</p>
  75. <p>But let's be careful ... there will be pitfalls ahead if we take this path. An example will show us why.</p>
  76. <p>Suppose we use a 1-nearest neighbor classifier to predict whether a patient has chronic kidney disease, based on glucose and white blood cell count.</p>
  77. </div>
  78. </div>
  79. </div>
  80. </div>
  81. <div class="jb_cell">
  82. <div class="cell border-box-sizing code_cell rendered">
  83. <div class="input">
  84. <div class="inner_cell">
  85. <div class="input_area">
  86. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">ckd</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="s1">&#39;White Blood Cell Count&#39;</span><span class="p">,</span> <span class="s1">&#39;Glucose&#39;</span><span class="p">,</span> <span class="n">group</span><span class="o">=</span><span class="s1">&#39;Color&#39;</span><span class="p">)</span>
  87. </pre></div>
  88. </div>
  89. </div>
  90. </div>
  91. <div class="output_wrapper">
  92. <div class="output">
  93. <div class="jb_output_wrapper }}">
  94. <div class="output_area">
  95. <div class="output_png output_subarea ">
  96. <img src="../../../images/chapters/17/2/Training_and_Testing_7_0.png"
  97. >
  98. </div>
  99. </div>
  100. </div>
  101. </div>
  102. </div>
  103. </div>
  104. </div>
  105. <div class="jb_cell">
  106. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  107. <div class="text_cell_render border-box-sizing rendered_html">
  108. <p>Earlier, we said that we expect to get some classifications wrong, because there's some intermingling of blue and gold points in the lower-left.</p>
  109. <p>But what about the points in the training set, that is, the points already on the scatter? Will we ever mis-classify them?</p>
  110. <p>The answer is no. Remember that 1-nearest neighbor classification looks for the point <em>in the training set</em> that is nearest to the point being classified. Well, if the point being classified is already in the training set, then its nearest neighbor in the training set is itself! And therefore it will be classified as its own color, which will be correct because each point in the training set is already correctly colored.</p>
  111. <p>In other words, <strong>if we use our training set to "test" our 1-nearest neighbor classifier, the classifier will pass the test 100% of the time.</strong></p>
  112. <p>Mission accomplished. What a great classifier!</p>
  113. <p>No, not so much. A new point in the lower-left might easily be mis-classified, as we noted earlier. "100% accuracy" was a nice dream while it lasted.</p>
  114. <p>The lesson of this example is <em>not</em> to use the training set to test a classifier that is based on it.</p>
  115. </div>
  116. </div>
  117. </div>
  118. </div>
  119. <div class="jb_cell">
  120. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  121. <div class="text_cell_render border-box-sizing rendered_html">
  122. <h3 id="Generating-a-Test-Set">Generating a Test Set<a class="anchor-link" href="#Generating-a-Test-Set"> </a></h3><p>In earlier chapters, we saw that random sampling could be used to estimate the proportion of individuals in a population that met some criterion. Unfortunately, we have just seen that the training set is not like a random sample from the population of all patients, in one important respect: Our classifier guesses correctly for a higher proportion of individuals in the training set than it does for individuals in the population.</p>
  123. <p>When we computed confidence intervals for numerical parameters, we wanted to have many new random samples from a population, but we only had access to a single sample. We solved that problem by taking bootstrap resamples from our sample.</p>
  124. <p>We will use an analogous idea to test our classifier. We will <em>create two samples out of the original training set</em>, use one of the samples as our training set, and <em>the other one for testing</em>.</p>
  125. <p>So we will have three groups of individuals:</p>
  126. <ul>
  127. <li>a training set on which we can do any amount of exploration to build our classifier;</li>
  128. <li>a separate testing set on which to try out our classifier and see what fraction of times it classifies correctly;</li>
  129. <li>the underlying population of individuals for whom we don't know the true classes; the hope is that our classifier will succeed about as well for these individuals as it did for our testing set.</li>
  130. </ul>
  131. </div>
  132. </div>
  133. </div>
  134. </div>
  135. <div class="jb_cell">
  136. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  137. <div class="text_cell_render border-box-sizing rendered_html">
  138. <p>How to generate the training and testing sets? You've guessed it – we'll select at random.</p>
  139. <p>There are 158 individuals in <code>ckd</code>. Let's use a random half of them for training and the other half for testing. To do this, we'll shuffle all the rows, take the first 79 as the training set, and the remaining 79 for testing.</p>
  140. </div>
  141. </div>
  142. </div>
  143. </div>
  144. <div class="jb_cell">
  145. <div class="cell border-box-sizing code_cell rendered">
  146. <div class="input">
  147. <div class="inner_cell">
  148. <div class="input_area">
  149. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">shuffled_ckd</span> <span class="o">=</span> <span class="n">ckd</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">with_replacement</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  150. <span class="n">training</span> <span class="o">=</span> <span class="n">shuffled_ckd</span><span class="o">.</span><span class="n">take</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">79</span><span class="p">))</span>
  151. <span class="n">testing</span> <span class="o">=</span> <span class="n">shuffled_ckd</span><span class="o">.</span><span class="n">take</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">79</span><span class="p">,</span> <span class="mi">158</span><span class="p">))</span>
  152. </pre></div>
  153. </div>
  154. </div>
  155. </div>
  156. </div>
  157. </div>
  158. <div class="jb_cell">
  159. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  160. <div class="text_cell_render border-box-sizing rendered_html">
  161. <p>Now let's construct our classifier based on the points in the training sample:</p>
  162. </div>
  163. </div>
  164. </div>
  165. </div>
  166. <div class="jb_cell">
  167. <div class="cell border-box-sizing code_cell rendered">
  168. <div class="input">
  169. <div class="inner_cell">
  170. <div class="input_area">
  171. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">training</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="s1">&#39;White Blood Cell Count&#39;</span><span class="p">,</span> <span class="s1">&#39;Glucose&#39;</span><span class="p">,</span> <span class="n">group</span><span class="o">=</span><span class="s1">&#39;Color&#39;</span><span class="p">)</span>
  172. <span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
  173. <span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">6</span><span class="p">);</span>
  174. </pre></div>
  175. </div>
  176. </div>
  177. </div>
  178. <div class="output_wrapper">
  179. <div class="output">
  180. <div class="jb_output_wrapper }}">
  181. <div class="output_area">
  182. <div class="output_png output_subarea ">
  183. <img src="../../../images/chapters/17/2/Training_and_Testing_13_0.png"
  184. >
  185. </div>
  186. </div>
  187. </div>
  188. </div>
  189. </div>
  190. </div>
  191. </div>
  192. <div class="jb_cell">
  193. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  194. <div class="text_cell_render border-box-sizing rendered_html">
  195. <p>We get the following classification regions and decision boundary:</p>
  196. </div>
  197. </div>
  198. </div>
  199. </div>
  200. <div class="jb_cell tag_remove_input">
  201. <div class="cell border-box-sizing code_cell rendered">
  202. </div>
  203. </div>
  204. <div class="jb_cell tag_remove_input">
  205. <div class="cell border-box-sizing code_cell rendered">
  206. </div>
  207. </div>
  208. <div class="jb_cell tag_remove_input">
  209. <div class="cell border-box-sizing code_cell rendered">
  210. <div class="output_wrapper">
  211. <div class="output">
  212. <div class="jb_output_wrapper }}">
  213. <div class="output_area">
  214. <div class="output_png output_subarea ">
  215. <img src="../../../images/chapters/17/2/Training_and_Testing_17_0.png"
  216. >
  217. </div>
  218. </div>
  219. </div>
  220. </div>
  221. </div>
  222. </div>
  223. </div>
  224. <div class="jb_cell">
  225. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  226. <div class="text_cell_render border-box-sizing rendered_html">
  227. <p>Place the <em>test</em> data on this graph and you can see at once that while the classifier got almost all the points right, there are some mistakes. For example, some blue points of the test set fall in the gold region of the classifier.</p>
  228. </div>
  229. </div>
  230. </div>
  231. </div>
  232. <div class="jb_cell tag_remove_input">
  233. <div class="cell border-box-sizing code_cell rendered">
  234. <div class="output_wrapper">
  235. <div class="output">
  236. <div class="jb_output_wrapper }}">
  237. <div class="output_area">
  238. <div class="output_png output_subarea ">
  239. <img src="../../../images/chapters/17/2/Training_and_Testing_19_0.png"
  240. >
  241. </div>
  242. </div>
  243. </div>
  244. </div>
  245. </div>
  246. </div>
  247. </div>
  248. <div class="jb_cell">
  249. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  250. <div class="text_cell_render border-box-sizing rendered_html">
  251. <p>Some errors notwithstanding, it looks like the classifier does fairly well on the test set. Assuming that the original sample was drawn randomly from the underlying population, the hope is that the classifier will perform with similar accuracy on the overall population, since the test set was chosen randomly from the original sample.</p>
  252. </div>
  253. </div>
  254. </div>
  255. </div>