Python Practical Data Analysis 100 Knock knock 72

Asked 2 years ago, Updated 2 years ago, 23 views

If you run the code below, the node will not turn red even if you change the value of t.Please let me know if there are any mistakes.

The environment is Jupiter Notebook with networkx version up to 2.6.3.

import pandas as pd 
df_links=pd.read_csv("links.csv')
df_links
import pandas as pd
df_links=pd.read_csv("links.csv")
df_links

import networkx as nx
import matplotlib.pyplot asplt

G=nx.Graph()

NUM=len(df_links.index)
for i in range (1,NUM+1):
    node_no=df_links.columns[i].string("Node")
    G.add_node(str(node_no))

for i in range (NUM):
    for jin range (NUM):
        node_name = "Node" + str(j)
        if df_links [node_name].iloc[i] == 1:
            G.add_edge(str(i), str(j))

nx.draw_networkx(G,node_color="k",edge_color="k",font_color="w")

plt.show()
import numpy as np

def determine_link (percent):
    land_val = np.random.rand()
    ifland_val<=percent:
        return1
    else:
        return 0
    
def simulate_percolation(num, list_active, percent_percolation):
    for i in range (num):
        if list_active[i] == 1:
            for jin range (num):
                 node_name = "Node" + str(j)
            if df_links [node_name].iloc[i] == 1:
                    if determine_link (percent_percolation) == 1:
                        list_active[j] = 1
    
    return list_active

percent_percolation = 0.1
T_NUM = 36
NUM=len(df_links.index)
list_active=np.zeros(NUM)
list_active[0] = 1
list_timeSeries = [ ]

for in range(T_NUM):
    list_active=simulate_percolation(NUM, list_active, percent_percolation)
    list_timeSeries.append(list_active.copy())
                    
default_node_coloring(list_active):
    list_color=[ ]
    for i in range (len(list_timeSeries[t])):
        if list_timeSeries[t][i] == 1:
            list_color.append("r")
        else:
            list_color.append("k")
    
    return list_color

t = 0

nx.draw_networkx(G,font_color="w",node_color=active_node_coloring(list_timeSeries[t]))

plt.show()

t = 11

nx.draw_networkx(G,font_color="w",node_color=active_node_coloring(list_timeSeries[t]))

plt.show()

python

2022-09-30 19:43

1 Answers

I found out the wrong part.
I mistook the mode of the tool I was using and couldn't find the important indentation difference in Python.
The following functions have different indentation in the middle.

Wrong part of the question source

def simulate_percolation(num, list_active, percent_percolation):
    for i in range (num):
        if list_active[i] == 1:
            for jin range (num):
                 node_name = "Node" + str(j)
            if df_links[node_name].iloc[i] == 1:#### Indentation in this line was shifted and decreased
                    if determine_link (percent_percolation) == 1:
                        list_active[j] = 1
    
    return list_active

Correct Content

def simulate_percolation(num, list_active, percent_percolation):
    for i in range (num):
        if list_active[i] == 1:
            for jin range (num):
                node_name = "Node" + str(j)
                if df_links [node_name].iloc[i] == 1:
                    if determine_link (percent_percolation) == 1:
                        list_active[j] = 1
    return list_active


2022-09-30 19:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.