forked from JMailloH/kNN_IS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.sql.bak
90 lines (82 loc) · 1.93 KB
/
knn.sql.bak
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
Drop table IF EXISTS iris_train;
Drop table IF EXISTS iris_test;
Drop table IF EXISTS iris_id_dist;
Drop table IF EXISTS iris_test_id;
Drop table if EXISTS almost_done;
CREATE TABLE iris_train (
SepalLength double,
SepalWidth double,
PetalLength double,
PetalWidth double,
Class string)
USING csv
OPTIONS (path "/home/sparky/iris/iris_train.csv", header "false", inferSchema "false");
CREATE TABLE iris_test (
-- id int,
SepalLength double,
SepalWidth double,
PetalLength double,
PetalWidth double,
Class string)
USING csv
OPTIONS (path "/home/sparky/iris/iris_test.csv", header "false", inferSchema "false");
-- So now we need to figure out how we calculate stuff here, naively
CREATE TABLE iris_test_id (
id int,
SepalLength double,
SepalWidth double,
PetalLength double,
PetalWidth double,
Class string);
Insert into iris_test_id
Select
ROW_NUMBER() OVER(Order by PetalWidth) as id,
SepalLength,
SepalWidth,
PetalLength,
PetalWidth,
Class string
from iris_test;
Create Table iris_id_dist (
id int,
dist double,
class string,
actual_class string
);
Insert into iris_id_dist
Select
test.id as id,
SQRT(
POW((test.SepalLength - train.SepalLength ), 2) +
POW((test.SepalWidth - train.SepalWidth ), 2) +
POW((test.PetalLength - train.PetalLength ), 2) +
POW((test.PetalWidth - train.PetalWidth ), 2)
) as distance,
train.class,
test.class
from iris_test_id as test
cross join iris_train train;
Select id, actual_class, class as predictedClass from (
Select
id,
actual_class,
class,
ROW_NUMBER() OVER (Partition by id order by votes desc) as vote_rank
from (
Select
id,
actual_class,
class,
Count(*) as votes
from (
Select
iris_id_dist.*,
ROW_NUMBER() OVER (Partition by id Order By iris_id_dist.dist asc ) as dist_class
from iris_id_dist
) derpy
where derpy.dist_class <= 5
group by id, class, actual_class
) as lerpy
) as outie
where outie.vote_rank = 1
order by id asc ;