Comments (4)
I believe the following would work:
from pynndescent import NNDescent
import scipy.sparse
import networkx
index = NNDescent(data)
adj_mat = scipy.sparse.lil_matrix((data.shape[0], data.shape[0]), dtype=np.int16)
adj_mat.rows = index._neighbor_graph[0]
adj_mat.data = np.ones_like(index._neighbor_graph[0], dtype=np.int16)
adj_mat = adj_mat.multiply(adj_mat.transpose())
mutual_knn_graph = networkx.from_scipy_sparse_matrix(adj_mat)
With potentially a little finessing of the sparse matrix (some conversions between sparse matrix formats may be required for example). The key here is that the _neighbor_graph
attribute contains an array with the indices of the top k neighbors for each point as the zeroth element (the first element is the corresponding distances); Converting that to a lil matrix is pretty easy and then it's just a matter of requisite manipulations of the adjacency matrix.
from pynndescent.
Thanks!
In pynndescent, after constructing this lil_matrix here:
https://github.com/lmcinnes/pynndescent/blob/master/pynndescent/pynndescent_.py#L1046
I can also do:
result.multiply(result.T).sqrt()
Right?
from pynndescent.
Yes, actually that should work too. Much easier. Thanks.
from pynndescent.
This is old now, and I think the link above is not a permalink.
Anyways for anyone else doing the same thing you can build the adjacency graph like this:
import sparse
indices = list()
indptr = list()
count = 0
indptr.append(count)
for row in index._neighbor_graph[0]:
for item in row:
indices.append(item)
count+=1
indptr.append(count)
adj = sparse.csr_matrix( ( np.ones(len(indices)), indices, indptr))
#assert all the indices in the adjacency are really the ones given in the neighbor graph:
for _ in range(50):
idx = np.random.choice(adj.shape[0])
assert np.all(np.isclose(index._neighbor_graph[0][idx], adj[idx].indices))
edit: the above is a directional knn graph, so not all nearest neighbor adjacencies will be returned.
from pynndescent.
Related Issues (20)
- Cosine metric - error "Negative values in data passed to precomputed distance matrix" HOT 2
- Question about covariance matrix used when using Mahalanobis distance
- Tests fail: E SystemError: initialization of _internal failed without raising an exception
- Newest version breaks with UMAP HOT 3
- Slice error using mac M1-max ARM HOT 6
- Exceedingly large amount of memory usage
- Very high memory usage HOT 7
- Specifying threshold in distance metrics
- API to save and load index from disk
- Minor bias in split selection? HOT 1
- true_angular is not a distance?
- TSSS missing a factor of 2
- Reverse diversification is actually forward diversification again
- pynndescent might break with next numba release HOT 3
- How to navigate pyinstaller HOT 1
- Querying the training set: runtime tradeoff for large k HOT 2
- 1 test fails: ZeroDivisionError: division by zero
- `np.infty` replacement
- Speed-up Indexing with custom metrics HOT 1
- Updating a compressed index fails with `AttributeError: 'NNDescent' object has no attribute '_neighbor_graph'` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pynndescent.