pysal / libpysal

Core components of Python Spatial Analysis Library
http://pysal.org/libpysal
Other
265 stars 78 forks source link

add distance attribute to knn weights #762

Open lanselin opened 2 months ago

lanselin commented 2 months ago

It would be nice if the KNN weights object also included a distance attribute in addition to neighbors and weights. The weights are all 1, which is not informative. In contrast, distance band weights have a binary argument, which if False gives the distance raised to a power.

I believe it would not necessitate any additional calculations. As an example, consider the GWT k-nearest neighbor weights created by GeoDa. Also, sklearn.neighbors KDTree already contains the distances.

martinfleis commented 2 months ago

As noted in #763, this is already possible, just using the kernel builder instead of KNN.

With Graph:

Graph.build_kernel(df, k=5, kernel="identity")
# or
Graph.build_kernel(df, k=5, kernel=None)

With W:

weights.Kernel.from_dataframe(df, k=5, kernel="identity")
lanselin commented 2 months ago

The kernel="identity" argument is not documented in the API documentation for weights.Kernel. Under libpysal.weights.Kernel class method from_dataframe there is no mention of a "kernel" argument., the argument is "function", but maybe that is something different. Please clarify the docs, that is the first point of contact for many users.

ljwolf commented 2 months ago

Maybe I'm misreading this, but I don't think that the identity kernel is supported for the old W class.

martinfleis commented 2 months ago

I honestly believed that the graph kernel API is just a mirror of weights in this case. My mistake, sorry.