Question

In: Computer Science

Using Python and networkx library, choose two graphs (one with a small and the other large...

Using Python and networkx library, choose two graphs (one with a small and the other large sample size) from a data network library on the web, and read them using code. Write a script using python that reads the network data and plots the data in to a graph (using networkx).

Solutions

Expert Solution

Sample Data:
import networkx as nx

G=nx.Graph()
G.add_node("a")
G.add_nodes_from(["b","c"])

G.add_edge(1,2)
edge = ("d", "e")
G.add_edge(*edge)
edge = ("a", "b")
G.add_edge(*edge)
# adding a list of edges:
G.add_edges_from([("a","c"),("c","d"), ("a",1), (1,"d"), ("a",2)])
nx.draw(G)
plt.savefig("simple_path.png") # save as png
plt.show() # display

Networked Data:

import csv
from operator import itemgetter
import networkx as nx
from networkx.algorithms import community

# Read in the nodelist file
with open('quakers_nodelist.csv', 'r') as nodecsv:
nodereader = csv.reader(nodecsv)
nodes = [n for n in nodereader][1:]

# Get a list of just the node names (the first item in each row)
node_names = [n[0] for n in nodes]

# Read in the edgelist file
with open('quakers_edgelist.csv', 'r') as edgecsv:
edgereader = csv.reader(edgecsv)
edges = [tuple(e) for e in edgereader][1:]

# Print the number of nodes and edges in our two lists
print(len(node_names))
print(len(edges))

G = nx.Graph() # Initialize a Graph object
G.add_nodes_from(node_names) # Add nodes to the Graph
G.add_edges_from(edges) # Add edges to the Graph
print(nx.info(G))

#attributes

hist_sig_dict = {}
gender_dict = {}
birth_dict = {}
death_dict = {}
id_dict = {}
nx.set_node_attributes(G, hist_sig_dict, 'historical_significance')
nx.set_node_attributes(G, gender_dict, 'gender')
nx.set_node_attributes(G, birth_dict, 'birth_year')
nx.set_node_attributes(G, death_dict, 'death_year')
nx.set_node_attributes(G, id_dict, 'sdfb_id')
for n in G.nodes(): # Loop through every node, in our data "n" will be the name of the person
print(n, G.nodes[n]['birth_year'])


Related Solutions

Write a small program to encrypt and decrypt a message using Python library.
Write a small program to encrypt and decrypt a message using Python library.
Graphs with Matplotlib Using the library Matplotlib and the provided data files create the following graphs:...
Graphs with Matplotlib Using the library Matplotlib and the provided data files create the following graphs: I) Pie chart Create a pie chart that shows the percentage of employees in each department within a company. The provided file: employee_count_by_department.txt contains the data required in order to generate this pie chart. II) Line Graph Create a line graph that shows a company's profit over the past ten years. The provided file: last_ten_year_net_profit.txt contains the data required in order to generate this...
Using two studies (one by DeBondt and Thaler (the law of small numbers) and the other...
Using two studies (one by DeBondt and Thaler (the law of small numbers) and the other by Barberis, Vishny and Shleifer (conservatism) how should you invest in the short-run and the longer-run? Explain why you would use the strategies you describe above.
Why would we choose a cDNA library over a genomic DNA library? In other words, what...
Why would we choose a cDNA library over a genomic DNA library? In other words, what is the difference between the two?
Construct two graphs lined up one on top of the other. the upper graph should reflect...
Construct two graphs lined up one on top of the other. the upper graph should reflect a total cost curve and the bottom graph should reflect the average cost curve and the marginal cost curve. Add a total revenue curve to the upper graph and a marginal revenue curve to the lower graph such that the firm is not making a profit or suffering a loss.
What different graphs are available in QuickBooks Accountant? After discussing the different graphs available, choose two...
What different graphs are available in QuickBooks Accountant? After discussing the different graphs available, choose two graphs to provide in-depth information on their contents? Why are graphs useful in the accounting field and also when making financial decisions?
Gauss Jordan elimination using python for a 2d list (3x3) without using any library. Thanks
Gauss Jordan elimination using python for a 2d list (3x3) without using any library. Thanks
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
You are studying two endangered species – one is a small flowering plant and the other...
You are studying two endangered species – one is a small flowering plant and the other is a rare lizard. Explain what method you would use to estimate the population size of each, and why. What type of population dynamics and growth models would you expect in each of these populations, and why? I'm stuck with is question and it is due tomorrow by 12pm
Illustrate on three demand-and-supply graphs how the size of a tax (small, medium and large) can...
Illustrate on three demand-and-supply graphs how the size of a tax (small, medium and large) can alter total revenue and deadweight loss.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT