[Python] How to fix an error in code of VS code in neutral graph network

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I meet a error when coding. It is "'a' and 'p' must have same size" This is code:

    ` %pip install -q node2vec==0.4.6
    %pip install -qI gensim==4.3.0`
    `import networkx as nx
    import matplotlib.pyplot as plt
    `
    # Create graph
    `G = nx.erdos_renyi_graph(10, 0.3, seed=1, directed=False)`

    # Plot graph
    `plt.figure()
    plt.axis('off')
    nx.draw_networkx(G,
    pos=nx.spring_layout(G, seed=0),
    node_size=600,
    cmap='coolwarm',
    font_size=14,
    font_color='white'
    )
    import random
    random.seed(0)
    import numpy as np
    np.random.seed(0)
    def next_node(previous, current, p, q):
    neighbors = list(G.neighbors(current))
    alphas = []
    for neighbor in neighbors:
    if neighbor == previous:
    alpha = 1/p
    elif G.has_edge(neighbor, previous):
    alpha = 1
    else:
    alpha = 1/q
    alphas.append(alpha)
    # Normalize the alpha values to create transition probabilities
    probs = [alpha / sum(alphas) for alpha in alphas]`

    # Randomly select the new node based on the transition probabilities
    `next = np.random.choice(neighbors, size=1, p=probs)[0]
    return next
    def random_walk(start, length, p, q):
    walk = [start]

    for i in range(length):
    current = walk[-1]
    previous = walk[-2] if len(walk) > 1 else None
    next = next_node(previous, current, p, q)
    walk.append(next)

    return [str (x) for x in walk]
    random_walk(0, 8, p=1, q=1)
    `


    I want fix this error and I expect to fix successfully. Why have error "'a' and 'p' must have same size". How to fix it? I have try to adapt some position but it does not change. I need know how to fix it.

    Continue reading...

Compartilhe esta Página