PacktPublishing / Network-Science-with-Python

Network Science with Python, published by Packt
MIT License
28 stars 26 forks source link

Potential fix for error I was getting in Chapter 7 #1

Open mrkatey opened 1 year ago

mrkatey commented 1 year ago
def draw_graph(G, show_names=False, node_size=1, font_size=10, edge_width=0.5): 

    import numpy as np
    import networkx as nx

    from IPython.display import SVG 
    from sknetwork.visualization import svg_graph 
    from sknetwork.data import Bunch 
    from sknetwork.ranking import PageRank 
    import scipy.sparse as sp

    adjacency = nx.adjacency_matrix(G, weight='weight')
    adjacency = sp.csr_matrix(adjacency)

    names = np.array(list(G.nodes())) 

    graph = Bunch() 
    graph.adjacency = adjacency 
    graph.names = np.array(names) 

    pagerank = PageRank() 

    scores = pagerank.fit_transform(adjacency) 

    if show_names: 

        image = svg_graph(graph.adjacency, font_size=font_size, node_size=node_size, names=graph.names, width=700, height=500, scores=scores, edge_width=edge_width) 

    else: 

        image = svg_graph(graph.adjacency, node_size=node_size, width=700, height=500, scores = scores, edge_width=edge_width) 

    return SVG(image) 
afourteene commented 4 months ago

Hey, If you have a problem with this function you can fix it like this code: ` def draw_graph(G, show_names=False, node_size=1, font_size=10, edge_width=0.5):

adjacency = nx.adjacency_matrix(G)

adjacency = csr_matrix(adjacency)

names = np.array(list(G.nodes())) 
graph = Bunch(adjacency=adjacency, names=names) 

pagerank = PageRank() 
pagerank.fit(adjacency)  # Compute PageRank scores

scores = pagerank.scores_

if show_names: 
    names = [str(name) for name in graph.names]  
    image = svg_graph(graph.adjacency, font_size=font_size, node_size=node_size, names=names, width=700, height=500, scores=scores, edge_width=edge_width) 
else: 
    image = svg_graph(graph.adjacency, node_size=node_size, width=700, height=500, scores=scores, edge_width=edge_width) 

return SVG(image)

`