-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented Various Distance Metric #107
base: main
Are you sure you want to change the base?
Changes from 1 commit
4900b40
341ebc0
34f6cc2
0c12221
d6d077b
16031e4
3fb7402
019f9f2
e955eee
9718f25
c2d485e
3512f9b
5d0550e
93d1b7a
0d73928
c3fb7c9
004c5c2
8d19454
2e4d110
a55d7cb
4e01ffb
6601058
cf7e2c6
313e84e
fbbeb70
56ead03
77e2b07
deb0d05
87d1367
5c3a18e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// #include "../src/slowmokit/methods/metrics/distance_metric/distance_metric.hpp" | ||
|
||
|
||
// int main() | ||
// { | ||
// std::vector<double> dist1 = {1, 4, 4, 4}; | ||
// std::vector<double> 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; | ||
// } |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,72 @@ | ||||||||||||||
/** | ||||||||||||||
* @file methods/metrics/distance_metric/distance_metric.hpp | ||||||||||||||
* | ||||||||||||||
* Easy include to calculate distance metrics | ||||||||||||||
*/ | ||||||||||||||
|
||||||||||||||
#include "distance_metric.hpp" | ||||||||||||||
|
||||||||||||||
template<class T> | ||||||||||||||
DistanceMetric<T>::DistanceMetric(std::vector<T> &x, std::vector<T> &y) | ||||||||||||||
{ | ||||||||||||||
this->x = x; | ||||||||||||||
this->y = y; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Use initialiser list, they are faster. |
||||||||||||||
if (x.size() != y.size()) | ||||||||||||||
{ | ||||||||||||||
throw std::domain_error("Size of the two vectors must be same"); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
template<class T> T DistanceMetric<T>::euclidean() | ||||||||||||||
{ | ||||||||||||||
T distance = 0; | ||||||||||||||
uttammittal02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
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<class T> T DistanceMetric<T>::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<class T> T DistanceMetric<T>::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); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||||||||||||||
} | ||||||||||||||
return std::pow(distance, 1.0 / power); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
template<class T> T DistanceMetric<T>::magnitude(std::vector<T> &x) | ||||||||||||||
{ | ||||||||||||||
T result = 0; | ||||||||||||||
int n = x.size(); | ||||||||||||||
for (int i = 0; i < n; i++) | ||||||||||||||
{ | ||||||||||||||
result += std::pow(x[i], 2); | ||||||||||||||
uttammittal02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
} | ||||||||||||||
return std::sqrt(result); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
template<class T> T DistanceMetric<T>::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)); | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -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 | ||||||||
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add short statement describing |
||||||||
* @returns the distance metrics | ||||||||
* @throws domain_error exception when size of the two vectors is not equal | ||||||||
*/ | ||||||||
template<class T> class DistanceMetric | ||||||||
{ | ||||||||
private: | ||||||||
std::vector<T> x; | ||||||||
std::vector<T> y; | ||||||||
|
||||||||
public: | ||||||||
DistanceMetric(std::vector<T> &x, std::vector<T> &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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
*/ | ||||||||
T minkowski(int); | ||||||||
|
||||||||
/** | ||||||||
* @brief to find the magnitude of the vector | ||||||||
* @param x a vector | ||||||||
* @returns magnitude of x | ||||||||
*/ | ||||||||
T magnitude(std::vector<T> &); | ||||||||
|
||||||||
/** | ||||||||
* @returns cosine similarity between the two vectors | ||||||||
*/ | ||||||||
T cosineSimilarity(); | ||||||||
}; | ||||||||
|
||||||||
#endif // SLOWMOKIT_DISTANCE_METRIC_HPP |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want user to feel insecure about their data. Use
const
. (No need to update in.hpp
file)const in cpp but not in hpp?
.hpp
file.cpp
fileis valid and recommended.