Classify sample.
103 {
104 std::vector<int> neighbors;
105 std::vector<std::pair<double, int>> distances;
106 for (
size_t i = 0; i < this->
X_.size(); ++i) {
107 auto current = this->
X_.at(i);
108 auto label = this->
Y_.at(i);
110 distances.emplace_back(distance, label);
111 }
112 std::sort(distances.begin(), distances.end());
113 for (
int i = 0; i <
k; i++) {
114 auto label = distances.at(i).second;
115 neighbors.push_back(label);
116 }
117 std::unordered_map<int, int> frequency;
118 for (auto neighbor : neighbors) {
119 ++frequency[neighbor];
120 }
121 std::pair<int, int> predicted;
122 predicted.first = -1;
123 predicted.second = -1;
124 for (auto& kv : frequency) {
125 if (kv.second > predicted.second) {
126 predicted.second = kv.second;
127 predicted.first = kv.first;
128 }
129 }
130 return predicted.first;
131 }
double k(double x)
Another test function.
double euclidean_distance(const std::vector< T > &a, const std::vector< T > &b)
Compute the Euclidean distance between two vectors.