Skip to content

Commit

Permalink
doco
Browse files Browse the repository at this point in the history
  • Loading branch information
timmenzies committed Aug 2, 2024
1 parent 481c67a commit 4aa6e8a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions etc/header.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cat <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<title>$1</title>
<meta charset="UTF-8">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/vs.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/python.min.js"></script>
<script>hljs.highlightAll();</script>
<link rel="stylesheet" href="ezr.css">
</head><body>
<small><p align="right"><a href="home">home</a> :: <a href="issues">issues</a> :: <a href="license">license</a>
:: <a href="home">home</a> :: <a href="issues">issues</a> :: <a href="license">license</a></p></small>
<h1>$1</h1>
EOF
46 changes: 46 additions & 0 deletions src/knn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# # KNN
# Here is some code that does a 5x5 cross-val for a knn.
# Adapt it to explore k=1,2,5 neasest neghits.
# also looking at the code HERE, try exploring a random subset of
# `train` of size 25,50,200,400
#
# ```python
import random,sys
sys.path.insert(0, '..')
from stats import SOME,some0
from ezr import DATA, SYM, csv, xval, the

def dot(): print(".", file=sys.stderr, flush=True, end="")

def knn(data, k, row1, rows=None):
seen = SYM()
for row in data.neighbors(row1, rows=rows)[:k]:
seen.add( row[data.cols.klass.at] )
return seen.mode

def one(data,k,p, train,test):
n,acc = 0,0
the.p=p
for row in test:
want = row[ data.cols.klass.at ]
got = knn(data, k, row, train)
acc += want==got
n += 1
return acc/n

def main(file):
random.seed(1234567891)
data = DATA().adds(csv(file))
somes = []
for n in [25,50,100,200,100000]:
for k in [1,2,5]:
for p in [1,2,3,4]:
dot()
somes += [SOME(txt=f"k{k}p{p}n{n}")]
for train,test in xval(data.rows, 5,5, n):
somes[-1].add(one(data, k, p, train, test))
print("\n" + file)
some0(somes)

for file in sys.argv[1:]: main(file)
# ```

0 comments on commit 4aa6e8a

Please sign in to comment.