हर दिन के सबसे लोकप्रिय फ़ोरम ढूँढें: II
पिछले अभ्यास में बढ़िया काम किया — आपने time-series ग्राफ़ लिस्ट बनाने वाला कोड लिखा था. अब आप उसी अभ्यास को पूरा करेंगे — यानी, आप यह निकालेंगे कि प्रति दिन कितने फ़ोरम का सबसे लोकप्रिय फ़ोरम स्कोर था!
यहाँ आप जिन चीज़ों में से एक करेंगे, वह है किसी dictionary को फ़िल्टर करने के लिए "dictionary comprehension". यह किसी list को फ़िल्टर करने वाली list comprehension जैसा ही है, बस सिंटैक्स इस तरह दिखता है: {key: val for key, val in dict.items() if ...}. इसे ध्यान में रखिए!
यह अभ्यास पाठ्यक्रम का हिस्सा है
इंटरमीडिएट Network Analysis in Python
अभ्यास निर्देश
nx.bipartite.degree_centrality()का उपयोग करके degree centrality निकालें, और आर्ग्युमेंट के रूप मेंG_subऔरforum_nodesदें.- dictionary को ऐसे फ़िल्टर करें कि केवल फ़ोरम की degree centralities रहें. आउटपुट अभिव्यक्ति में
key: valजोड़ीn, dcहोनी चाहिए.dc.items()पर इटरेट करें और जाँचें किnforum_nodesमें है. - सबसे लोकप्रिय फ़ोरम/फ़ोरम्स को पहचानें — जिनकी degree centrality सबसे अधिक हो (
max(forum_dcs.values())) और जिनका DC मान शून्य न हो. - सबसे बड़े
dcमानों कोhighest_dcsमें append करें. - plots बनाएँ!
- पहले plot के लिए list comprehension का उपयोग करें, जिसमें आप
most_popular_forums(जो कि lists की list है) परforumsको iterator वैरिएबल बनाकर इटरेट करें. आउटपुट अभिव्यक्ति सबसे लोकप्रिय फ़ोरम्स की संख्या होनी चाहिए, जिसेlen()से निकालें. - दूसरे plot के लिए,
highest_dcsऔरplt.plot()का उपयोग करके शीर्ष degree centrality स्कोर को visualize करें.
- पहले plot के लिए list comprehension का उपयोग करें, जिसमें आप
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Import necessary modules
from datetime import timedelta
import networkx as nx
import matplotlib.pyplot as plt
most_popular_forums = []
highest_dcs = []
curr_day = dayone
td = timedelta(days=1)
while curr_day < lastday:
if curr_day.day == 1:
print(curr_day)
G_sub = nx.Graph()
G_sub.add_nodes_from(G.nodes(data=True))
G_sub.add_edges_from([(u, v, d) for u, v, d in G.edges(data=True) if d['date'] >= curr_day and d['date'] < curr_day + td])
# Get the degree centrality
dc = ____
# Filter the dictionary such that there's only forum degree centralities
forum_dcs = {____:____ for ____, ____ in ____ if n in ____}
# Identify the most popular forum(s)
most_popular_forum = [n for n, dc in ____ if dc == ____(____) and dc != 0]
most_popular_forums.append(most_popular_forum)
# Store the highest dc values in highest_dcs
highest_dcs.append(max(____))
curr_day += td
plt.figure(1)
plt.plot([len(____) for ____ in ____], color='blue', label='Forums')
plt.ylabel('Number of Most Popular Forums')
plt.show()
plt.figure(2)
plt.plot(____, color='orange', label='DC Score')
plt.ylabel('Top Degree Centrality Score')
plt.show()