<jupyter_start><jupyter_text>0. Data preparationThe following is a network of connections within part of the brain of a fly.<jupyter_code>fly = nx.read_edgelist("bn-fly-drosophila_medulla_1.edges",nodetype=int)
n = fly.number_of_nodes()
print(n,"nodes")<jupyter_output><empty_output><jupyter_text>This network has a feature that we have not allowed so far: some nodes link to themselves. These appear, for example, as nonzeros on the diagonal of the adjacency matrix.<jupyter_code>A = nx.adjacency_matrix(fly).toarray()
print(sum(A[i,i] for i in range(n)),"self-linked nodes")<jupyter_output><empty_output><jupyter_text>We will get rid of these self-links by zeroing out the diagonal and reconstructing the graph from the modified adjacency matrix. This will also relabel the nodes to go in order from 0 to $n-1$.<jupyter_code>np.fill_diagonal(A,0)
fly = nx.from_numpy_array(A)
nodes = pd.Index(fly.nodes)<jupyter_output><empty_output><jupyter_text>1. Basic info  1.1 Compute the average node degree of the network.<jupyter_code>kbar = ...

# No need to change this line.
print("average degree is",kbar)
grader.check("info-kbar")<jupyter_output><empty_output><jupyter_text>1.2 Draw the ego graph of node 80, with labels for the nodes.  1.3 Create a list of all the nodes that are adjacent to node 9.<jupyter_code>nbrs9 = ...

# No need to change the following line.
print("neighbors of node 9:",nbrs9)
grader.check("info-neighbors")<jupyter_output><empty_output><jupyter_text>2. Clustering and distance  2.1Create a Series for the local clustering coefficients in the network.<jupyter_code>cluster = ...

# No need to change the following lines.
print("clustering coeffs:")
print(cluster.describe())
grader.check("cluster-values")<jupyter_output><empty_output><jupyter_text>2.2Compute $T(110)$, the number of triangles that have node 110 as a vertex. Then, apply the formula$$\frac{2T(110)}{d_{110}(d_{110}-1)},$$where $d_{110}$ is the degree of node 110, to get the clustering coefficient of 110.<jupyter_code># Set this to the number of triangles.
T110 = ...

# Set this to the local clustering coefficient.
C110 = ...

# No need to change the following line.
print(T110,"adjacent triangles, giving clustering coefficient",C110)
grader.check("cluster-110")<jupyter_output><empty_output><jupyter_text>2.3Let $k$ be the integer closest to `kbar`. For each $q=0,0.02,0.04,0.06,\ldots$, compute and print out the average clustering coefficient of a Watts–Strogatz graph for $n$, $k$, and $q$, with random seed equal to `SEED`. When the average coefficient is smaller than the average clustering coefficient of the `fly` graph, stop.<jupyter_code># Exit this loop when the average WS clustering exceeds the average clustering in the fly graph.
for q in np.arange(0,0.5,0.02):

# No need to change the following line.
print("final value of q is",q)
grader.check("cluster-WS")<jupyter_output><empty_output><jupyter_text>2.4Compute the mean shortest path length between all pairs of nodes in the largest connected component of the `fly` graph. (This computation might take 20-60 seconds, depending on the speed of the computer you use.)<jupyter_code>avg_dist = ...

# No need to change the following line.
print("average shortest path length:",avg_dist)
grader.check("distance-average")<jupyter_output><empty_output><jupyter_text>3. Degree distribution   3.1Create a Series of the node degrees in the `fly` graph.<jupyter_code>degrees = ...

# No need to change the following line.
print("node degrees for fly graph:\n",degrees.describe())
grader.check("degree-series")<jupyter_output><empty_output><jupyter_text>3.2 Using log scales on both axes, plot a histogram of the degree distribution. You should see a rough linear trend in the resulting plot.  3.4 Create a Barabási–Albert graph with random seed `SEED`, the same number of nodes as the `fly` graph, and parameter $m$ equal to the integer nearest to half the average degree of the `fly` graph. Find the standard deviation of its degree distribution.<jupyter_code># This should be assigned to a Graph object.
BA = ...

# This should get the value of the standard deviation of the degree distribution.
BA_deg_stdev = ...

# No need to change the following lines.
print("BA graph has",len(BA),"nodes and",BA.number_of_edges(),"edges")
print("and node degrees with standard deviation",BA_deg_stdev)
grader.check("degree-BA")<jupyter_output><empty_output><jupyter_text>4. Centrality  4.1Create a data frame indexed by node and with columns named *degree*, *betweenness*, and *eigenvector* for centrality measures.<jupyter_code># This should be a data frame indexed by nodes and with 3 columns for centrality measures. Fidn the mean value in each column.
centrality = ...
# These should be assigned to the means of the three columns.
dc_mean = ...
bc_mean = ...
ec_mean = ...

# No need to change the following lines.
print("centrality scores\n:",centrality)
print("with means",dc_mean,",",bc_mean,",",ec_mean)
grader.check("central-columns")<jupyter_output><empty_output><jupyter_text>4.2Of the three pairings degree-betweenness, degree-eigenvector, and betweenness-eigenvector, find the Pearson correlation coefficient of centrality scores for the pairing that is the *least* strongly correlated.<jupyter_code>min_correlation = ...

# No need to change the following line.
print("weakest correlation is",min_correlation)
grader.check("central-corr")<jupyter_output><empty_output><jupyter_text>4.3For each type of centrality score, find the 10 highest-scoring nodes.<jupyter_code># Each should be an array, list, or Series.
top_degree = ...
top_betweenness = ...
top_eigenvector = ...

# No need to change these lines.
results = pd.DataFrame({"degree":top_degree,"betweenness":top_betweenness,"eigenvector":top_eigenvector})
print(results)
grader.check("central-rank")<jupyter_output><empty_output><jupyter_text>---To double-check your work, the cell below will rerun all of the autograder tests.<jupyter_code>grader.check_all()<jupyter_output><empty_output><jupyter_text>SubmissionMake sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit.Select *Kernel/Restart & Run All*, then save, then run this export cell again. Submit by pushing the resulting zip file to your GitHub assignment repo.<jupyter_code>grader.export(pdf=False, force_save=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10199659495434144, 'fraction_numerical': 0.016251354279523293, 'mean_word_length': 5.696373056994819, 'pattern_counts': {'":': 3, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '351164', 'license': 'MIT', 'n_tokens_mistral': 2079, 'n_tokens_neox': 1969, 'n_words': 708, 'path': 'network/network lab.ipynb', 'repo_name': 'UD-Math-Data-Science-1/labs', 'size': '6461'}