ranking vertices by value of vertex property

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

ranking vertices by value of vertex property

Ioana K-Hulpus
Hi,
I ran PageRank on my graph, and now I need to get the top-k highest scored.
Is there any faster / more straightforward way of achieving this than by
creating and then sorting a new dictionary, as in the following method?


def top_k_page_rank(g, pagerank_prop, k):
    pr_sorted_scores = sort_pagerank_scores(g, pagerank_prop)
    top_k = list(pr_sorted_scores.items())[0:k]
    return top_k


def sort_pagerank_scores(g, pagerank_prop):
    pr_scores = dict()
    for v in g.vertices():
        pr_scores[v] = g.vertex_properties[pagerank_prop][v]
    pr_sorted_scores = {k: v for k, v in sorted(pr_scores.items(),
key=lambda item: item[1], reverse=True)}
    return pr_sorted_scores

Thanks,
Ioana






--
Sent from: https://nabble.skewed.de/
_______________________________________________
graph-tool mailing list
[hidden email]
https://lists.skewed.de/mailman/listinfo/graph-tool
Reply | Threaded
Open this post in threaded view
|

Re: ranking vertices by value of vertex property

Tiago Peixoto
Administrator
Am 07.07.20 um 15:39 schrieb Ioana K-Hulpus:
> Hi,
> I ran PageRank on my graph, and now I need to get the top-k highest scored.
> Is there any faster / more straightforward way of achieving this than by
> creating and then sorting a new dictionary, as in the following method?

Yes, you can access property map values as numpy arrays via the ".a"
attribute, e.g. if pr is a vertex property map with the pagerank scores,
then

   pr.a

will give you a numpy array. This means you can use the argsort() method
to obtain the indexes (i.e. vertices) in increasing order:

   pr.a.argsort()

Best,
Tiago




--
Tiago de Paula Peixoto <[hidden email]>


_______________________________________________
graph-tool mailing list
[hidden email]
https://lists.skewed.de/mailman/listinfo/graph-tool

signature.asc (849 bytes) Download Attachment
--
Tiago de Paula Peixoto <tiago@skewed.de>
Reply | Threaded
Open this post in threaded view
|

Re: ranking vertices by value of vertex property

Ioana K-Hulpus
Great, thanks a lot!
-Ioana

On Tue, Jul 7, 2020 at 4:29 PM Tiago de Paula Peixoto <[hidden email]> wrote:
Am 07.07.20 um 15:39 schrieb Ioana K-Hulpus:
> Hi,
> I ran PageRank on my graph, and now I need to get the top-k highest scored.
> Is there any faster / more straightforward way of achieving this than by
> creating and then sorting a new dictionary, as in the following method?

Yes, you can access property map values as numpy arrays via the ".a"
attribute, e.g. if pr is a vertex property map with the pagerank scores,
then

   pr.a

will give you a numpy array. This means you can use the argsort() method
to obtain the indexes (i.e. vertices) in increasing order:

   pr.a.argsort()

Best,
Tiago




--
Tiago de Paula Peixoto <[hidden email]>

_______________________________________________
graph-tool mailing list
[hidden email]
https://lists.skewed.de/mailman/listinfo/graph-tool

_______________________________________________
graph-tool mailing list
[hidden email]
https://lists.skewed.de/mailman/listinfo/graph-tool