5 #ifndef MACHINE_LEARNING_METRICS_HPP 6 #define MACHINE_LEARNING_METRICS_HPP 8 #include "../include/matrix/Matrix.hpp" 22 static MatrixD
minkowski(MatrixD m,
double p,
bool root =
true) {
23 MatrixD distances = MatrixD::zeros(m.nRows(), m.nRows());
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);
31 distances(i, j) = pow(distances(i, j), 1 / p);
42 MatrixD distances = MatrixD::zeros(a.nRows(), a.nRows());
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);
61 static MatrixD
euclidean(MatrixD m,
bool root =
true) {
69 static MatrixD
manhattan(MatrixD m,
bool root =
true) {
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");
83 MatrixD distances = MatrixD::zeros(a.nRows(), b.nRows());
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;
94 distances(i, j) = pow(distances(i, j), 1 / p);
106 if (a.nCols() != b.nCols())
107 throw runtime_error(
"Matrices have different number of dimensions");
109 MatrixD distances = MatrixD::zeros(a.nRows(), b.nRows());
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);
129 static MatrixD
euclidean(MatrixD a, MatrixD b,
bool root =
true) {
138 static MatrixD
manhattan(MatrixD a, MatrixD b,
bool root =
true) {
143 #endif //MACHINE_LEARNING_METRICS_HPP static MatrixD manhattan(MatrixD m, bool root=true)
Calculates the Manhattan distances between elements in a matrix.
static MatrixD minkowski(MatrixD m, double p, bool root=true)
Calculates the Minkowski distances between elements in a matrix.
static MatrixD euclidean(MatrixD a, MatrixD b, bool root=true)
Calculates the Euclidean distances between elements in two matrices.
static MatrixD chebyshev(MatrixD a)
Calculates the Chebyshev distances between elements in a matrix.
static MatrixD minkowski(MatrixD a, MatrixD b, double p, bool root=true)
Calculates the Minkowski distances between elements in two matrices.
static MatrixD euclidean(MatrixD m, bool root=true)
Calculates the Euclidean distances between elements in a matrix.
static MatrixD chebyshev(MatrixD a, MatrixD b)
Calculates the Chebyshev distances between elements in two matrices.
static MatrixD manhattan(MatrixD a, MatrixD b, bool root=true)
Calculates the Manhattan distances between elements in two matrices.