-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.cpp
129 lines (109 loc) · 2.59 KB
/
knn.cpp
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
#include<bits/stdc++.h>
using namespace std;
vector<int> distance(vector<int>a, vector<int>b, vector<int>t)
{
vector<int>d;
int dis=0;
//euclidian distance
for (int i = 0; i < a.size(); i++)
{
dis = (a[i]-t[0])*(a[i]-t[0]) + (b[i]-t[1])*(b[i]-t[1]);
d.push_back(dis);
}
return d;
}
int initialize_k(vector<int>a)
{
int n = a.size();
int k = sqrt(n);
if(k < 3)
k = 3;
if(k > 3)
{
if(k % 2 == 0)
k = k + 1;
}
return k;
}
string mostFrequent(vector<string>arr)
{
map<string, int> m;
// count occurrences of every string
for (int i = 0; i < arr.size(); i++)
{
map<string, int>::iterator it = m.find(arr[i]);
if (it == m.end())
m.insert(pair<string, int>(arr[i], 1));
else
m[arr[i]] += 1;
}
// find the max
map<string, int>::iterator it = m.begin();
for (auto it2 = m.begin(); it2 != m.end(); ++it2)
{
if (it2 -> second > it -> second)
it = it2;
}
return it->first;
}
string knn_Classify(vector<int>a, vector<int>b, vector<string>c, vector<int>t)
{
vector<int>d;
vector<int>tmp;
vector<string>label;
vector<int>p;
int k = initialize_k(a);
d = distance(a,b,t);
//cloning distance vector
tmp = d;
//ranking the distance vector from low to high
sort(tmp.begin(),tmp.end());
//only keeping the first k elements
tmp.resize(k);
//finding indices of k shortest distances from original distance vector
for (int i = 0; i < d.size(); i++)
{
for (int j = 0; j < k; j++)
{
if(d[i] == tmp[j])
{
p.push_back(i);
break;
}
}
}
// creating a vector of k shortest distances
for (int i = 0; i < k; i++)
{
label.push_back(c[p[i]]);
}
//applying majority and return the result
return mostFrequent(label);
}
int main()
{
ifstream file("input.txt");
string str,s1;
int s;
//x1 and x2 (2D) input
vector<int>x1;
vector<int>x2;
vector<int>x; //test data
vector<string>y; //classification
//file input
while (getline(file, str))
{
istringstream ss(str);
ss >> s;
x1.push_back(s);
ss >> s;
x2.push_back(s);
ss >> s1;
y.push_back(s1);
}
//test data
x.push_back(3);
x.push_back(7);
s1 = knn_Classify(x1,x2,y,x);
cout<<s1;
}