ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [NetworkX] Assignment 3 - Influence Measures and Network Centralization
    Data & ML & AI/NetworkX 2023. 1. 11. 02:38
    반응형

    본 게시물은 Coursera의 Applied Social Network Analysis in Python(by Daniel Romero)를 통해 자습하며 작성한 게시물입니다.

    이 Assignment는 Coursera Jupyter 콘솔에서만 제대로 진행할 수 있습니다.

    (데이터 파일을 따로 제공하지 않기 때문입니다.)

     

     

    Part1 개괄 원본

    Answer questions 1-4 using the network G1, a network of friendships at a university department. Each node corresponds to a person, and an edge indicates friendship.
    import networkx as nx
    
    G1 = nx.read_gml('assets/friendships.gml')

    개괄 해석 (많은 의역, 생략 및 요약)

    • Q1~4까지는 G1을 사용합니다.
    • G1: 친구관계 네트워크 그래프

    ...

     

    Question 1

    Find the degree centrality, closeness centrality, and betweeness centrality of node 100.
    This function should return a tuple of floats (degree_centrality, closeness_centrality, betweenness_centrality).
    • 노드(100)의 연결중심성, 근접중심성, 매개중심성을 구해라
    • (연결, 근접, 매개) 튜플형태를 return해라
    • 힌트: nx.degree_centrality(), nx.closeness_centrality(), nx.betweenness_centrality()

    해결방법

    def answer_one():
        degCent = nx.degree_centrality(G1)
        closeCent = nx.closeness_centrality(G1)
        btwnCent = nx.betweenness_centrality(G1, normalized=True, endpoints=False)
        
        return (degCent[100], closeCent[100], btwnCent[100])
    
    # 결과확인
    ans_one = answer_one()  # (0.0026501766784452294, 0.2654784240150094, 7.142902633244772e-05)

    Question 2

    Suppose you are employed by an online shopping website and are tasked with selecting one user in network G1 to send an online shopping voucher to. We expect that the user who receives the voucher will send it to their friends in the network. You want the voucher to reach as many nodes as possible. The voucher can be forwarded to multiple users at the same time, but the travel distance of the voucher is limited to one step, which means if the voucher travels more than one step in this network, it is no longer valid. Apply your knowledge in network centrality to select the best candidate for the voucher.
    This function should return an integer, the chosen node.
    • 온라인 쇼핑몰에 고용돼서 G1 네트워크에 바우처를 보내서 광고한다고 생각해봐라
    • 한명한테 바우처를 보내면 그 사람이 자기 친구들한테 보여줄거다
    • 근데 딱 한단계만 보여준다(직접적인 자기 친구들한테만 보여준다)
    • 그럼 누구한테 보내는게 가장 좋을까?
    • integer를 return해라
    • 힌트: nx.degree_centrality()

    해결방법

    def answer_two():
        degCent = nx.degree_centrality(G1)
        sorted_nodes = sorted(degCent.items(), key=lambda x:x[1], reverse=True)
        return sorted_nodes[0][0]
    
    # 결과확인
    ans_two = answer_two()  # 105

    Question 3

    Now the limit of the voucher’s travel distance has been removed. Because the network is connected, regardless of who you pick, every node in the network will eventually receive the voucher. However, we now want to ensure that the voucher reaches nodes as quickly as possible (i.e. in the fewest number of hops). How will you change your selection strategy? Write a function to tell us who is the best candidate in the network under this condition.
    This function should return an integer, the chosen node.
    • 한명한테 바우처를 보내면 그 사람이 자기 친구들한테 보여줄거고, 그 사람은 또 다른 친구들한테 보여줄거다
    • 가능한 빨리, 모든 사람에게 도달하게 하려고 한다. (가장 적은 hop 수로)
    • 그럼 누구한테 보내는게 가장 좋을까?
    • integer를 return해라
    • 힌트: nx.closeness_centrality()

    해결방법

    def answer_three():
        closeCent = nx.closeness_centrality(G1)
        sorted_nodes = sorted(closeCent.items(), key=lambda x:x[1], reverse=True)
        return sorted_nodes[0][0]
    
    # 결과확인
    ans_three = answer_three()  # 23

    Question 4

    Now the limit of the voucher’s travel distance has been removed. Because the network is connected, regardless of who you pick, every node in the network will eventually receive the voucher. However, we now want to ensure that the voucher reaches nodes as quickly as possible (i.e. in the fewest number of hops). How will you change your selection strategy? Write a function to tell us who is the best candidate in the network under this condition.
    This function should return an integer, the chosen node.
    • 경쟁사가 당신을 방해하려고 한다. 네트워크에서 한명을 없애려고한다.
    • 네트워크에서 브릿지(hub) 역할을 하는 사람을 없앨 거라고 하더라
    • 그게 누구일까?
    • integer를 return해라
    • 힌트: nx.betweenness_centrality()

    해결방법

    def answer_four():
        btwnCent = nx.betweenness_centrality(G1, normalized=True, endpoints=False)
        sorted_nodes = sorted(btwnCent.items(), key=lambda x:x[1], reverse=True)
        return sorted_nodes[0][0]
    
    # 결과확인
    ans_four = answer_four()  # 333

     

     

    Part2 개괄 원본

    G2 is a directed network of political blogs, where nodes correspond to a blog and edges correspond to links between blogs. Use your knowledge of PageRank and HITS to answer Questions 5-9.
    G2 = nx.read_gml('assets/blogs.gml')

    개괄 해석 (많은 의역, 생략 및 요약)

    • Q5~9까지는 G2를 사용합니다.
    • G2: 블로그 네트워크 그래프

    ...............

     

    Question 5

    Apply the Scaled Page Rank Algorithm to this network. Find the Page Rank of node 'realclearpolitics.com' with damping value 0.85.
    This function should return a float.
    • 'realclearpolitics.com'의 페이지랭크 값을 구해라 (alpha=0.85=default, 이후 설명 생략)
    • integer를 return해라
    • 힌트: nx.pagerank()

    해결방법

    def answer_five():
        pr = nx.pagerank(G2)
        return pr['realclearpolitics.com']
    
    # 결과확인
    ans_five = answer_five()  # 0.004636694781649098

    Question 6

    Apply the Scaled Page Rank Algorithm to this network with damping value 0.85. Find the 5 nodes with highest Page Rank.
    This function should return a list of the top 5 blogs in desending order of Page Rank.
    • 페이지랭크 값이 높은 블로그 5개를 구해라
    • 순서대로 list를 return해라

    해결방법

    def answer_six():
        pr = nx.pagerank(G2)
        sorted_nodes = sorted(pr.items(), key=lambda x:x[1], reverse=True)
        top_blogs = [blog[0] for blog in sorted_nodes[:5]]
        return top_blogs
    
    # 결과확인
    ans_six = answer_six()
    ['dailykos.com',
     'atrios.blogspot.com',
     'instapundit.com',
     'blogsforbush.com',
     'talkingpointsmemo.com']

    Question 7

    Apply the HITS Algorithm to the network to find the hub and authority scores of node 'realclearpolitics.com'.
    Your result should return a tuple of floats (hub_score, authority_score)
    • HITS알고리즘으로 'realclearpolitics.com'의 hub, authority 값을 구해라
    • (hub, auth) 튜플을 return해라
    • 힌트: nx.hits()

    해결방법

    def answer_seven():
        hub, auth = nx.hits(G2)
        hub_score = hub['realclearpolitics.com']
        auth_score = auth['realclearpolitics.com']
        return (hub_score, auth_score)
    
    # 결과확인
    ans_seven = answer_seven()  # (0.0003243556140278733, 0.003918957644934256)

    Question 8

    Apply the HITS Algorithm to this network to find the 5 nodes with highest hub scores.
    This function should return a list of the top 5 blogs in desending order of hub scores.
    • HITS알고리즘 hub값이 높은 블로그 5개를 구해라
    • 순서대로 list를 return해라

    해결방법

    def answer_eight():
        hub, auth = nx.hits(G2)
        sorted_nodes = sorted(hub.items(), key=lambda x:x[1], reverse=True)
        top_blogs = [blog[0] for blog in sorted_nodes[:5]]
        return top_blogs
    
    # 결과확인
    ans_eight = answer_eight()
    ['politicalstrategy.org',
     'madkane.com/notable.html',
     'liberaloasis.com',
     'stagefour.typepad.com/commonprejudice',
     'bodyandsoul.typepad.com']

    Question 9

    Apply the HITS Algorithm to this network to find the 5 nodes with highest authority scores.
    This function should return a list of the top 5 blogs in desending order of authority scores.
    • HITS알고리즘 authority 값이 높은 블로그 5개를 구해라
    • 순서대로 list를 return해라

    해결방법

    def answer_nine():
        hub, auth = nx.hits(G2)
        sorted_nodes = sorted(auth.items(), key=lambda x:x[1], reverse=True)
        top_blogs = [blog[0] for blog in sorted_nodes[:5]]
        return top_blogs
    
    # 결과확인
    ans_nine = answer_nine()
    ['dailykos.com',
     'talkingpointsmemo.com',
     'atrios.blogspot.com',
     'washingtonmonthly.com',
     'talkleft.com']
    반응형

    댓글

Designed by Tistory.