-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add nodesCosineSimilarity算法-基于节点属性计算余弦相似度 #45
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
Conversation
// 计算节点向量和种子节点向量的点积 | ||
const dot = seedNodeVector.dot(nodeVector); | ||
// 计算节点向量和种子节点向量的余弦相似度 | ||
const cosineSimilarity = dot / (seedNodeNorm2 * nodeNorm2); |
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.
可能除零
!`${node.properties[propertyKey]}`.match(dateReg) && | ||
isNaN(Number(node.properties[propertyKey])) || propertyKey === 'id') { | ||
if (propertyKeyInfo.hasOwnProperty(propertyKey)) { | ||
delete propertyKeyInfo[propertyKey]; |
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.
缩进
}) | ||
|
||
// 取top50的属性 | ||
const sortKeys = Object.keys(propertyKeyInfo).sort((a,b)=>{ |
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.
=> 前后空格
sort((a, b) => propertyKeyInfo[b] - propertyKeyInfo[a])
const sortKeys = Object.keys(propertyKeyInfo).sort((a,b)=>{ | ||
return propertyKeyInfo[b] - propertyKeyInfo[a]; | ||
}); | ||
return sortKeys.length < n ? sortKeys : sortKeys.slice(0, n); |
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.
缩进
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.
好多地方都有问题,都检查一下,不一一评论了
|
||
// 获取所有属性并排序 | ||
export const getAllSortProperties = (nodes: NodeConfig[] = [], n: number = 100) => { | ||
const propertyKeyInfo = {}; |
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.
这个文件的所有缩进不是两格?
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.
缩进还是有问题
packages/graph/src/utils/vector.ts
Outdated
return 0; | ||
} | ||
let res = 0; | ||
for(let key in this.arr) { |
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.
缩进
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.
for 后面加空格
import propertiesGraphData from './data/cluster-origin-properties-data.json'; | ||
|
||
describe('nodesCosineSimilarity', () => { | ||
it('nodesCosineSimilarity simple demo: ', () => { |
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.
最好多1-2个单测,以后改的时候有更强参考性
// 目前过滤只保留可以转成数值型的或日期型的, todo: 统一转成one-hot特征向量或者embedding | ||
if (!`${node.properties[propertyKey]}`.match(secondReg) && | ||
!`${node.properties[propertyKey]}`.match(dateReg) && | ||
isNaN(Number(node.properties[propertyKey])) || propertyKey === 'id') { |
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.
propertyKey === 'id' 和上面所有 && 是同级的?那把这个判断放到最前面,可以有更好性能
e8bcc1e
to
cc29aad
Compare
// 计算元素向量和目标元素向量的点积 | ||
const dot = targetItemVector.dot(itemVector); | ||
// 计算元素向量和目标元素向量的余弦相似度 | ||
const cosineSimilarity = seedNodeNorm2 * itemNorm2 ? dot / (seedNodeNorm2 * itemNorm2) : 0; |
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.
seedNodeNorm2 * itemNorm2 被计算了两次
|
||
// 获取所有属性并排序 | ||
export const getAllSortProperties = (nodes: NodeConfig[] = [], n: number = 100) => { | ||
const propertyKeyInfo = {}; |
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.
缩进还是有问题
packages/graph/src/utils/vector.ts
Outdated
return 0; | ||
} | ||
let res = 0; | ||
for(let key in this.arr) { |
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.
for 后面加空格
|
||
- feat: add consine-similarity algorithm and nodes-consine-similarity algorithm; |
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.
下面加一个空行
b4e35c1
to
aaf8b36
Compare
…gorithm feat: add consine-similarity algorithm and perf code chore: perf code chore: perf code chore: perf code chore: perf code chore: perf code chore: perf code chore: perf code
6206cb3
to
51eb973
Compare
已修改 |
No description provided.