-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn_class.tcl
39 lines (30 loc) · 907 Bytes
/
knn_class.tcl
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
# --------------------------------
# The f8 (fate) scripting language
# --------------------------------
#
# Example: a simple KNN classifier
#
# (c) www.carminecella.com
#
source "stdlib.tcl"
source "learning.tcl"
set f (openstream "../data/iris.data.txt" 'input 'text)
set orig (readcsv f)
set orig_sh (lshuffle orig)
set data (dataset orig_sh)
set samples (llength data)
set features (size (car (car data)))
puts samples " samples - " features " features\n\n"
set trainsz (floor (* 80 (/ samples 100)))
set testsz (- samples trainsz)
set trainset (lrange data 0 trainsz)
set testset (lrange data trainsz testsz)
puts "split: " (llength trainset) " train samples, " (llength testset) " test samples\n\n"
puts "training..."
set model (knntrain trainset 3)
puts "done\n"
puts "testing..."
set classes (knntest model testset)
puts "done\n\n"
puts "accuracy = " (accuracy classes testset) "\n"
# eof