From 4900b40a7428bc86b9119e78884c01390de26e87 Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Tue, 7 Feb 2023 19:22:35 +0530 Subject: [PATCH 01/19] Resolved Issue #64 --- src/slowmokit/ducks/matrix/matrix.cpp | 112 +++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 10 deletions(-) diff --git a/src/slowmokit/ducks/matrix/matrix.cpp b/src/slowmokit/ducks/matrix/matrix.cpp index e7f5588..8c588e4 100644 --- a/src/slowmokit/ducks/matrix/matrix.cpp +++ b/src/slowmokit/ducks/matrix/matrix.cpp @@ -36,7 +36,7 @@ Matrix::Matrix(const std::vector> in) } template -Matrix &Matrix::operator *=(const T &scalar) +Matrix &Matrix::operator*=(const T &scalar) { for (int i = 0; i < n; i++) { @@ -48,7 +48,7 @@ Matrix &Matrix::operator *=(const T &scalar) } template -Matrix &Matrix::operator *=(const Matrix &rhs) +Matrix &Matrix::operator*=(const Matrix &rhs) { auto [n2, m2] = rhs.getShape(); @@ -77,7 +77,7 @@ Matrix &Matrix::operator *=(const Matrix &rhs) } template -Matrix &Matrix::operator +=(const Matrix &rhs) +Matrix &Matrix::operator+=(const Matrix &rhs) { auto [n2, m2] = rhs.getShape(); @@ -97,7 +97,7 @@ Matrix &Matrix::operator +=(const Matrix &rhs) } template -Matrix &Matrix::operator -=(const Matrix &rhs) +Matrix &Matrix::operator-=(const Matrix &rhs) { auto [n2, m2] = rhs.getShape(); @@ -119,22 +119,22 @@ Matrix &Matrix::operator -=(const Matrix &rhs) template std::array Matrix::getShape() const { - return { this->n, this->m }; + return {this->n, this->m}; } template -T &Matrix::operator() (int i, int j) +T &Matrix::operator()(int i, int j) { if (i >= n or i < 0) throw std::out_of_range("\ni should be between 0 and " + std::to_string(n - 1) + " inclusive"); if (j >= m or j < 0) - throw std::out_of_range("\nj should be between 0 and " + std::to_string(m - 1) + " inclusive"); + throw std::out_of_range("\nj should be between 0 and " + std::to_string(m - 1) + " inclusive"); return mat[i][j]; } template -const std::vector &Matrix::operator[] (int i) const +const std::vector &Matrix::operator[](int i) const { if (i >= n or i < 0) throw std::out_of_range("\ni should be between 0 and " + std::to_string(n - 1) + " inclusive"); @@ -143,10 +143,10 @@ const std::vector &Matrix::operator[] (int i) const } template -std::ostream& operator<<(std::ostream &os, const Matrix &matrix) +std::ostream &operator<<(std::ostream &os, const Matrix &matrix) { int n = std::size(matrix); - int m = std::size(matrix[0]); + int m = std::size(matrix[0]); for (int i = 0; i < n; i++) { @@ -184,3 +184,95 @@ Matrix operator-(Matrix lhs, const Matrix &rhs) lhs -= rhs; return lhs; } + +template +Matrix operator+(G num, const Matrix &matrix) +{ + int n = std::size(matrix); + int m = std::size(matrix[0]); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + matrix[i][j] += num; + } + } + return matrix; +} + +template +Matrix operator-(G num, const Matrix &matrix) +{ + int n = std::size(matrix); + int m = std::size(matrix[0]); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + matrix[i][j] -= num; + } + } + return matrix; +} + +template +Matrix operator*(G num, const Matrix &matrix) +{ + int n = std::size(matrix); + int m = std::size(matrix[0]); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + matrix[i][j] *= num; + } + } + return matrix; +} + + +template +Matrix operator+(const Matrix &matrix, G num) +{ + int n = std::size(matrix); + int m = std::size(matrix[0]); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + matrix[i][j] += num; + } + } + return matrix; +} + +template +Matrix operator-(const Matrix &matrix, G num) +{ + int n = std::size(matrix); + int m = std::size(matrix[0]); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + matrix[i][j] -= num; + } + } + return matrix; +} + + +template +Matrix operator*(const Matrix &matrix, G num) +{ + int n = std::size(matrix); + int m = std::size(matrix[0]); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + matrix[i][j] *= num; + } + } + return matrix; +} \ No newline at end of file From 341ebc0bf67288dfc02b7d9e260c6490a4a233d2 Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Tue, 7 Feb 2023 22:46:57 +0530 Subject: [PATCH 02/19] Fixed All Operators Issue#64 --- .gitignore | 5 +- src/slowmokit/ducks/matrix/matrix.cpp | 115 +++++++++++--------------- src/slowmokit/ducks/matrix/matrix.hpp | 54 ++++++++---- 3 files changed, 88 insertions(+), 86 deletions(-) diff --git a/.gitignore b/.gitignore index 1c43e46..6f6f80f 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,7 @@ cmake-build-debug # CHelper -**/*__tests \ No newline at end of file +**/*__tests + +# Binary Executive +**/*.bin \ No newline at end of file diff --git a/src/slowmokit/ducks/matrix/matrix.cpp b/src/slowmokit/ducks/matrix/matrix.cpp index 8c588e4..0bfe681 100644 --- a/src/slowmokit/ducks/matrix/matrix.cpp +++ b/src/slowmokit/ducks/matrix/matrix.cpp @@ -36,7 +36,8 @@ Matrix::Matrix(const std::vector> in) } template -Matrix &Matrix::operator*=(const T &scalar) +template +Matrix &Matrix::operator*=(const G &scalar) { for (int i = 0; i < n; i++) { @@ -47,6 +48,32 @@ Matrix &Matrix::operator*=(const T &scalar) return *this; } +template +template +Matrix &Matrix::operator+=(const G &scalar) +{ + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + mat[i][j] += scalar; + } + + return *this; +} + +template +template +Matrix &Matrix::operator-=(const G &scalar) +{ + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + mat[i][j] -= scalar; + } + + return *this; +} + template Matrix &Matrix::operator*=(const Matrix &rhs) { @@ -145,8 +172,7 @@ const std::vector &Matrix::operator[](int i) const template std::ostream &operator<<(std::ostream &os, const Matrix &matrix) { - int n = std::size(matrix); - int m = std::size(matrix[0]); + auto [n, m] = matrix.getShape(); for (int i = 0; i < n; i++) { @@ -185,94 +211,49 @@ Matrix operator-(Matrix lhs, const Matrix &rhs) return lhs; } -template +template Matrix operator+(G num, const Matrix &matrix) { - int n = std::size(matrix); - int m = std::size(matrix[0]); - for (int i = 0; i < n; i++) - { - for (int j = 0; j < m; j++) - { - matrix[i][j] += num; - } - } - return matrix; + Matrix res = matrix; + res += num; + return res; } -template +template Matrix operator-(G num, const Matrix &matrix) { - int n = std::size(matrix); - int m = std::size(matrix[0]); - for (int i = 0; i < n; i++) - { - for (int j = 0; j < m; j++) - { - matrix[i][j] -= num; - } - } - return matrix; + Matrix res = matrix; + res -= num; + return res; } -template +template Matrix operator*(G num, const Matrix &matrix) { - int n = std::size(matrix); - int m = std::size(matrix[0]); - for (int i = 0; i < n; i++) - { - for (int j = 0; j < m; j++) - { - matrix[i][j] *= num; - } - } - return matrix; + Matrix res = matrix; + res *= num; + return res; } template -Matrix operator+(const Matrix &matrix, G num) +Matrix operator+(Matrix matrix, const G &num) { - int n = std::size(matrix); - int m = std::size(matrix[0]); - for (int i = 0; i < n; i++) - { - for (int j = 0; j < m; j++) - { - matrix[i][j] += num; - } - } + matrix += num; return matrix; } template -Matrix operator-(const Matrix &matrix, G num) +Matrix operator-(Matrix matrix, const G &num) { - int n = std::size(matrix); - int m = std::size(matrix[0]); - for (int i = 0; i < n; i++) - { - for (int j = 0; j < m; j++) - { - matrix[i][j] -= num; - } - } + matrix -= num; return matrix; } template -Matrix operator*(const Matrix &matrix, G num) +Matrix operator*(Matrix matrix, const G &num) { - int n = std::size(matrix); - int m = std::size(matrix[0]); - for (int i = 0; i < n; i++) - { - for (int j = 0; j < m; j++) - { - matrix[i][j] *= num; - } - } + matrix *= num; return matrix; -} \ No newline at end of file +} diff --git a/src/slowmokit/ducks/matrix/matrix.hpp b/src/slowmokit/ducks/matrix/matrix.hpp index 177ba57..eeb6ce5 100644 --- a/src/slowmokit/ducks/matrix/matrix.hpp +++ b/src/slowmokit/ducks/matrix/matrix.hpp @@ -55,7 +55,8 @@ class Matrix * @param scalar: Number with which you want to multiply matrix with * @returns: Matrix after multiplying current matrix with scalar */ - Matrix &operator *=(const T &); + template + Matrix &operator *=(const G &); /** @@ -68,6 +69,24 @@ class Matrix Matrix &operator *=(const Matrix &); + /** + * @brief Overloading += to add a matrix with a scalar + * @param scalar: Number with which you want to add matrix with + * @returns: Matrix after adding current matrix with scalar + */ + template + Matrix &operator +=(const G &); + + + /** + * @brief Overloading -= to subtract a matrix with a scalar + * @param scalar: Number with which you want to subtract matrix with + * @returns: Matrix after subtracting scalar from current matrix + */ + template + Matrix &operator -=(const G &); + + /** * @brief function to get dimension of the matrix */ @@ -82,10 +101,6 @@ class Matrix */ Matrix &dot(const Matrix &); - /** - * @brief - */ - /** * @brief overloading += operator for adding another matrix to existing matrix @@ -119,21 +134,24 @@ class Matrix */ const std::vector &operator[] (int) const; - /** - * @brief overloading << for easy printing of Matrix - */ - friend std::ostream& operator<<(std::ostream &, const Matrix &); }; + +/** + * @brief overloading << for easy printing of Matrix + */ +template +std::ostream& operator<<(std::ostream &, const Matrix &); + /** * @brief Free Function to multiply a matrix to a number or another matrix * @param lhs: A number or a Matrix * @param rhs: A number (only if lhs is not a number) or a Matrix * @returns Matrix Object */ -template Matrix operator*(T, const Matrix &); -template Matrix operator*(Matrix, const T &); -template Matrix operator*(Matrix lhs, const Matrix &rhs); +template Matrix operator*(G, const Matrix &); +template Matrix operator*(Matrix, const G &); +template Matrix operator*(Matrix, const Matrix &); /** * @brief Free Function to add a matrix to a number or another matrix @@ -141,9 +159,9 @@ template Matrix operator*(Matrix lhs, const Matrix &rhs); * @param rhs: A number (only if lhs is not a number) or a Matrix * @returns Matrix Object */ -template Matrix operator+(T, const Matrix &); -template Matrix operator+(Matrix, const T &); -template Matrix operator+(Matrix lhs, const Matrix &rhs); +template Matrix operator+(G, const Matrix &); +template Matrix operator+(Matrix, const G &); +template Matrix operator+(Matrix, const Matrix &); /** * @brief Free Function to subtract a matrix to a number or another matrix @@ -151,9 +169,9 @@ template Matrix operator+(Matrix lhs, const Matrix &rhs); * @param rhs: A number (only if lhs is not a number) or a Matrix * @returns Matrix Object */ -template Matrix operator-(T, const Matrix &); -template Matrix operator-(Matrix, const T &); -template Matrix operator-(Matrix lhs, const Matrix &rhs); +template Matrix operator-(G, const Matrix &); +template Matrix operator-(Matrix, const G &); +template Matrix operator-(Matrix, const Matrix &); #endif //SLOWMOKIT_IO_HPP From 16031e49efa80167c6d0e127d89d9eed6aa44b80 Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Tue, 7 Feb 2023 23:34:46 +0530 Subject: [PATCH 03/19] Resolved Issue#81 --- src/slowmokit/ducks/matrix/matrix.cpp | 26 ++++++++++++++++++++++++-- src/slowmokit/ducks/matrix/matrix.hpp | 23 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/slowmokit/ducks/matrix/matrix.cpp b/src/slowmokit/ducks/matrix/matrix.cpp index 0bfe681..0934837 100644 --- a/src/slowmokit/ducks/matrix/matrix.cpp +++ b/src/slowmokit/ducks/matrix/matrix.cpp @@ -235,7 +235,6 @@ Matrix operator*(G num, const Matrix &matrix) return res; } - template Matrix operator+(Matrix matrix, const G &num) { @@ -250,10 +249,33 @@ Matrix operator-(Matrix matrix, const G &num) return matrix; } - template Matrix operator*(Matrix matrix, const G &num) { matrix *= num; return matrix; } + +template +Matrix Matrix::matmul(const Matrix rhs) +{ + Matrix res = *this; + res *= rhs; + return res; +} + +template +Matrix Matrix::add(const Matrix rhs) +{ + Matrix res = *this; + res += rhs; + return res; +} + +template +Matrix Matrix::subtract(const Matrix rhs) +{ + Matrix res = *this; + res -= rhs; + return res; +} diff --git a/src/slowmokit/ducks/matrix/matrix.hpp b/src/slowmokit/ducks/matrix/matrix.hpp index eeb6ce5..c59906b 100644 --- a/src/slowmokit/ducks/matrix/matrix.hpp +++ b/src/slowmokit/ducks/matrix/matrix.hpp @@ -134,6 +134,29 @@ class Matrix */ const std::vector &operator[] (int) const; + + /** + * @brief This function will multiply two matrix + * @param rhs: This is the matrix which will be multiplied with the main matrix + * @throw: whatever operator *= throws + */ + Matrix matmul(const Matrix); + + + /** + * @brief This function will add 2 matrix + * @param rhs: This is the matrix which will be added with the main matrix + * @throw: whatever operator *= throws + */ + Matrix add(const Matrix); + + + /** + * @brief This function will subtract 2 matrix + * @param rhs: This is the matrix which will be subtracted from the main matrix + * @throw: whatever operator *= throws + */ + Matrix subtract(const Matrix); }; From 3fb7402d4e33cc044d5bfe6e51f3235c8c551547 Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Tue, 7 Feb 2023 23:53:47 +0530 Subject: [PATCH 04/19] Revert "Resolved Issue#81" This reverts commit 16031e49efa80167c6d0e127d89d9eed6aa44b80. --- src/slowmokit/ducks/matrix/matrix.cpp | 26 ++------------------------ src/slowmokit/ducks/matrix/matrix.hpp | 23 ----------------------- 2 files changed, 2 insertions(+), 47 deletions(-) diff --git a/src/slowmokit/ducks/matrix/matrix.cpp b/src/slowmokit/ducks/matrix/matrix.cpp index 0934837..0bfe681 100644 --- a/src/slowmokit/ducks/matrix/matrix.cpp +++ b/src/slowmokit/ducks/matrix/matrix.cpp @@ -235,6 +235,7 @@ Matrix operator*(G num, const Matrix &matrix) return res; } + template Matrix operator+(Matrix matrix, const G &num) { @@ -249,33 +250,10 @@ Matrix operator-(Matrix matrix, const G &num) return matrix; } + template Matrix operator*(Matrix matrix, const G &num) { matrix *= num; return matrix; } - -template -Matrix Matrix::matmul(const Matrix rhs) -{ - Matrix res = *this; - res *= rhs; - return res; -} - -template -Matrix Matrix::add(const Matrix rhs) -{ - Matrix res = *this; - res += rhs; - return res; -} - -template -Matrix Matrix::subtract(const Matrix rhs) -{ - Matrix res = *this; - res -= rhs; - return res; -} diff --git a/src/slowmokit/ducks/matrix/matrix.hpp b/src/slowmokit/ducks/matrix/matrix.hpp index c59906b..eeb6ce5 100644 --- a/src/slowmokit/ducks/matrix/matrix.hpp +++ b/src/slowmokit/ducks/matrix/matrix.hpp @@ -134,29 +134,6 @@ class Matrix */ const std::vector &operator[] (int) const; - - /** - * @brief This function will multiply two matrix - * @param rhs: This is the matrix which will be multiplied with the main matrix - * @throw: whatever operator *= throws - */ - Matrix matmul(const Matrix); - - - /** - * @brief This function will add 2 matrix - * @param rhs: This is the matrix which will be added with the main matrix - * @throw: whatever operator *= throws - */ - Matrix add(const Matrix); - - - /** - * @brief This function will subtract 2 matrix - * @param rhs: This is the matrix which will be subtracted from the main matrix - * @throw: whatever operator *= throws - */ - Matrix subtract(const Matrix); }; From 019f9f252f7c23cf6b51f0283d42e1bf00a1c590 Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Wed, 8 Feb 2023 00:18:26 +0530 Subject: [PATCH 05/19] Resolved Issue #81 --- src/slowmokit/ducks/matrix/matrix.cpp | 25 +++++++++++++++++++++++++ src/slowmokit/ducks/matrix/matrix.hpp | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/slowmokit/ducks/matrix/matrix.cpp b/src/slowmokit/ducks/matrix/matrix.cpp index 0bfe681..7e79a53 100644 --- a/src/slowmokit/ducks/matrix/matrix.cpp +++ b/src/slowmokit/ducks/matrix/matrix.cpp @@ -257,3 +257,28 @@ Matrix operator*(Matrix matrix, const G &num) matrix *= num; return matrix; } + + +template +Matrix Matrix::matmul(const Matrix rhs) +{ + Matrix res = *this; + res *= rhs; + return res; +} + +template +Matrix Matrix::add(const Matrix rhs) +{ + Matrix res = *this; + res += rhs; + return res; +} + +template +Matrix Matrix::subtract(const Matrix rhs) +{ + Matrix res = *this; + res -= rhs; + return res; +} \ No newline at end of file diff --git a/src/slowmokit/ducks/matrix/matrix.hpp b/src/slowmokit/ducks/matrix/matrix.hpp index eeb6ce5..ffa874e 100644 --- a/src/slowmokit/ducks/matrix/matrix.hpp +++ b/src/slowmokit/ducks/matrix/matrix.hpp @@ -134,6 +134,30 @@ class Matrix */ const std::vector &operator[] (int) const; + + /** + * @brief This function will multiply two matrix + * @param rhs: This is the matrix which will be multiplied with the main matrix + * @throw: whatever operator *= throws + */ + Matrix matmul(const Matrix); + + + /** + * @brief This function will add 2 matrix + * @param rhs: This is the matrix which will be added with the main matrix + * @throw: whatever operator *= throws + */ + Matrix add(const Matrix); + + + /** + * @brief This function will subtract 2 matrix + * @param rhs: This is the matrix which will be subtracted from the main matrix + * @throw: whatever operator *= throws + */ + Matrix subtract(const Matrix); + }; From 3512f9bc294a7711af4a9c408e75e3571ced0c2c Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Fri, 10 Feb 2023 21:48:17 +0530 Subject: [PATCH 06/19] F1 Score #72 --- .gitignore | 3 ++ CMakeLists.txt | 4 ++- examples/metrics/f1score_eg.cpp | 11 +++++++ src/slowmokit/ducks/matrix/matrix.cpp | 2 +- src/slowmokit/methods/metrics/f1score.cpp | 38 +++++++++++++++++++++++ src/slowmokit/methods/metrics/f1score.hpp | 21 +++++++++++++ 6 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 examples/metrics/f1score_eg.cpp create mode 100644 src/slowmokit/methods/metrics/f1score.cpp create mode 100644 src/slowmokit/methods/metrics/f1score.hpp diff --git a/.gitignore b/.gitignore index 4ed0d3d..4f98a19 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,6 @@ cmake-build-debug # build build + +#cph extension +.cph/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 62bfc4b..c30d666 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,4 +55,6 @@ add_library(slowmokit src/slowmokit/methods/metrics/precision.hpp src/slowmokit/methods/metrics/precision.cpp src/slowmokit/methods/metrics/recall.hpp - src/slowmokit/methods/metrics/recall.cpp) + src/slowmokit/methods/metrics/recall.cpp + src/slowmokit/methods/metrics/f1score.hpp + src/slowmokit/methods/metrics/f1score.cpp) diff --git a/examples/metrics/f1score_eg.cpp b/examples/metrics/f1score_eg.cpp new file mode 100644 index 0000000..9875c95 --- /dev/null +++ b/examples/metrics/f1score_eg.cpp @@ -0,0 +1,11 @@ +// #include "../src/slowmokit/methods/metrics/f1score.hpp" +// int main() +// { +// std::vector pred = {0, 1, 2, 1, 0, 2, 1, 0, 1, 2}; +// std::vector actual = {0, 0, 2, 1, 0, 2, 1, 0, 1, 2}; +// std::map f1ScoreEg = f1Score(pred, actual); +// for(int i = 0; i < f1ScoreEg.size(); i++){ +// std::cout << "Class " << i << " F1_Score: " << f1ScoreEg[i] << std::endl; +// } +// return 0; +// } \ No newline at end of file diff --git a/src/slowmokit/ducks/matrix/matrix.cpp b/src/slowmokit/ducks/matrix/matrix.cpp index 1f02f8d..0e63bc2 100644 --- a/src/slowmokit/ducks/matrix/matrix.cpp +++ b/src/slowmokit/ducks/matrix/matrix.cpp @@ -264,4 +264,4 @@ template Matrix Matrix::subtract(const Matrix rhs) Matrix res = *this; res -= rhs; return res; -} \ No newline at end of file +} diff --git a/src/slowmokit/methods/metrics/f1score.cpp b/src/slowmokit/methods/metrics/f1score.cpp new file mode 100644 index 0000000..d687eb4 --- /dev/null +++ b/src/slowmokit/methods/metrics/f1score.cpp @@ -0,0 +1,38 @@ +/** + * @file methods/metrics/f1score.cpp + * + * Implementation of the f1Score main program + */ +#include "f1score.hpp" +#include "precision.cpp" +#include "recall.cpp" + +template +std::map f1Score(std::vector &pred, std::vector &actual) +{ + // 2 * Precision * Recall / (Precision + Recall) + std::map precisionMap, recallMap; + precisionMap = precision(pred, actual); + recallMap = recall(pred, actual); + std::map f1ScoreMap; + for (int i = 0; i < precisionMap.size(); i++) + { + T classNumber = i; + if (precisionMap[classNumber] == 0 || recallMap[classNumber] == 0) + { + f1ScoreMap[classNumber] = 0; + } + else + { + f1ScoreMap[classNumber] = (2 * (double) precisionMap[classNumber] * + (double) recallMap[classNumber]) / + ((double) precisionMap[classNumber] + + (double) recallMap[classNumber]); + + double x = f1ScoreMap[classNumber]; + float value = (int) (x * 100 + .5); + f1ScoreMap[classNumber] = (float) value / 100; + } + } + return f1ScoreMap; +} diff --git a/src/slowmokit/methods/metrics/f1score.hpp b/src/slowmokit/methods/metrics/f1score.hpp new file mode 100644 index 0000000..edd7e1f --- /dev/null +++ b/src/slowmokit/methods/metrics/f1score.hpp @@ -0,0 +1,21 @@ +/** + * @file methods/metrics/f1score.hpp + * + * Easy include to find f1score + */ + +#ifndef SLOWMOKIT_F1SCORE_HPP +#define SLOWMOKIT_F1SCORE_HPP +#include "../../core.hpp" + +/** + * Takes predicted and actual values + * @param pred -> predicted values + * @param trueLabels -> true values + * @returns f1score score + */ + +template +std::map f1Score(std::vector &, std::vector &); + +#endif // SLOWMOKIT_F1SCORE_HPP From 93d1b7aef59f49be73adce12fcd18a90c9a743c9 Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Fri, 10 Feb 2023 21:55:14 +0530 Subject: [PATCH 07/19] Resolved #72 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 78b07dd..8209dc4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,6 +60,6 @@ add_library(slowmokit src/slowmokit/methods/metrics/recall.hpp src/slowmokit/methods/metrics/recall.cpp src/slowmokit/methods/metrics/f1score.hpp - src/slowmokit/methods/metrics/f1score.cpp) + src/slowmokit/methods/metrics/f1score.cpp src/slowmokit/methods/metrics/mean_squared_error.hpp src/slowmokit/methods/metrics/mean_squared_error.cpp) \ No newline at end of file From 0d73928fd2891a00bc82b8b975bcd0fdb23216c8 Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Fri, 10 Feb 2023 21:59:52 +0530 Subject: [PATCH 08/19] Formatted Code --- src/slowmokit/methods/metrics/f1score.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/slowmokit/methods/metrics/f1score.hpp b/src/slowmokit/methods/metrics/f1score.hpp index edd7e1f..12bb54a 100644 --- a/src/slowmokit/methods/metrics/f1score.hpp +++ b/src/slowmokit/methods/metrics/f1score.hpp @@ -13,6 +13,8 @@ * @param pred -> predicted values * @param trueLabels -> true values * @returns f1score score + * @throws exception invalid_argument in case size of the two vectors is not + * equal */ template From c3fb7c9da58a995ca8e0e4f147864ed04287b2c2 Mon Sep 17 00:00:00 2001 From: Modernbeast02 <89138051+Modernbeast02@users.noreply.github.com> Date: Fri, 10 Feb 2023 22:17:36 +0530 Subject: [PATCH 09/19] f1Score.md --- docs/methods/metrics/f1Score.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 docs/methods/metrics/f1Score.md diff --git a/docs/methods/metrics/f1Score.md b/docs/methods/metrics/f1Score.md new file mode 100644 index 0000000..663e6ad --- /dev/null +++ b/docs/methods/metrics/f1Score.md @@ -0,0 +1,31 @@ +# F1 Score + +The F1-score combines the precision and recall of a classifier into a single metric by taking their harmonic mean. It is primarily used to compare the performance of two classifiers. + + +## Parameters + +| Name | Definition | Type | +| ------------- | ------------------------------------------------------------------------------------------- | ----------------| +| Pred | Takes a vector of predicted values | `T,double` | +| Actual | Takes a vector of actual values | `T,double` | + + +## Methods + +| Name | Definition | Return value | +| -----------------------------------------------| ----------------------------------------------------- | ----------------- | +| `f1Score(vector &x, vector &y)` | To find the f1 Score | `map` | + +## Example + +```cpp +std::vector pred = {0, 1, 2, 1, 0, 2, 1, 0, 1, 2}; +std::vector actual = {0, 0, 2, 1, 0, 2, 1, 0, 1, 2}; +std::map f1ScoreEg = f1Score(pred, actual); +for(int i = 0; i < f1ScoreEg.size(); i++){ + std::cout << "Class " << i << " F1_Score: " << f1ScoreEg[i] << std::endl; +} + +``` +Screenshot 2023-02-10 at 10 14 46 PM From 004c5c263bf16156a7615c37019ba4f11ea21e2a Mon Sep 17 00:00:00 2001 From: Modernbeast02 <89138051+Modernbeast02@users.noreply.github.com> Date: Sat, 11 Feb 2023 17:52:11 +0530 Subject: [PATCH 10/19] Update f1Score.md --- docs/methods/metrics/f1Score.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/methods/metrics/f1Score.md b/docs/methods/metrics/f1Score.md index 663e6ad..1524e15 100644 --- a/docs/methods/metrics/f1Score.md +++ b/docs/methods/metrics/f1Score.md @@ -7,8 +7,8 @@ The F1-score combines the precision and recall of a classifier into a single met | Name | Definition | Type | | ------------- | ------------------------------------------------------------------------------------------- | ----------------| -| Pred | Takes a vector of predicted values | `T,double` | -| Actual | Takes a vector of actual values | `T,double` | +| Pred | Takes a vector of predicted values | `int,double` | +| Actual | Takes a vector of actual values | `int,double` | ## Methods From 8d194541778ddaa7e7a33af73009c6433924808f Mon Sep 17 00:00:00 2001 From: Modernbeast02 <89138051+Modernbeast02@users.noreply.github.com> Date: Sat, 11 Feb 2023 18:01:44 +0530 Subject: [PATCH 11/19] Update f1Score.md --- docs/methods/metrics/f1Score.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/methods/metrics/f1Score.md b/docs/methods/metrics/f1Score.md index 1524e15..a12f9b4 100644 --- a/docs/methods/metrics/f1Score.md +++ b/docs/methods/metrics/f1Score.md @@ -2,6 +2,11 @@ The F1-score combines the precision and recall of a classifier into a single metric by taking their harmonic mean. It is primarily used to compare the performance of two classifiers. +Some advantages of F1-score: +1)Very small precision or recall will result in lower overall score. Thus it helps balance the two metrics. +2)If you choose your positive class as the one with fewer samples, F1-score can help balance the metric across positive/negative samples. + + ## Parameters From 2e4d1102578547ea9763d8126a1f1839af3df01e Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Tue, 14 Feb 2023 19:30:27 +0530 Subject: [PATCH 12/19] Implemented Distance Metric --- CMakeLists.txt | 4 +- examples/neighbors/distance_metric_eg.cpp | 18 ++++ .../distance_metric/distance_metric.cpp | 89 +++++++++++++++++++ .../distance_metric/distance_metric.hpp | 69 ++++++++++++++ 4 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 examples/neighbors/distance_metric_eg.cpp create mode 100644 src/slowmokit/methods/neighbors/distance_metric/distance_metric.cpp create mode 100644 src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8209dc4..09232fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,4 +62,6 @@ add_library(slowmokit src/slowmokit/methods/metrics/f1score.hpp src/slowmokit/methods/metrics/f1score.cpp src/slowmokit/methods/metrics/mean_squared_error.hpp - src/slowmokit/methods/metrics/mean_squared_error.cpp) \ No newline at end of file + src/slowmokit/methods/metrics/mean_squared_error.cpp + src/slowmokit/methods/neighbors/distance_metric/distance_metric.cpp + src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp) \ No newline at end of file diff --git a/examples/neighbors/distance_metric_eg.cpp b/examples/neighbors/distance_metric_eg.cpp new file mode 100644 index 0000000..32a6812 --- /dev/null +++ b/examples/neighbors/distance_metric_eg.cpp @@ -0,0 +1,18 @@ +// #include "../src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp" + + +// int main() +// { +// std::vector dist1 = {1, 4, 4, 4}; +// std::vector dist2 = {1, 2, 3, 4}; +// DistanceMetric Dist(dist1, dist2); +// std::cout << "Minkowski Distance is " << Dist.minkowski(dist1, dist2, 3) +// << std::endl; +// std::cout << "Euclidean Distance is " << Dist.euclidean(dist1, dist2) +// << std::endl; +// std::cout << "Manhattan Distance is " << Dist.manhattan(dist1, dist2) +// << std::endl; +// std::cout << "Cosine Similarity is " << Dist.cosineSimilarity(dist1, dist2) +// << std::endl; +// return 0; +// } \ No newline at end of file diff --git a/src/slowmokit/methods/neighbors/distance_metric/distance_metric.cpp b/src/slowmokit/methods/neighbors/distance_metric/distance_metric.cpp new file mode 100644 index 0000000..7949953 --- /dev/null +++ b/src/slowmokit/methods/neighbors/distance_metric/distance_metric.cpp @@ -0,0 +1,89 @@ +/** + * @file methods/neighbors/distance_metric/distance_metric.hpp + * + * Easy include to calculate distance metrics + */ + +#include "distance_metric.hpp" + +template +DistanceMetric::DistanceMetric(std::vector &x, std::vector &t) +{ + this->x = x; + this->y = y; +} + +template +double DistanceMetric::euclidean(std::vector &x, std::vector &y) +{ + if (x.size() != y.size()) + { + throw std::domain_error("Size of the two vectors must be same"); + } + double distance = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + distance += (x[i] - y[i]) * (x[i] - y[i]); + } + return sqrt(distance); +} + +template +double DistanceMetric::manhattan(std::vector &x, std::vector &y) +{ + if (x.size() != y.size()) + { + throw std::domain_error("Size of the two vectors must be same"); + } + double distance = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + distance += abs(x[i] - y[i]); + } + return distance; +} +template +double DistanceMetric::minkowski(std::vector &x, std::vector &y, + int power) +{ + if (x.size() != y.size()) + { + throw std::domain_error("Size of the two vectors must be same"); + } + double distance = 0.0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + distance += pow(x[i] - y[i], power); + } + return pow(distance, 1.0 / power); +} + +template double DistanceMetric::magnitude(std::vector &x) +{ + double result = 0.0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + result += pow(x[i], 2); + } + return sqrt(result); +} + +template +double DistanceMetric::cosineSimilarity(std::vector &x, std::vector &y) +{ + if (x.size() != y.size()) + { + throw std::domain_error("Size of the two vectors must be same"); + } + double dotProduct = 0.0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + dotProduct += x[i] * y[i]; + } + return dotProduct / (magnitude(x) * magnitude(y)); +} diff --git a/src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp b/src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp new file mode 100644 index 0000000..c847f49 --- /dev/null +++ b/src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp @@ -0,0 +1,69 @@ +/** + * @file methods/neighbors/distance_metric/distance_metric.hpp + * + * Easy include to calculate distances + */ + +#ifndef SLOWMOKIT_DISTANCE_METRIC_HPP +#define SLOWMOKIT_DISTANCE_METRIC_HPP +#include "../../../core.hpp" + +/** + * Takes predicted and actual values of classes + * @param x + * @param y + * @returns the distance metrics + * @throws domain_error exception when size of the two vectors is not equal + */ +template class DistanceMetric +{ + private: + std::vector x; + std::vector y; + + public: + DistanceMetric(std::vector &x, std::vector &y); + + /** + * @param x An array where each row is a sample and each column is a feature. + * @param y An array where each row is a sample and each column is a feature. + * @throws domain_error exception when size of the two vectors is not equal + * @returns euclidean distance between the two vectors + */ + double euclidean(std::vector &, std::vector &); + + + /** + *@param x An array where each row is a sample and each column is a feature. + * @param y An array where each row is a sample and each column is a feature. + * @throws domain_error exception when size of the two vectors is not equal + * @returns manhattan distance between the two vectors + */ + double manhattan(std::vector &, std::vector &); + + + /** + * @param x An array where each row is a sample and each column is a feature. + * @param y An array where each row is a sample and each column is a feature. + * @throws domain_error exception when size of the two vectors is not equal + * @returns minkowski distance between the two vectors + */ + double minkowski(std::vector &, std::vector &, int); + + /** + * @brief to find the magnitude of the vector + * @param x a vector + * @returns magnitude of x + */ + double magnitude(std::vector &); + + /** + * @param x An array where each row is a sample and each column is a feature. + * @param y An array where each row is a sample and each column is a feature. + * @throws domain_error exception when size of the two vectors is not equal + * @returns cosine similarity between the two vectors + */ + double cosineSimilarity(std::vector &, std::vector &); +}; + +#endif // SLOWMOKIT_DISTANCE_METRIC_HPP \ No newline at end of file From 6601058c1a2a10d91bb6392b0e4f123a71547a51 Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Tue, 14 Feb 2023 19:43:06 +0530 Subject: [PATCH 13/19] test --- docs/methods/metrics/f1Score.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/docs/methods/metrics/f1Score.md b/docs/methods/metrics/f1Score.md index 1d855a0..847e19f 100644 --- a/docs/methods/metrics/f1Score.md +++ b/docs/methods/metrics/f1Score.md @@ -7,20 +7,6 @@ Some advantages of F1-score: 2)If you choose your positive class as the one with fewer samples, F1-score can help balance the metric across positive/negative samples. -Some advantages of F1-score: -1)Very small precision or recall will result in lower overall score. Thus it helps balance the two metrics. -2)If you choose your positive class as the one with fewer samples, F1-score can help balance the metric across positive/negative samples. - - -<<<<<<< HEAD -======= -Some advantages of F1-score: -1)Very small precision or recall will result in lower overall score. Thus it helps balance the two metrics. -2)If you choose your positive class as the one with fewer samples, F1-score can help balance the metric across positive/negative samples. - - ->>>>>>> c9540ce05b05e0bc69fab49ec29acb733e2f7939 - ## Parameters | Name | Definition | Type | From cf7e2c61d9891e75fd24915d3d9e157ff72f3a2e Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Tue, 14 Feb 2023 22:00:02 +0530 Subject: [PATCH 14/19] Changes in DistanceMetric --- CMakeLists.txt | 4 +- examples/metrics/distance_metric_eg.cpp | 14 +++ examples/neighbors/distance_metric_eg.cpp | 18 ---- .../distance_metric/distance_metric.cpp | 72 +++++++++++++++ .../distance_metric/distance_metric.hpp | 58 ++++++++++++ .../distance_metric/distance_metric.cpp | 89 ------------------- .../distance_metric/distance_metric.hpp | 69 -------------- 7 files changed, 146 insertions(+), 178 deletions(-) create mode 100644 examples/metrics/distance_metric_eg.cpp delete mode 100644 examples/neighbors/distance_metric_eg.cpp create mode 100644 src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp create mode 100644 src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp delete mode 100644 src/slowmokit/methods/neighbors/distance_metric/distance_metric.cpp delete mode 100644 src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b81667b..68899f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,5 +63,5 @@ add_library(slowmokit src/slowmokit/methods/metrics/f1score.cpp src/slowmokit/methods/metrics/mean_squared_error.hpp src/slowmokit/methods/metrics/mean_squared_error.cpp - src/slowmokit/methods/neighbors/distance_metric/distance_metric.cpp - src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp) + src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp + src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp) diff --git a/examples/metrics/distance_metric_eg.cpp b/examples/metrics/distance_metric_eg.cpp new file mode 100644 index 0000000..6bf483a --- /dev/null +++ b/examples/metrics/distance_metric_eg.cpp @@ -0,0 +1,14 @@ +// #include "../src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp" + + +// int main() +// { +// std::vector dist1 = {1, 4, 4, 4}; +// std::vector dist2 = {1, 2, 3, 4}; +// DistanceMetric Dist(dist1, dist2); +// std::cout << "Minkowski Distance is " << Dist.minkowski(3) << std::endl; +// std::cout << "Euclidean Distance is " << Dist.euclidean() << std::endl; +// std::cout << "Manhattan Distance is " << Dist.manhattan() << std::endl; +// std::cout << "Cosine Similarity is " << Dist.cosineSimilarity() << std::endl; +// return 0; +// } \ No newline at end of file diff --git a/examples/neighbors/distance_metric_eg.cpp b/examples/neighbors/distance_metric_eg.cpp deleted file mode 100644 index 32a6812..0000000 --- a/examples/neighbors/distance_metric_eg.cpp +++ /dev/null @@ -1,18 +0,0 @@ -// #include "../src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp" - - -// int main() -// { -// std::vector dist1 = {1, 4, 4, 4}; -// std::vector dist2 = {1, 2, 3, 4}; -// DistanceMetric Dist(dist1, dist2); -// std::cout << "Minkowski Distance is " << Dist.minkowski(dist1, dist2, 3) -// << std::endl; -// std::cout << "Euclidean Distance is " << Dist.euclidean(dist1, dist2) -// << std::endl; -// std::cout << "Manhattan Distance is " << Dist.manhattan(dist1, dist2) -// << std::endl; -// std::cout << "Cosine Similarity is " << Dist.cosineSimilarity(dist1, dist2) -// << std::endl; -// return 0; -// } \ No newline at end of file diff --git a/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp b/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp new file mode 100644 index 0000000..ebdfda1 --- /dev/null +++ b/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp @@ -0,0 +1,72 @@ +/** + * @file methods/metrics/distance_metric/distance_metric.hpp + * + * Easy include to calculate distance metrics + */ + +#include "distance_metric.hpp" + +template +DistanceMetric::DistanceMetric(std::vector &x, std::vector &y) +{ + this->x = x; + this->y = y; + if (x.size() != y.size()) + { + throw std::domain_error("Size of the two vectors must be same"); + } +} + +template T DistanceMetric::euclidean() +{ + T distance = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + distance += (x[i] - y[i]) * (x[i] - y[i]); + } + return std::sqrt(distance); +} + +template T DistanceMetric::manhattan() +{ + T distance = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + distance += std::abs(x[i] - y[i]); + } + return distance; +} +template T DistanceMetric::minkowski(int power) +{ + T distance = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + distance += std::pow(x[i] - y[i], power); + } + return std::pow(distance, 1.0 / power); +} + +template T DistanceMetric::magnitude(std::vector &x) +{ + T result = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + result += std::pow(x[i], 2); + } + return std::sqrt(result); +} + +template T DistanceMetric::cosineSimilarity() +{ + T dotProduct = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + dotProduct += x[i] * y[i]; + } + return dotProduct / (magnitude(x) * magnitude(y)); +} diff --git a/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp b/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp new file mode 100644 index 0000000..5e0ab91 --- /dev/null +++ b/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp @@ -0,0 +1,58 @@ +/** + * @file methods/metrics/distance_metric/distance_metric.hpp + * + * Easy include to calculate distances + */ + +#ifndef SLOWMOKIT_DISTANCE_METRIC_HPP +#define SLOWMOKIT_DISTANCE_METRIC_HPP +#include "../../../core.hpp" + +/** + * Takes predicted and actual values of classes + * @param x + * @param y + * @returns the distance metrics + * @throws domain_error exception when size of the two vectors is not equal + */ +template class DistanceMetric +{ + private: + std::vector x; + std::vector y; + + public: + DistanceMetric(std::vector &x, std::vector &y); + + /** + * @returns euclidean distance between the two vectors + */ + T euclidean(); + + + /** + * @returns manhattan distance between the two vectors + */ + T manhattan(); + + + /** + * @param power The order of the norm + * @returns minkowski distance between the two vectors + */ + T minkowski(int); + + /** + * @brief to find the magnitude of the vector + * @param x a vector + * @returns magnitude of x + */ + T magnitude(std::vector &); + + /** + * @returns cosine similarity between the two vectors + */ + T cosineSimilarity(); +}; + +#endif // SLOWMOKIT_DISTANCE_METRIC_HPP \ No newline at end of file diff --git a/src/slowmokit/methods/neighbors/distance_metric/distance_metric.cpp b/src/slowmokit/methods/neighbors/distance_metric/distance_metric.cpp deleted file mode 100644 index 7949953..0000000 --- a/src/slowmokit/methods/neighbors/distance_metric/distance_metric.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/** - * @file methods/neighbors/distance_metric/distance_metric.hpp - * - * Easy include to calculate distance metrics - */ - -#include "distance_metric.hpp" - -template -DistanceMetric::DistanceMetric(std::vector &x, std::vector &t) -{ - this->x = x; - this->y = y; -} - -template -double DistanceMetric::euclidean(std::vector &x, std::vector &y) -{ - if (x.size() != y.size()) - { - throw std::domain_error("Size of the two vectors must be same"); - } - double distance = 0; - int n = x.size(); - for (int i = 0; i < n; i++) - { - distance += (x[i] - y[i]) * (x[i] - y[i]); - } - return sqrt(distance); -} - -template -double DistanceMetric::manhattan(std::vector &x, std::vector &y) -{ - if (x.size() != y.size()) - { - throw std::domain_error("Size of the two vectors must be same"); - } - double distance = 0; - int n = x.size(); - for (int i = 0; i < n; i++) - { - distance += abs(x[i] - y[i]); - } - return distance; -} -template -double DistanceMetric::minkowski(std::vector &x, std::vector &y, - int power) -{ - if (x.size() != y.size()) - { - throw std::domain_error("Size of the two vectors must be same"); - } - double distance = 0.0; - int n = x.size(); - for (int i = 0; i < n; i++) - { - distance += pow(x[i] - y[i], power); - } - return pow(distance, 1.0 / power); -} - -template double DistanceMetric::magnitude(std::vector &x) -{ - double result = 0.0; - int n = x.size(); - for (int i = 0; i < n; i++) - { - result += pow(x[i], 2); - } - return sqrt(result); -} - -template -double DistanceMetric::cosineSimilarity(std::vector &x, std::vector &y) -{ - if (x.size() != y.size()) - { - throw std::domain_error("Size of the two vectors must be same"); - } - double dotProduct = 0.0; - int n = x.size(); - for (int i = 0; i < n; i++) - { - dotProduct += x[i] * y[i]; - } - return dotProduct / (magnitude(x) * magnitude(y)); -} diff --git a/src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp b/src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp deleted file mode 100644 index c847f49..0000000 --- a/src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @file methods/neighbors/distance_metric/distance_metric.hpp - * - * Easy include to calculate distances - */ - -#ifndef SLOWMOKIT_DISTANCE_METRIC_HPP -#define SLOWMOKIT_DISTANCE_METRIC_HPP -#include "../../../core.hpp" - -/** - * Takes predicted and actual values of classes - * @param x - * @param y - * @returns the distance metrics - * @throws domain_error exception when size of the two vectors is not equal - */ -template class DistanceMetric -{ - private: - std::vector x; - std::vector y; - - public: - DistanceMetric(std::vector &x, std::vector &y); - - /** - * @param x An array where each row is a sample and each column is a feature. - * @param y An array where each row is a sample and each column is a feature. - * @throws domain_error exception when size of the two vectors is not equal - * @returns euclidean distance between the two vectors - */ - double euclidean(std::vector &, std::vector &); - - - /** - *@param x An array where each row is a sample and each column is a feature. - * @param y An array where each row is a sample and each column is a feature. - * @throws domain_error exception when size of the two vectors is not equal - * @returns manhattan distance between the two vectors - */ - double manhattan(std::vector &, std::vector &); - - - /** - * @param x An array where each row is a sample and each column is a feature. - * @param y An array where each row is a sample and each column is a feature. - * @throws domain_error exception when size of the two vectors is not equal - * @returns minkowski distance between the two vectors - */ - double minkowski(std::vector &, std::vector &, int); - - /** - * @brief to find the magnitude of the vector - * @param x a vector - * @returns magnitude of x - */ - double magnitude(std::vector &); - - /** - * @param x An array where each row is a sample and each column is a feature. - * @param y An array where each row is a sample and each column is a feature. - * @throws domain_error exception when size of the two vectors is not equal - * @returns cosine similarity between the two vectors - */ - double cosineSimilarity(std::vector &, std::vector &); -}; - -#endif // SLOWMOKIT_DISTANCE_METRIC_HPP \ No newline at end of file From 313e84ea1279998f9a86eb9cc200db838c6c89de Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Tue, 14 Feb 2023 22:24:16 +0530 Subject: [PATCH 15/19] Changes Part 2, hopefully last --- .../distance_metric/distance_metric.cpp | 18 +++++++++--------- .../distance_metric/distance_metric.hpp | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp b/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp index ebdfda1..a607a43 100644 --- a/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp +++ b/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp @@ -17,9 +17,9 @@ DistanceMetric::DistanceMetric(std::vector &x, std::vector &y) } } -template T DistanceMetric::euclidean() +template double DistanceMetric::euclidean() { - T distance = 0; + double distance = 0; int n = x.size(); for (int i = 0; i < n; i++) { @@ -38,9 +38,9 @@ template T DistanceMetric::manhattan() } return distance; } -template T DistanceMetric::minkowski(int power) +template double DistanceMetric::minkowski(int power) { - T distance = 0; + double distance = 0; int n = x.size(); for (int i = 0; i < n; i++) { @@ -49,20 +49,20 @@ template T DistanceMetric::minkowski(int power) return std::pow(distance, 1.0 / power); } -template T DistanceMetric::magnitude(std::vector &x) +template double DistanceMetric::magnitude(std::vector &x) { - T result = 0; + double result = 0; int n = x.size(); for (int i = 0; i < n; i++) { - result += std::pow(x[i], 2); + result += x[i] * x[i]; } return std::sqrt(result); } -template T DistanceMetric::cosineSimilarity() +template double DistanceMetric::cosineSimilarity() { - T dotProduct = 0; + double dotProduct = 0; int n = x.size(); for (int i = 0; i < n; i++) { diff --git a/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp b/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp index 5e0ab91..bc33eb4 100644 --- a/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp +++ b/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp @@ -27,7 +27,7 @@ template class DistanceMetric /** * @returns euclidean distance between the two vectors */ - T euclidean(); + double euclidean(); /** @@ -40,19 +40,19 @@ template class DistanceMetric * @param power The order of the norm * @returns minkowski distance between the two vectors */ - T minkowski(int); + double minkowski(int); /** * @brief to find the magnitude of the vector * @param x a vector * @returns magnitude of x */ - T magnitude(std::vector &); + double magnitude(std::vector &); /** * @returns cosine similarity between the two vectors */ - T cosineSimilarity(); + double cosineSimilarity(); }; #endif // SLOWMOKIT_DISTANCE_METRIC_HPP \ No newline at end of file From fbbeb70ae9483bba09a12dc8a212b6b265568cfd Mon Sep 17 00:00:00 2001 From: Modernbeast02 <89138051+Modernbeast02@users.noreply.github.com> Date: Wed, 15 Feb 2023 13:18:42 +0530 Subject: [PATCH 16/19] Distance_metric.md --- docs/methods/metrics/distance_metric.md | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 docs/methods/metrics/distance_metric.md diff --git a/docs/methods/metrics/distance_metric.md b/docs/methods/metrics/distance_metric.md new file mode 100644 index 0000000..3d8ae72 --- /dev/null +++ b/docs/methods/metrics/distance_metric.md @@ -0,0 +1,47 @@ +# Distance Metric + +## Euclidean Distance +Euclidean Distance represents the shortest distance between two points. + +## Manhattan Distance +Manhattan Distance is the sum of absolute differences between points across all the dimensions. + +## Manhattan Distance +Minkowski Distance is the generalized form of Euclidean and Manhattan Distance. + +## Cosine Similarity +Cosine similarity is a metric, helpful in determining, how similar the data objects are irrespective of their size. + +## Parameters + +| Name | Definition | Type | +| -----------------| --------------------------------------------------------------------------------------- | ---- | +| x | A vector of values | `T`| +| y | A vector of values | `T`| + + +## Methods + +| Name | Definition | Return value | +| ------------------------------- | ----------------------------------------------------- | ----------------- | +| `euclidean()` | To find the euclidean distance | `double` | +| `manhattan()` | To find the manhattan distance | `int, double` | +| `minkowski(int p)` | To find the minkowski distance | `double` | +| `magnitude(vector &x)` | To find the magnitude of the vector | `double` | +| `cosineSimilarity()` | To find the cosine similarity | `double` | + + + +## Example + +```cpp +std::vector dist1 = {1, 4, 4, 4}; +std::vector dist2 = {1, 2, 3, 4}; +DistanceMetric Dist(dist1, dist2); +std::cout << "Minkowski Distance is " << Dist.minkowski(3) << std::endl; +std::cout << "Euclidean Distance is " << Dist.euclidean() << std::endl; +std::cout << "Manhattan Distance is " << Dist.manhattan() << std::endl; +std::cout << "Cosine Similarity is " << Dist.cosineSimilarity() << std::endl; +``` + + From 56ead03230c2d4cce0f9207151820dce2497885b Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Wed, 15 Feb 2023 18:21:12 +0530 Subject: [PATCH 17/19] Hope Never Dies --- .../methods/metrics/distance_metric/distance_metric.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp b/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp index a607a43..8837509 100644 --- a/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp +++ b/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp @@ -44,7 +44,7 @@ template double DistanceMetric::minkowski(int power) int n = x.size(); for (int i = 0; i < n; i++) { - distance += std::pow(x[i] - y[i], power); + distance += std::pow(std::abs(x[i] - y[i]), power); } return std::pow(distance, 1.0 / power); } From deb0d05ae45557925cac51407fb85e40ce003197 Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Wed, 15 Feb 2023 18:48:12 +0530 Subject: [PATCH 18/19] Hope is Dead --- .../methods/metrics/distance_metric/distance_metric.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp b/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp index 8837509..0e2524e 100644 --- a/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp +++ b/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp @@ -25,6 +25,7 @@ template double DistanceMetric::euclidean() { distance += (x[i] - y[i]) * (x[i] - y[i]); } + assert(distance >= 0); return std::sqrt(distance); } @@ -36,6 +37,7 @@ template T DistanceMetric::manhattan() { distance += std::abs(x[i] - y[i]); } + assert(distance >= 0); return distance; } template double DistanceMetric::minkowski(int power) @@ -46,6 +48,7 @@ template double DistanceMetric::minkowski(int power) { distance += std::pow(std::abs(x[i] - y[i]), power); } + assert(distance >= 0); return std::pow(distance, 1.0 / power); } @@ -57,6 +60,7 @@ template double DistanceMetric::magnitude(std::vector &x) { result += x[i] * x[i]; } + assert(result >= 0); return std::sqrt(result); } From 5c3a18e8473aa2a69e1c6425b4d879c47f85a58b Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Sun, 19 Feb 2023 00:44:38 +0530 Subject: [PATCH 19/19] Formatted --- .../distance_metric/distance_metric.cpp | 107 +++++++++--------- .../distance_metric/distance_metric.hpp | 75 ++++++------ 2 files changed, 94 insertions(+), 88 deletions(-) diff --git a/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp b/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp index 0e2524e..cb8917b 100644 --- a/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp +++ b/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp @@ -6,71 +6,76 @@ #include "distance_metric.hpp" -template +template DistanceMetric::DistanceMetric(std::vector &x, std::vector &y) { - this->x = x; - this->y = y; - if (x.size() != y.size()) - { - throw std::domain_error("Size of the two vectors must be same"); - } + this->x = x; + this->y = y; + if (x.size() != y.size()) + { + throw std::domain_error("Size of the two vectors must be same"); + } } -template double DistanceMetric::euclidean() +template +double DistanceMetric::euclidean() { - double distance = 0; - int n = x.size(); - for (int i = 0; i < n; i++) - { - distance += (x[i] - y[i]) * (x[i] - y[i]); - } - assert(distance >= 0); - return std::sqrt(distance); + double distance = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + distance += (x[i] - y[i]) * (x[i] - y[i]); + } + assert(distance >= 0); + return std::sqrt(distance); } -template T DistanceMetric::manhattan() +template +T DistanceMetric::manhattan() { - T distance = 0; - int n = x.size(); - for (int i = 0; i < n; i++) - { - distance += std::abs(x[i] - y[i]); - } - assert(distance >= 0); - return distance; + T distance = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + distance += std::abs(x[i] - y[i]); + } + assert(distance >= 0); + return distance; } -template double DistanceMetric::minkowski(int power) +template +double DistanceMetric::minkowski(int power) { - double distance = 0; - int n = x.size(); - for (int i = 0; i < n; i++) - { - distance += std::pow(std::abs(x[i] - y[i]), power); - } - assert(distance >= 0); - return std::pow(distance, 1.0 / power); + double distance = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + distance += std::pow(std::abs(x[i] - y[i]), power); + } + assert(distance >= 0); + return std::pow(distance, 1.0 / power); } -template double DistanceMetric::magnitude(std::vector &x) +template +double DistanceMetric::magnitude(std::vector &x) { - double result = 0; - int n = x.size(); - for (int i = 0; i < n; i++) - { - result += x[i] * x[i]; - } - assert(result >= 0); - return std::sqrt(result); + double result = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + result += x[i] * x[i]; + } + assert(result >= 0); + return std::sqrt(result); } -template double DistanceMetric::cosineSimilarity() +template +double DistanceMetric::cosineSimilarity() { - double dotProduct = 0; - int n = x.size(); - for (int i = 0; i < n; i++) - { - dotProduct += x[i] * y[i]; - } - return dotProduct / (magnitude(x) * magnitude(y)); + double dotProduct = 0; + int n = x.size(); + for (int i = 0; i < n; i++) + { + dotProduct += x[i] * y[i]; + } + return dotProduct / (magnitude(x) * magnitude(y)); } diff --git a/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp b/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp index bc33eb4..1df618b 100644 --- a/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp +++ b/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp @@ -15,44 +15,45 @@ * @returns the distance metrics * @throws domain_error exception when size of the two vectors is not equal */ -template class DistanceMetric +template +class DistanceMetric { - private: - std::vector x; - std::vector y; - - public: - DistanceMetric(std::vector &x, std::vector &y); - - /** - * @returns euclidean distance between the two vectors - */ - double euclidean(); - - - /** - * @returns manhattan distance between the two vectors - */ - T manhattan(); - - - /** - * @param power The order of the norm - * @returns minkowski distance between the two vectors - */ - double minkowski(int); - - /** - * @brief to find the magnitude of the vector - * @param x a vector - * @returns magnitude of x - */ - double magnitude(std::vector &); - - /** - * @returns cosine similarity between the two vectors - */ - double cosineSimilarity(); + private: + std::vector x; + std::vector y; + + public: + DistanceMetric(std::vector &x, std::vector &y); + + /** + * @returns euclidean distance between the two vectors + */ + double euclidean(); + + + /** + * @returns manhattan distance between the two vectors + */ + T manhattan(); + + + /** + * @param power The order of the norm + * @returns minkowski distance between the two vectors + */ + double minkowski(int); + + /** + * @brief to find the magnitude of the vector + * @param x a vector + * @returns magnitude of x + */ + double magnitude(std::vector &); + + /** + * @returns cosine similarity between the two vectors + */ + double cosineSimilarity(); }; #endif // SLOWMOKIT_DISTANCE_METRIC_HPP \ No newline at end of file