mhahsler / dbscan

Density Based Clustering of Applications with Noise (DBSCAN) and Related Algorithms - R package
GNU General Public License v3.0
311 stars 65 forks source link

some strange results of sNN function #37

Closed ZhenyiWangTHU closed 5 years ago

ZhenyiWangTHU commented 5 years ago

I use the function sNN() to find the shared neighbors of points, but I notice a strange result when I test this function on an easy example. The code and results are shown below:

testdata <- c(-2,-1,0,1,2,2.4,2.5,3,3.5,4) distancematrix <- dist(testdata,method = "minkowski",p=2) test_res <- sNN(x=distancematrix,k=5,sort = FALSE) test_res$id[3,] 1 2 3 4 5 2 4 1 5 6 test_res$shared[3,] [1] 5 4 5 0 0

The shared k=5 neighbors of '0' and '2' are '1' and '2', but sNN() says they have no shared neighbors. Did I do anything wrong? Any suggestions will be appreciated!

ZhenyiWangTHU commented 5 years ago

Hah, results are reasonable. Because sNN only calculates the number of shared nearest neighbors of points are in each others snn list. This function is fast and powerful. Thank you for your contribution!

ZhenyiWangTHU commented 5 years ago

I interpreted the meaning of sNN() function as above, but I'm not very sure about it. If I was wrong, please let me know. Thank you :)

mhahsler commented 5 years ago

Thank you for the bug report. I have found a problem in the code that computes the shared NN similarity. I have fixed the code on GitHub. The fix will be part of the next release on CRAN.

ZhenyiWangTHU commented 5 years ago

Thank you for the bug report. I have found a problem in the code that computes the shared NN similarity. I have fixed the code on GitHub. The fix will be part of the next release on CRAN.

Hi Michael, thank you for your quick response! I tried your new code using the example above and get the result is test_res$shared[3,] 5 4 5 2 2 This result is correct, but I think the result before is also correct. Because old version sNN only calculates the number of shared NN of points are in each others sNN list. If two points are not in each other's snn list, the similarity of them will be 0. The new version one calculates all k nearest neighbor's shared NN similarity and return them. Namely, even two point are not in each other's snn list, if their k nearest neighbors are intersected, the similarity of them will be the number of the intersection but not 0. Hah, I actually need the old version function in my algorithm. Thanks again!

mhahsler commented 5 years ago

I had to go back to the sNN clustering paper to clear this up and decided to implement both versions by providing the parameter jp. If jp is TRUE the sNN will use the definition by Javis and Patrick (1973), where shared neighbors are only counted between points that are in each other's neighborhood, otherwise, 0 is returned. If FALSE, then the number of shared neighbors is returned, even if the points are not neighbors.

Let me know it this works for you.

ZhenyiWangTHU commented 5 years ago

I had to go back to the sNN clustering paper to clear this up and decided to implement both versions by providing the parameter jp. If jp is TRUE the sNN will use the definition by Javis and Patrick (1973), where shared neighbors are only counted between points that are in each other's neighborhood, otherwise, 0 is returned. If FALSE, then the number of shared neighbors is returned, even if the points are not neighbors.

Let me know it this works for you.

This works for me, cheers! I think both versions are very useful for different researchers. Thanks a million for your nice work!

mhahsler commented 5 years ago

Great!