Git Product home page Git Product logo

Comments (4)

lmcinnes avatar lmcinnes commented on August 27, 2024

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.

gokceneraslan avatar gokceneraslan commented on August 27, 2024

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.

lmcinnes avatar lmcinnes commented on August 27, 2024

Yes, actually that should work too. Much easier. Thanks.

from pynndescent.

ljmartin avatar ljmartin commented on August 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.