Question: Complete the sweep _ louvain _ resolutions function to identify community structures within the graph. Use the Louvain algorithm to find the best partition. Use

Complete the sweep_louvain_resolutions function to identify community structures within the graph. Use the Louvain algorithm to find the best partition. Use 10 different resolution parameters from min_resolution to max_resolution (inclusive), and compare the result of each partition to the ground truth (you can find the ground truth in the value field associated with each node in the graph) using the normalized mutual information function. Return the list of resolutions and NMIs from the resulting community assignments. Additionally, complete the plot_nmi_vs_resolution function to display the NMI for each resolution as a line plot.
When I use normalized_mutual_info_score function I run into this error: Found input variables with inconsistent numbers of samples: [10,9], any guidance?
def sweep_louvain_resolutions(G: nx.Graph, min_resolution: int=1, max_resolution: int=10)-> Tuple[List[int], List[float]]:
"""
Inputs:
G: NetworkX graph object
min_resolution : integer
max_resolution : integer
Returns:
Tuple of list of resolutions and list of NMIs
"""
resolutions =[]
nmis =[]
communities = list(nx.community.louvain_communities(G))
for i in range(min_resolution, max_resolution+1):
res = list(louvain_communities(G, resolution=i))
resolutions.append(i)
nmi_scores = normalized_mutual_info_score(communities, res)
#nmis.append(nmi_scores)
return resolutions, nmis
def plot_nmi_vs_resolution(resolutions: List[int], nmis: List[float], save: bool=False)-> None:
"""
Inputs:
min_resolution : integer
max_resolution : integer
save: boolean
Returns:
None
"""
if save:
plt.savefig('1_2_1.png')
plt.show()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!