# How to apply graph embedding to KNN algorithm?

**URL:** <https://community.neo4j.com/t/how-to-apply-graph-embedding-to-knn-algorithm/47319>\
**Category:** Neo4j Graph Platform\
**Created:** [November 11, 2021, 10:10pm UTC](https://community.neo4j.com/t/how-to-apply-graph-embedding-to-knn-algorithm/47319 "2021-11-11T22:10:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lingvisa](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lingvisa/32/8150_2.png) [@lingvisa](https://community.neo4j.com/u/lingvisa)\
**Post date:** [November 11, 2021, 10:10pm UTC](https://community.neo4j.com/t/how-to-apply-graph-embedding-to-knn-algorithm/47319/1 "2021-11-11T22:10:15Z")

</div>

In particular, if I use fastRPExtended to generate embeddings for nodes with 256 dimensions, named 'graphEmbedding', does it make sense to use 'graphEmbedding' as the parameter of the KNN agorithm, nodeWeightProperty? The following example documentation uses a single float 'age'.

```auto
CALL gds.beta.knn.write('myGraph', {
    writeRelationshipType: 'SIMILAR',
    writeProperty: 'score',
    topK: 1,
    randomSeed: 42,
    nodeWeightProperty: 'age'
})
YIELD nodesCompared, relationshipsWritten

```

In the original paper, 'Efficient k-nearest neighbor graph construction for generic similarity measures', it discussed the parameter K and D:

1. it experimented on several K values: 10, 20, 30, 40, 50, and recommend K=20 by default.
2. It experimented on 3 sets of D values, (2,5), (10,20), (50, 100), and it says their algorithm best suits for D=20 and K=20 too.

Is K in the paper the topK in neo4j KNN?  
Is D in the paper the dimensionality of the nodeWeightProperty in neo4j KNN? If that's the case, it doesn't make sense to use a dimension of 256 to graphEmbedding for this property value? Because the optimal D=20 according to the paper.

Please clarify the implementation of KNN in GDS. Thanks.

---

<div class="post-metadata">

**Author:** ![nsmith\_piano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/nsmith_piano/32/6037_2.png) [@nsmith\_piano](https://community.neo4j.com/u/nsmith_piano)\
**Post date:** [November 15, 2021, 2:53pm UTC](https://community.neo4j.com/t/how-to-apply-graph-embedding-to-knn-algorithm/47319/2 "2021-11-15T14:53:13Z")

</div>

1. Yes, the K parameter in the paper is the same as the topK parameter in the GDS algorithm.

2. The D parameter in the paper represents the [intrinsic dimensionality](https://en.wikipedia.org/wiki/Intrinsic_dimension) of the dataset. Intrinsic dimensionality is a measure of the complexity of the dataset, and it is different from the dimensionality of the input features in the dataset. Table 1 in the paper shows you the dimensionality of the datasets that the authors worked with. All but one of them were greater than 20.

I think you should have good [recall](https://en.wikipedia.org/wiki/Precision_and_recall) with 256 dimension vectors as the nodeWeightProperty and topK = 20.

---

<div class="post-metadata">

**Author:** ![lingvisa](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lingvisa/32/8150_2.png) [@lingvisa](https://community.neo4j.com/u/lingvisa)\
**Post date:** [November 15, 2021, 4:35pm UTC](https://community.neo4j.com/t/how-to-apply-graph-embedding-to-knn-algorithm/47319/3 "2021-11-15T16:35:09Z")

</div>

A related question regarding the choice of K: for the general KNN algorithm, the sqrt(N) is considered as the best K value. If the graph have 1 million nodes, the K would be 1000, which is huge compared with the default K=20 in the Dong's paper. So we shouldn't use sqrt(N) but use a much smaller K as described in the paper?

---

<div class="post-metadata">

**Author:** ![nsmith\_piano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/nsmith_piano/32/6037_2.png) [@nsmith\_piano](https://community.neo4j.com/u/nsmith_piano)\
**Post date:** [November 15, 2021, 8:20pm UTC](https://community.neo4j.com/t/how-to-apply-graph-embedding-to-knn-algorithm/47319/4 "2021-11-15T20:20:54Z")

</div>

When we talk about KNN, we are often thinking of the classification machine learning algorithm. We find the labels of the k-nearest neighbor to a point with an unknown label, and then infer the value of the unknown label based on the majority class of the neighbors. The K = sqrt(n) rule of thumb often turns out to work well for that purpose.

The Neo4j implementation of KNN is not a classifier. We are using the algorithm to find the nearest neighbors to each node, and usually creating relationships to those nodes. Without checking every single combination of nodes in the graph, the algorithm is able to efficiently give us _approximately_ the k-closest neighbors to each node. Dong's algorithm doesn't have anything to say about node labels. The target is recall, in other words, the percentage of each nodes true nearest neighbors returned by the algorithm.

The right K to optimize recall without running for too long will depend on your use case and the complexity of your dataset. The values of K used in Dong's paper were much smaller than sqrt(N) for the datasets shown in table 1. If you are using graph embeddings of dimension 256, I would think your recall would be good at K=20, similar to the dense vectors in the Corel, Audio, and Shape data sets that Dong tested.

---

<div class="post-metadata">

**Author:** ![lingvisa](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lingvisa/32/8150_2.png) [@lingvisa](https://community.neo4j.com/u/lingvisa)\
**Post date:** [November 16, 2021, 12:29am UTC](https://community.neo4j.com/t/how-to-apply-graph-embedding-to-knn-algorithm/47319/5 "2021-11-16T00:29:56Z")

</div>

I was also thinking the Neo4j implementation of KNN is not in the sense of classification. Thanks for the clear explanation.
