Skip to content

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

Merged
merged 1 commit into from
Dec 16, 2021

Conversation

xdddst
Copy link
Member

@xdddst xdddst commented Dec 15, 2021

No description provided.

// 计算节点向量和种子节点向量的点积
const dot = seedNodeVector.dot(nodeVector);
// 计算节点向量和种子节点向量的余弦相似度
const cosineSimilarity = dot / (seedNodeNorm2 * nodeNorm2);
Copy link
Contributor

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];
Copy link
Contributor

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)=>{
Copy link
Contributor

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进

Copy link
Contributor

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 = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件的所有缩进不是两格?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进还是有问题

return 0;
}
let res = 0;
for(let key in this.arr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进

Copy link
Contributor

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: ', () => {
Copy link
Contributor

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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

propertyKey === 'id' 和上面所有 && 是同级的?那把这个判断放到最前面,可以有更好性能

@xdddst xdddst force-pushed the feat/add-cosine-similarity branch 3 times, most recently from e8bcc1e to cc29aad Compare December 16, 2021 03:58
// 计算元素向量和目标元素向量的点积
const dot = targetItemVector.dot(itemVector);
// 计算元素向量和目标元素向量的余弦相似度
const cosineSimilarity = seedNodeNorm2 * itemNorm2 ? dot / (seedNodeNorm2 * itemNorm2) : 0;
Copy link
Contributor

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 = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进还是有问题

return 0;
}
let res = 0;
for(let key in this.arr) {
Copy link
Contributor

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下面加一个空行

@xdddst xdddst force-pushed the feat/add-cosine-similarity branch 3 times, most recently from b4e35c1 to aaf8b36 Compare December 16, 2021 04:15
…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
@xdddst xdddst force-pushed the feat/add-cosine-similarity branch from 6206cb3 to 51eb973 Compare December 16, 2021 04:18
@xdddst
Copy link
Member Author

xdddst commented Dec 16, 2021

已修改

@Yanyan-Wang Yanyan-Wang merged commit 646a2bc into master Dec 16, 2021
@Yanyan-Wang Yanyan-Wang deleted the feat/add-cosine-similarity branch December 16, 2021 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants