2018年10月17日 星期三

Networkx studying

在瑞士的時候需要做一個關於,感情標籤的分析,那時後腦裡有個想法是「能不能用間接標籤做推理?」

(例如什麼是電影裡的暴力,可以用槍,尖叫等非直接的多媒體做基於邏輯圖的推理,去定義這樣一個主觀標籤)

所以就學了一下 Networkx 去定義圖結點



####Neighbors####
# Define nodes_with_m_nbrs()
def nodes_with_m_nbrs(G,m):
    """
    Returns all nodes in graph G that have m neighbors.
    """
    nodes = set()
 
    # Iterate over all nodes in G
    for n in G.nodes():
 
        # Check if the number of neighbors of n matches m
        if len(G.neighbors(n)) == m:
     
            # Add the node n to the set
            nodes.add(n)
         
    # Return the nodes with m neighbors
    return nodes

# Compute and print all nodes in T that have 6 neighbors
six_nbrs = nodes_with_m_nbrs(T,6)
print(T)

####Degree####

# Compute the degree of every node: degrees
degrees = [len(T.neighbors(n)) for n in T.nodes()]

# Print the degrees
print(degrees)

def find_nodes_with_highest_deg_cent(G):

    # Compute the degree centrality of G: deg_cent
    deg_cent = nx.degree_centrality(G)
 
    # Compute the maximum degree centrality: max_dc
    max_dc = max(list(deg_cent.values()))
 
    nodes = set()
 
    # Iterate over the degree centrality dictionary
    for k, v in deg_cent.items():
 
        # Check if the current value has the maximum degree centrality
        if v == max_dc:
     
            # Add the current node to the set of nodes
            nodes.add(k)
         
    return nodes

# Define find_node_with_highest_bet_cent()
def find_node_with_highest_bet_cent(G):

    # Compute betweenness centrality: bet_cent
    bet_cent = nx.betweenness_centrality(G)
 
    # Compute maximum betweenness centrality: max_bc
    max_bc = max(list(bet_cent.values()))
 
    nodes = set()
 
    # Iterate over the betweenness centrality dictionary
    for k, v in bet_cent.items():
 
        # Check if the current value has the maximum betweenness centrality
        if v == max_bc:
     
            # Add the current node to the set of nodes
            nodes.add(k)
         
    return nodes





沒有留言:

張貼留言