-
Notifications
You must be signed in to change notification settings - Fork 15
Implemented Various Distance Metric #107
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
Open
Modernbeast02
wants to merge
30
commits into
PEC-CSS:main
Choose a base branch
from
Modernbeast02:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 22 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
4900b40
Resolved Issue #64
Modernbeast02 341ebc0
Fixed All Operators Issue#64
Modernbeast02 34f6cc2
Merge branch 'PEC-CSS:main' into main
Modernbeast02 0c12221
Taken Pull and Merged
Modernbeast02 d6d077b
Merge branch 'main' of https://github.com/Modernbeast02/PWOC_slowmokit
Modernbeast02 16031e4
Resolved Issue#81
Modernbeast02 3fb7402
Revert "Resolved Issue#81"
Modernbeast02 019f9f2
Resolved Issue #81
Modernbeast02 e955eee
Merge branch 'main' into Issue
Ishwarendra 9718f25
conflict resolved
Modernbeast02 c2d485e
conflict resolved
Modernbeast02 3512f9b
F1 Score #72
Modernbeast02 5d0550e
Resolved Conflict in matrix.cpp
Modernbeast02 93d1b7a
Resolved #72
Modernbeast02 0d73928
Formatted Code
Modernbeast02 c3fb7c9
f1Score.md
Modernbeast02 004c5c2
Update f1Score.md
Modernbeast02 8d19454
Update f1Score.md
Modernbeast02 2e4d110
Implemented Distance Metric
Modernbeast02 a55d7cb
Distance Metric
Modernbeast02 4e01ffb
test
Modernbeast02 6601058
test
Modernbeast02 cf7e2c6
Changes in DistanceMetric
Modernbeast02 313e84e
Changes Part 2, hopefully last
Modernbeast02 fbbeb70
Distance_metric.md
Modernbeast02 56ead03
Hope Never Dies
Modernbeast02 77e2b07
Merge branch 'main' of https://github.com/Modernbeast02/PWOC_slowmokit
Modernbeast02 deb0d05
Hope is Dead
Modernbeast02 87d1367
Resolved Conflicts
Modernbeast02 5c3a18e
Formatted
Modernbeast02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // #include "../src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp" | ||
|
|
||
|
|
||
| // int main() | ||
| // { | ||
| // std::vector<int> dist1 = {1, 4, 4, 4}; | ||
| // std::vector<int> 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; | ||
| // } |
89 changes: 89 additions & 0 deletions
89
src/slowmokit/methods/neighbors/distance_metric/distance_metric.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /** | ||
| * @file methods/neighbors/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> &t) | ||
| { | ||
| this->x = x; | ||
| this->y = y; | ||
| } | ||
|
|
||
| template<class T> | ||
| double DistanceMetric<T>::euclidean(std::vector<T> &x, std::vector<T> &y) | ||
uttammittal02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
uttammittal02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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); | ||
uttammittal02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| template<class T> | ||
| double DistanceMetric<T>::manhattan(std::vector<T> &x, std::vector<T> &y) | ||
| { | ||
| if (x.size() != y.size()) | ||
| { | ||
| throw std::domain_error("Size of the two vectors must be same"); | ||
| } | ||
| double distance = 0; | ||
uttammittal02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| int n = x.size(); | ||
| for (int i = 0; i < n; i++) | ||
| { | ||
| distance += abs(x[i] - y[i]); | ||
uttammittal02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return distance; | ||
| } | ||
| template<class T> | ||
| double DistanceMetric<T>::minkowski(std::vector<T> &x, std::vector<T> &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); | ||
uttammittal02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return pow(distance, 1.0 / power); | ||
| } | ||
|
|
||
| template<class T> double DistanceMetric<T>::magnitude(std::vector<T> &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<class T> | ||
| double DistanceMetric<T>::cosineSimilarity(std::vector<T> &x, std::vector<T> &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)); | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
src/slowmokit/methods/neighbors/distance_metric/distance_metric.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 T> class DistanceMetric | ||
| { | ||
| private: | ||
| std::vector<T> x; | ||
| std::vector<T> y; | ||
|
|
||
| public: | ||
| DistanceMetric(std::vector<T> &x, std::vector<T> &y); | ||
uttammittal02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @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<T> &, std::vector<T> &); | ||
|
|
||
|
|
||
| /** | ||
| *@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<T> &, std::vector<T> &); | ||
|
|
||
|
|
||
| /** | ||
| * @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<T> &, std::vector<T> &, int); | ||
|
|
||
| /** | ||
| * @brief to find the magnitude of the vector | ||
| * @param x a vector | ||
| * @returns magnitude of x | ||
| */ | ||
| double magnitude(std::vector<T> &); | ||
|
|
||
| /** | ||
| * @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<T> &, std::vector<T> &); | ||
| }; | ||
|
|
||
| #endif // SLOWMOKIT_DISTANCE_METRIC_HPP | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.