Machine learning algorithms in C++
Metrics.hpp
Go to the documentation of this file.
1 //
2 // Created by dodo on 31/10/17.
3 //
4 
5 #ifndef MACHINE_LEARNING_METRICS_HPP
6 #define MACHINE_LEARNING_METRICS_HPP
7 
8 #include "../include/matrix/Matrix.hpp"
9 
13 class Metrics {
14 
15  public:
16 
22  static MatrixD minkowski(MatrixD m, double p, bool root = true) {
23  MatrixD distances = MatrixD::zeros(m.nRows(), m.nRows());
24 
25  for (size_t i = 0; i < m.nRows(); i++) {
26  for (size_t j = i + 1; j < m.nRows(); j++) {
27  for (size_t k = 0; k < m.nCols(); k++) {
28  distances(i, j) += pow(abs(m(i, k) - m(j, k)), p);
29  }
30  if (root)
31  distances(i, j) = pow(distances(i, j), 1 / p);
32  }
33  }
34 
35  return distances;
36  }
37 
41  static MatrixD chebyshev(MatrixD a) {
42  MatrixD distances = MatrixD::zeros(a.nRows(), a.nRows());
43 
44  for (size_t i = 0; i < a.nRows(); i++) {
45  for (size_t j = i + 1; j < a.nRows(); j++) {
46  for (size_t k = 0; k < a.nCols(); k++) {
47  double x1 = a(i, k), x2 = a(j, k);
48  double term = abs(x1 - x2);
49  distances(i, j) = distances(j, i) = max(distances(i, j), term);
50  }
51  }
52  }
53 
54  return distances;
55  }
56 
61  static MatrixD euclidean(MatrixD m, bool root = true) {
62  return minkowski(m, 2, root);
63  }
64 
69  static MatrixD manhattan(MatrixD m, bool root = true) {
70  return minkowski(m, 1, root);
71  }
72 
79  static MatrixD minkowski(MatrixD a, MatrixD b, double p, bool root = true) {
80  if (a.nCols() != b.nCols())
81  throw runtime_error("Matrices have different number of dimensions");
82 
83  MatrixD distances = MatrixD::zeros(a.nRows(), b.nRows());
84 
85  for (size_t i = 0; i < a.nRows(); i++) {
86  for (size_t j = 0; j < b.nRows(); j++) {
87  for (size_t k = 0; k < a.nCols(); k++) {
88  double x1 = a(i, k), x2 = b(j, k);
89  double term = pow(abs(x1 - x2), p);
90  distances(i, j) += term;
91  }
92  if (root)
93 
94  distances(i, j) = pow(distances(i, j), 1 / p);
95  }
96  }
97 
98  return distances;
99  }
100 
105  static MatrixD chebyshev(MatrixD a, MatrixD b) {
106  if (a.nCols() != b.nCols())
107  throw runtime_error("Matrices have different number of dimensions");
108 
109  MatrixD distances = MatrixD::zeros(a.nRows(), b.nRows());
110 
111  for (size_t i = 0; i < a.nRows(); i++) {
112  for (size_t j = 0; j < b.nRows(); j++) {
113  for (size_t k = 0; k < a.nCols(); k++) {
114  double x1 = a(i, k), x2 = b(j, k);
115  double term = abs(x1 - x2);
116  distances(i, j) = max(distances(i, j), term);
117  }
118  }
119  }
120 
121  return distances;
122  }
123 
129  static MatrixD euclidean(MatrixD a, MatrixD b, bool root = true) {
130  return minkowski(a, b, 2, root);
131  }
132 
138  static MatrixD manhattan(MatrixD a, MatrixD b, bool root = true) {
139  return minkowski(a, b, 1, root);
140  }
141 };
142 
143 #endif //MACHINE_LEARNING_METRICS_HPP
static MatrixD manhattan(MatrixD m, bool root=true)
Calculates the Manhattan distances between elements in a matrix.
Definition: Metrics.hpp:69
static MatrixD minkowski(MatrixD m, double p, bool root=true)
Calculates the Minkowski distances between elements in a matrix.
Definition: Metrics.hpp:22
static MatrixD euclidean(MatrixD a, MatrixD b, bool root=true)
Calculates the Euclidean distances between elements in two matrices.
Definition: Metrics.hpp:129
static MatrixD chebyshev(MatrixD a)
Calculates the Chebyshev distances between elements in a matrix.
Definition: Metrics.hpp:41
static MatrixD minkowski(MatrixD a, MatrixD b, double p, bool root=true)
Calculates the Minkowski distances between elements in two matrices.
Definition: Metrics.hpp:79
Distance metrics.
Definition: Metrics.hpp:13
static MatrixD euclidean(MatrixD m, bool root=true)
Calculates the Euclidean distances between elements in a matrix.
Definition: Metrics.hpp:61
static MatrixD chebyshev(MatrixD a, MatrixD b)
Calculates the Chebyshev distances between elements in two matrices.
Definition: Metrics.hpp:105
static MatrixD manhattan(MatrixD a, MatrixD b, bool root=true)
Calculates the Manhattan distances between elements in two matrices.
Definition: Metrics.hpp:138