-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add cosine similarity algorithm #69
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7476f19
feat: cosine similarity algorithm
zqqcee 64e3e6c
test: unit test
zqqcee c1c39ae
Merge remote-tracking branch 'upstream/next' into v5-algo-cosinesim
zqqcee 39e7191
fix: change default export to export
zqqcee 4712412
feat: v5 algorithm nodes-cosine-similarity
zqqcee c3dd035
test: nodes cossim unit test
zqqcee 411ea1a
fix: fix import bug, fix: change NodeSimilarity type
zqqcee cb2a2e2
fix: remove annotation
zqqcee 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import cosineSimilarity from "../../packages/graph/src/cosine-similarity"; | ||
|
|
||
| describe('cosineSimilarity abnormal demo: ', () => { | ||
| it('item contains only zeros: ', () => { | ||
| const item = [0, 0, 0]; | ||
| const targetTtem = [3, 1, 1]; | ||
| const cosineSimilarityValue = cosineSimilarity(item, targetTtem); | ||
| expect(cosineSimilarityValue).toBe(0); | ||
| }); | ||
| it('targetTtem contains only zeros: ', () => { | ||
| const item = [3, 5, 2]; | ||
| const targetTtem = [0, 0, 0]; | ||
| const cosineSimilarityValue = cosineSimilarity(item, targetTtem); | ||
| expect(cosineSimilarityValue).toBe(0); | ||
| }); | ||
| it('item and targetTtem both contains only zeros: ', () => { | ||
| const item = [0, 0, 0]; | ||
| const targetTtem = [0, 0, 0]; | ||
| const cosineSimilarityValue = cosineSimilarity(item, targetTtem); | ||
| expect(cosineSimilarityValue).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('cosineSimilarity normal demo: ', () => { | ||
| it('demo similar: ', () => { | ||
| const item = [30, 0, 100]; | ||
| const targetTtem = [32, 1, 120]; | ||
| const cosineSimilarityValue = cosineSimilarity(item, targetTtem); | ||
| expect(cosineSimilarityValue).toBeGreaterThanOrEqual(0); | ||
| expect(cosineSimilarityValue).toBeLessThan(1); | ||
| expect(Number(cosineSimilarityValue.toFixed(3))).toBe(0.999); | ||
| }); | ||
| it('demo dissimilar: ', () => { | ||
| const item = [10, 300, 2]; | ||
| const targetTtem = [1, 2, 30]; | ||
| const cosineSimilarityValue = cosineSimilarity(item, targetTtem); | ||
| expect(cosineSimilarityValue).toBeGreaterThanOrEqual(0); | ||
| expect(cosineSimilarityValue).toBeLessThan(1); | ||
| expect(Number(cosineSimilarityValue.toFixed(3))).toBe(0.074); | ||
| }); | ||
| }); |
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,29 @@ | ||
| import { Vector } from "./vector"; | ||
|
|
||
| /** | ||
| Calculates the cosine similarity | ||
| @param item - The element. | ||
| @param targetItem - The target element. | ||
| @returns The cosine similarity between the item and the targetItem. | ||
| */ | ||
| const cosineSimilarity = ( | ||
| item: number[], | ||
| targetItem: number[], | ||
| ): number => { | ||
| // Vector of the target element | ||
| const targetItemVector = new Vector(targetItem); | ||
| // Norm of the target element vector | ||
| const targetNodeNorm2 = targetItemVector.norm2(); | ||
| // Vector of the item | ||
| const itemVector = new Vector(item); | ||
| // Norm of the item vector | ||
| const itemNorm2 = itemVector.norm2(); | ||
| // Calculate the dot product of the item vector and the target element vector | ||
| const dot = targetItemVector.dot(itemVector); | ||
| const norm2Product = targetNodeNorm2 * itemNorm2; | ||
| // Calculate the cosine similarity between the item vector and the target element vector | ||
| const cosineSimilarity = norm2Product ? dot / norm2Product : 0; | ||
| return cosineSimilarity; | ||
| } | ||
|
|
||
| export default cosineSimilarity; | ||
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.
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.
nodes-cosine-similarity 也可以一起提~
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.
✅