-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.html
444 lines (432 loc) · 37.6 KB
/
table.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>table.py</title>
<link rel="stylesheet" href="pycco.css">
<meta name="thumbnail" content="https://avatars0.githubusercontent.com/u/23156192?v=3&s=200" />
<link rel="shortcut icon" href="favicon.png" type="image/png">
</head>
<body>
<div id='container'>
<a href="https://github.com/ttv1/src/blob/master/table.py"><img style="z-index: 10; position: absolute; top: 0; left: 0; border: 0;" src="https://camo.githubusercontent.com/567c3a48d796e2fc06ea80409cc9dd82bf714434/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_left_darkblue_121621.png"></a>
<div id="background"></div>
<div class='section'>
<div class='docs'><h1> table.py</h1><p> This is ttv1 code (Timm tools, version 1).</p></div>
</div>
<div class='clearall'>
<div class='section' id='section-0'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-0'>#</a>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-55634425-2', 'auto');
ga('send', 'pageview');
</script>
<p><img
src="https://avatars0.githubusercontent.com/u/23156192?v=3&s=200"
align=left
width=120>
<br> <br>
<a href="http://ttv1.github.io">home</a> | <a href="#discussion">discuss</a> | <a href="https://github.com/ttv1/src/issues">report bug</a>
<br>
<script>
(function() {
var cx = '009630129455493240085:aja5uvdnjeo';
var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>
<div style="width:0px;overflow:hidden;height:0px;"> <!-- if you use display:none here, it doesn't work-->
<gcse:search></gcse:search>
</div>
<form style="margin-left: 20px;" id="searchbox_009630129455493240085:aja5uvdnjeo" action="">
<input value="009630129455493240085:aja5uvdnjeo" name="cx" type="hidden"/>
<input value="FORID:11" name="cof" type="hidden"/>
<input id="q" style="" name="q" size="25" type="text"/>
<button class="btn">Search</button>
</form></p>
<p><br clear=all></p>
<hr />
<p>Given a source of csv data, <code>table</code> reads and stores the rows while at the same
time keeping statistics on what was seen in each row.</p>
<p>Column statistics are kept as <code>thing</code>s which are instances of a class that
watches a stream of data and works out if that column is a numeric or symbolic.
All these <code>things</code> are stored in <code>table.all</code> as well as in the <em>column groups</em>
defined in <code>table.COLS</code>.</p>
<pre><code> DEFAULT= "x"
COLS = dict(less = "<", # numeric goals to minimize
more = ">", # numeric goals to maximize
klass= ":", # symbolic goal (for classification)
y = ":<>"):# all symbols denoting goals
</code></pre>
<p>Any column with a header that starts with something else, get stored in the
group table.DEFAULT.</p>
<p>By default, <code>table</code> reads all its data at once from a <code>inits</code> list passes to its
constructor. But <code>table</code> can also read table rows incrementally from strings, a
file, or a file inside a zip file.</p>
<p>For very fast csv reads, use <code>csv</code> not <code>table</code>. <code>Table</code> uses <code>csv</code> as a
primitive but also keeps extensive statistics on each column. If those
statistics are not required, then use <code>csv</code> for (much) faster loads. For
example, on one machine, with pypy3, <code>csv</code> can load nearly two million records
in under six seconds while in the same time <code>table</code> can only load 10,000.</p>
<p>For clues on how to optimize <code>table</code>, see the profiler output of <code>pypy3
tableg.py -- _table3</code>.</p>
<hr />
<h2>Programmer's Guide</h2>
</div>
<div class='code'>
<div class="highlight"><pre><span class="kn">from</span> <span class="nn">thing</span> <span class="kn">import</span> <span class="n">thing</span>
<span class="kn">from</span> <span class="nn">csv</span> <span class="kn">import</span> <span class="n">csv</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-1'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-1'>#</a>
</div>
</div>
<div class='code'>
<div class="highlight"><pre><span class="k">class</span> <span class="nc">table</span><span class="p">:</span>
<span class="n">K</span> <span class="o">=</span> <span class="mi">3</span>
<span class="n">W</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span><span class="n">x</span>
<span class="n">DEFAULT</span> <span class="o">=</span> <span class="s">"x"</span>
<span class="n">COLS</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">less</span> <span class="o">=</span> <span class="s">"<"</span><span class="p">,</span> <span class="c"># numeric goals to be minimized</span>
<span class="n">more</span> <span class="o">=</span> <span class="s">">"</span><span class="p">,</span> <span class="c"># numeric goals to be maximized</span>
<span class="n">klass</span><span class="o">=</span> <span class="s">":"</span><span class="p">,</span> <span class="c"># symbolic goal (used for classification)</span>
<span class="n">y</span> <span class="o">=</span> <span class="s">":<>"</span> <span class="c"># all symbols denoting goals</span>
<span class="p">)</span> <span class="c"># (any any other thing goes into table.DEFAULT)</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-2'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-2'>#</a>
</div>
<p>Read table either from a <code>str</code>, a <code>file</code>, a <code>file</code> in a <code>zip</code>
or from some <code>inits</code> list.</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">inits</span><span class="o">=</span><span class="p">[],</span><span class="nb">str</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="nb">file</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="nb">zip</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="n">i</span><span class="o">.</span><span class="n">rows</span><span class="p">,</span> <span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">,</span> <span class="n">i</span><span class="o">.</span><span class="n">all</span> <span class="o">=</span> <span class="p">[],{},[]</span>
<span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="n">table</span><span class="o">.</span><span class="n">DEFAULT</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">table</span><span class="o">.</span><span class="n">COLS</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="nb">file</span><span class="p">:</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">csv</span><span class="p">(</span><span class="nb">str</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="nb">file</span><span class="p">,</span><span class="nb">zip</span><span class="o">=</span><span class="nb">zip</span><span class="p">,</span><span class="n">header</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<span class="n">i</span> <span class="o">+</span> <span class="n">row</span>
<span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="n">row</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">inits</span><span class="p">]</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-3'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-3'>#</a>
</div>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="k">return</span> <span class="s">'{:rows </span><span class="si">%s</span><span class="s"> :less </span><span class="si">%s</span><span class="s"> :more </span><span class="si">%s</span><span class="s"> :klass </span><span class="si">%s</span><span class="s"> :x </span><span class="si">%s</span><span class="s"> :y </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span>
<span class="nb">len</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">rows</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="s">"less"</span><span class="p">]),</span> <span class="nb">len</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="s">"more"</span><span class="p">]),</span>
<span class="nb">len</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="s">"klass"</span><span class="p">]),</span> <span class="nb">len</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="s">"x"</span><span class="p">]),</span> <span class="nb">len</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="s">"y"</span><span class="p">]))</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-4'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-4'>#</a>
</div>
<p>If <code>all</code> is defined, we are beyong the first header row.</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">__add__</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">row</span><span class="p">):</span>
<span class="n">i</span><span class="o">.</span><span class="n">data</span><span class="p">(</span><span class="n">row</span><span class="p">)</span> <span class="k">if</span> <span class="n">i</span><span class="o">.</span><span class="n">all</span> <span class="k">else</span> <span class="n">i</span><span class="o">.</span><span class="n">header</span><span class="p">(</span><span class="n">row</span><span class="p">)</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-5'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-5'>#</a>
</div>
<p>Create one <code>thing</code> for each column.
Store that <code>thing</code> in <code>all</code> as well as
in its associated <code>COLS</code> group.</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">header</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">row</span><span class="p">):</span>
<span class="k">for</span> <span class="n">col</span><span class="p">,</span><span class="n">cell</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">row</span><span class="p">):</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">thing</span><span class="p">(</span><span class="n">pos</span><span class="o">=</span><span class="n">col</span><span class="p">,</span> <span class="n">txt</span><span class="o">=</span><span class="n">cell</span><span class="p">)</span>
<span class="n">i</span><span class="o">.</span><span class="n">all</span> <span class="o">+=</span> <span class="p">[</span><span class="n">t</span><span class="p">]</span>
<span class="n">placed</span> <span class="o">=</span> <span class="bp">False</span>
<span class="k">for</span> <span class="n">key</span><span class="p">,</span><span class="n">chars</span> <span class="ow">in</span> <span class="n">table</span><span class="o">.</span><span class="n">COLS</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="k">for</span> <span class="n">char</span> <span class="ow">in</span> <span class="n">chars</span><span class="p">:</span>
<span class="k">if</span> <span class="n">cell</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">char</span><span class="p">:</span>
<span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">+=</span> <span class="p">[</span><span class="n">t</span><span class="p">]</span>
<span class="n">placed</span> <span class="o">=</span> <span class="bp">True</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-6'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-6'>#</a>
</div>
<p>If we can't place it anywhere else, place it in <code>table.DEFAULT</code>.</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">if</span> <span class="ow">not</span> <span class="n">placed</span><span class="p">:</span>
<span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="n">table</span><span class="o">.</span><span class="n">DEFAULT</span><span class="p">]</span> <span class="o">+=</span> <span class="p">[</span><span class="n">t</span><span class="p">]</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-7'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-7'>#</a>
</div>
<p>Update the statistics held in each thing for each column.
Keep the data in <code>rows</code>.</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">data</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">row</span><span class="p">):</span>
<span class="p">[</span><span class="n">t</span> <span class="o">+</span> <span class="n">row</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">pos</span><span class="p">]</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">i</span><span class="o">.</span><span class="n">all</span><span class="p">]</span>
<span class="n">i</span><span class="o">.</span><span class="n">rows</span> <span class="o">+=</span> <span class="p">[</span> <span class="n">row</span> <span class="p">]</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-8'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-8'>#</a>
</div>
<h3>Misc utilities</h3>
</div>
<div class='code'>
<div class="highlight"><pre></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-9'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-9'>#</a>
</div>
<p>Return a table just like this one,
but withtout the row data</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">twin</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">inits</span><span class="o">=</span><span class="p">[]):</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">table</span><span class="p">([[</span><span class="n">t</span><span class="o">.</span><span class="n">txt</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">i</span><span class="o">.</span><span class="n">all</span><span class="p">]])</span>
<span class="p">[</span><span class="n">t</span> <span class="o">+</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inits</span><span class="p">]</span>
<span class="k">return</span> <span class="n">t</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-10'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-10'>#</a>
</div>
<p>Return the first klass value of a row</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">klass</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">row</span><span class="p">):</span>
<span class="k">return</span> <span class="n">row</span><span class="p">[</span><span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="s">"klass"</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">pos</span><span class="p">]</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-11'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-11'>#</a>
</div>
<p>Return the first goal value of a row</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">goal</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">row</span><span class="p">):</span>
<span class="k">return</span> <span class="n">row</span><span class="p">[</span><span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="s">"y"</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">pos</span><span class="p">]</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-12'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-12'>#</a>
</div>
<p>Compute Euclidean distance between two rows.
Makes use of sevices defined in each thing in <code>all</code>.</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">dist</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span><span class="n">k</span><span class="p">,</span> <span class="n">what</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="n">ds</span><span class="p">,</span><span class="n">ns</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span><span class="mf">1e-32</span>
<span class="n">what</span> <span class="o">=</span> <span class="n">what</span> <span class="ow">or</span> <span class="p">[</span><span class="n">table</span><span class="o">.</span><span class="n">DEFAULT</span><span class="p">]</span>
<span class="k">for</span> <span class="n">grp</span> <span class="ow">in</span> <span class="n">what</span><span class="p">:</span>
<span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">i</span><span class="o">.</span><span class="n">group</span><span class="p">[</span><span class="n">grp</span><span class="p">]:</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">dist</span><span class="p">(</span><span class="n">j</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">pos</span><span class="p">],</span> <span class="n">k</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">pos</span><span class="p">])</span>
<span class="k">if</span> <span class="n">d</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">ds</span> <span class="o">+=</span> <span class="n">d</span>
<span class="n">ns</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">return</span> <span class="n">ds</span><span class="o">**</span><span class="mf">0.5</span> <span class="o">/</span> <span class="n">ns</span><span class="o">**</span><span class="mf">0.5</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-13'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-13'>#</a>
</div>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">nearest</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">row</span><span class="p">,</span><span class="n">rows</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">what</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">details</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
<span class="n">bt</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:</span> <span class="n">x</span><span class="o"><</span> <span class="n">y</span><span class="p">,</span>
<span class="n">zero</span> <span class="o">=</span> <span class="mf">1e32</span><span class="p">):</span>
<span class="n">best</span> <span class="o">=</span> <span class="n">zero</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">row</span>
<span class="n">rows</span> <span class="o">=</span> <span class="n">rows</span> <span class="ow">or</span> <span class="n">i</span><span class="o">.</span><span class="n">rows</span>
<span class="k">for</span> <span class="n">otherRow</span> <span class="ow">in</span> <span class="n">rows</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">id</span><span class="p">(</span><span class="n">row</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">id</span><span class="p">(</span><span class="n">otherRow</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">dist</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">otherRow</span><span class="p">,</span>
<span class="n">what</span><span class="o">=</span><span class="n">what</span> <span class="ow">or</span> <span class="p">[</span><span class="n">table</span><span class="o">.</span><span class="n">DEFAULT</span><span class="p">])</span>
<span class="k">if</span> <span class="n">bt</span><span class="p">(</span><span class="n">tmp</span><span class="p">,</span><span class="n">best</span><span class="p">):</span>
<span class="n">out</span><span class="p">,</span><span class="n">best</span> <span class="o">=</span> <span class="n">otherRow</span><span class="p">,</span><span class="n">tmp</span>
<span class="k">return</span> <span class="n">out</span><span class="p">,</span><span class="n">best</span> <span class="k">if</span> <span class="n">details</span> <span class="k">else</span> <span class="n">out</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-14'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-14'>#</a>
</div>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">furthest</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">row</span><span class="p">,</span><span class="n">rows</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">what</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">details</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">return</span> <span class="n">i</span><span class="o">.</span><span class="n">nearest</span><span class="p">(</span><span class="n">row</span><span class="p">,</span>
<span class="n">what</span> <span class="o">=</span> <span class="n">what</span> <span class="ow">or</span> <span class="p">[</span><span class="n">table</span><span class="o">.</span><span class="n">DEFAULT</span><span class="p">],</span>
<span class="n">rows</span> <span class="o">=</span> <span class="n">rows</span> <span class="ow">or</span> <span class="n">i</span><span class="o">.</span><span class="n">rows</span><span class="p">,</span>
<span class="n">bt</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:</span> <span class="n">x</span> <span class="o">></span> <span class="n">y</span><span class="p">,</span>
<span class="n">zero</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
<span class="n">details</span><span class="o">=</span><span class="n">details</span><span class="p">)</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-15'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-15'>#</a>
</div>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">distances</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">rows</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">what</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="n">out</span><span class="p">,</span><span class="n">index</span> <span class="o">=</span> <span class="p">{},{}</span>
<span class="n">rows</span> <span class="o">=</span> <span class="n">rows</span> <span class="ow">or</span> <span class="n">i</span><span class="o">.</span><span class="n">rows</span>
<span class="k">for</span> <span class="n">j</span><span class="p">,</span><span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span>
<span class="n">out</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">index</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">row</span>
<span class="k">for</span> <span class="n">j</span><span class="p">,</span><span class="n">row1</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span>
<span class="k">for</span> <span class="n">k</span><span class="p">,</span><span class="n">row2</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span>
<span class="k">if</span> <span class="n">j</span> <span class="o">></span> <span class="n">k</span><span class="p">:</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">dist</span><span class="p">(</span><span class="n">row1</span><span class="p">,</span><span class="n">row2</span><span class="p">,</span>
<span class="n">what</span> <span class="o">=</span> <span class="n">what</span> <span class="ow">or</span> <span class="p">[</span><span class="n">table</span><span class="o">.</span><span class="n">DEFAULT</span><span class="p">])</span>
<span class="n">out</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="p">[(</span><span class="n">d</span><span class="p">,</span><span class="n">k</span><span class="p">)]</span>
<span class="n">out</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+=</span> <span class="p">[(</span><span class="n">d</span><span class="p">,</span><span class="n">j</span><span class="p">)]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">out</span><span class="p">:</span>
<span class="n">out</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
<span class="k">return</span> <span class="n">out</span><span class="p">,</span><span class="n">index</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-16'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-16'>#</a>
</div>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">knn</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">row1</span><span class="p">,</span><span class="n">k</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">rows</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">w</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="n">k</span> <span class="o">=</span> <span class="n">k</span> <span class="ow">or</span> <span class="n">table</span><span class="o">.</span><span class="n">K</span>
<span class="n">w</span> <span class="o">=</span> <span class="n">w</span> <span class="ow">or</span> <span class="n">table</span><span class="o">.</span><span class="n">W</span>
<span class="n">rows</span><span class="o">=</span><span class="n">rows</span> <span class="ow">or</span> <span class="n">i</span><span class="o">.</span><span class="n">rows</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[(</span><span class="n">w</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">dist</span><span class="p">(</span><span class="n">row1</span><span class="p">,</span><span class="n">row2</span><span class="p">)),</span>
<span class="n">i</span><span class="o">.</span><span class="n">klass</span><span class="p">(</span><span class="n">row2</span><span class="p">))</span>
<span class="k">for</span> <span class="n">row2</span> <span class="ow">in</span> <span class="n">rows</span>
<span class="k">if</span> <span class="nb">id</span><span class="p">(</span><span class="n">row1</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">id</span><span class="p">(</span><span class="n">row2</span><span class="p">)]</span>
<span class="n">kth</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">tmp</span><span class="p">)[:</span><span class="n">k</span><span class="p">]</span>
<span class="n">scores</span><span class="o">=</span><span class="p">{}</span>
<span class="k">for</span> <span class="n">w</span><span class="p">,</span><span class="n">klass</span> <span class="ow">in</span> <span class="n">kth</span><span class="p">:</span>
<span class="n">scores</span><span class="p">[</span><span class="n">klass</span><span class="p">]</span> <span class="o">=</span> <span class="n">scores</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">klass</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="n">w</span>
<span class="n">ranked</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">scores</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span><span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="k">return</span> <span class="n">ranked</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-17'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-17'>#</a>
</div>
<hr />
<p><img align=right
src="https://raw.githubusercontent.com/timm/timm.github.io/master/timm.png"
width=170></p>
<h2>Copyleft</h2>
<p>Copyright © 2016,2017 Tim Menzies <a href="mailto:tim@menzies.us">tim@menzies.us</a>, MIT license v2.</p>
<p>Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:</p>
<p>The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
<p>Share and enjoy.</p>
<hr>
<a name="discussion"><h2>Discussion</h2></a>
<div id="disqus_thread"></div>
<script>
/**
* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
/*
var disqus_config = function () {
this.page.url = 'https://ttv1.github.ip/table.html'
this.page.identifier = 'table';
};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//ttv1.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
<div class='code'>
<div class="highlight"><pre></pre></div>
</div>
</div>
<div class='clearall'></div>
</div>
<script id="dsq-count-scr" src="//ttv1.disqus.com/count.js" async></script>
</body>
</html>