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; +``` + + diff --git a/docs/methods/metrics/f1Score.md b/docs/methods/metrics/f1Score.md index a12f9b4..847e19f 100644 --- a/docs/methods/metrics/f1Score.md +++ b/docs/methods/metrics/f1Score.md @@ -7,7 +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. - ## Parameters | Name | Definition | Type | 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/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..cb8917b --- /dev/null +++ b/src/slowmokit/methods/metrics/distance_metric/distance_metric.cpp @@ -0,0 +1,81 @@ +/** + * @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 +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); +} + +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; +} +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); +} + +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); +} + +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)); +} 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..1df618b --- /dev/null +++ b/src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp @@ -0,0 +1,59 @@ +/** + * @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 + */ + 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