-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
481c67a
commit 4aa6e8a
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
# ``` |