A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d4/d3e/k__nearest__neighbors_8cpp_source.html below:

TheAlgorithms/C++: machine_learning/k_nearest_neighbors.cpp Source File

44

std::vector<double> aux;

45

std::transform(a.begin(), a.end(), b.begin(), std::back_inserter(aux),

46

[](T x1, T x2) { return std::pow((x1 - x2), 2); });

48 return

std::sqrt(std::accumulate(aux.begin(), aux.end(), 0.0));

57

std::vector<std::vector<double>>

X_

{};

58

std::vector<int>

Y_

{};

67 explicit Knn

(std::vector<std::vector<double>>& X, std::vector<int>& Y)

103 int predict

(std::vector<double>& sample,

int

k) {

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);

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);

117

std::unordered_map<int, int> frequency;

118 for

(

auto

neighbor : neighbors) {

119

++frequency[neighbor];

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;

130 return

predicted.first;

141

std::cout <<

"------- Test 1 -------"

<< std::endl;

142

std::vector<std::vector<double>> X1 = {{0.0, 0.0}, {0.25, 0.25},

143

{0.0, 0.5}, {0.5, 0.5},

144

{1.0, 0.5}, {1.0, 1.0}};

145

std::vector<int> Y1 = {1, 1, 1, 1, 2, 2};

147

std::vector<double> sample1 = {1.2, 1.2};

148

std::vector<double> sample2 = {0.1, 0.1};

149

std::vector<double> sample3 = {0.1, 0.5};

150

std::vector<double> sample4 = {1.0, 0.75};

151

assert(model1.predict(sample1, 2) == 2);

152

assert(model1.predict(sample2, 2) == 1);

153

assert(model1.predict(sample3, 2) == 1);

154

assert(model1.predict(sample4, 2) == 2);

155

std::cout <<

"... Passed"

<< std::endl;

156

std::cout <<

"------- Test 2 -------"

<< std::endl;

157

std::vector<std::vector<double>> X2 = {

158

{0.0, 0.0, 0.0}, {0.25, 0.25, 0.0}, {0.0, 0.5, 0.0}, {0.5, 0.5, 0.0},

159

{1.0, 0.5, 0.0}, {1.0, 1.0, 0.0}, {1.0, 1.0, 1.0}, {1.5, 1.5, 1.0}};

160

std::vector<int> Y2 = {1, 1, 1, 1, 2, 2, 3, 3};

162

std::vector<double> sample5 = {1.2, 1.2, 0.0};

163

std::vector<double> sample6 = {0.1, 0.1, 0.0};

164

std::vector<double> sample7 = {0.1, 0.5, 0.0};

165

std::vector<double> sample8 = {1.0, 0.75, 1.0};

166

assert(model2.predict(sample5, 2) == 2);

167

assert(model2.predict(sample6, 2) == 1);

168

assert(model2.predict(sample7, 2) == 1);

169

assert(model2.predict(sample8, 2) == 3);

170

std::cout <<

"... Passed"

<< std::endl;

171

std::cout <<

"------- Test 3 -------"

<< std::endl;

172

std::vector<std::vector<double>> X3 = {{0.0}, {1.0}, {2.0}, {3.0},

173

{4.0}, {5.0}, {6.0}, {7.0}};

174

std::vector<int> Y3 = {1, 1, 1, 1, 2, 2, 2, 2};

176

std::vector<double> sample9 = {0.5};

177

std::vector<double> sample10 = {2.9};

178

std::vector<double> sample11 = {5.5};

179

std::vector<double> sample12 = {7.5};

180

assert(model3.predict(sample9, 3) == 1);

181

assert(model3.predict(sample10, 3) == 1);

182

assert(model3.predict(sample11, 3) == 2);

183

assert(model3.predict(sample12, 3) == 2);

184

std::cout <<

"... Passed"

<< std::endl;


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4