US Cities Distribution Network

1.1 Task Description

Nodes: Cities with attributes (1) location, (2) population;

Edges: Connections between cities with weight attribute the cost of traveling;
%matplotlib notebook
import networkx as nx
import matplotlib.pyplot as plt
G = nx.read_gpickle('major_us_cities')
G.nodes(data=True)
[('El Paso, TX', {'location': (-106, 31), 'population': 674433}),
('Long Beach, CA', {'location': (-118, 33), 'population': 469428}),
('Dallas, TX', {'location': (-96, 32), 'population': 1257676}),
('Oakland, CA', {'location': (-122, 37), 'population': 406253}),
('Albuquerque, NM', {'location': (-106, 35), 'population': 556495}),
('Baltimore, MD', {'location': (-76, 39), 'population': 622104}),
('Raleigh, NC', {'location': (-78, 35), 'population': 431746}),
('Mesa, AZ', {'location': (-111, 33), 'population': 457587}),
('Arlington, TX', {'location': (-97, 32), 'population': 379577}),
('Sacramento, CA', {'location': (-121, 38), 'population': 479686}),
('Wichita, KS', {'location': (-97, 37), 'population': 386552}),
('Tucson, AZ', {'location': (-110, 32), 'population': 526116}),
('Cleveland, OH', {'location': (-81, 41), 'population': 390113}),
('Louisville/Jefferson County, KY',
{'location': (-85, 38), 'population': 609893}),
('San Jose, CA', {'location': (-121, 37), 'population': 998537}),
('Oklahoma City, OK', {'location': (-97, 35), 'population': 610613}),
('Atlanta, GA', {'location': (-84, 33), 'population': 447841}),
('New Orleans, LA', {'location': (-90, 29), 'population': 378715}),
('Miami, FL', {'location': (-80, 25), 'population': 417650}),
('Fresno, CA', {'location': (-119, 36), 'population': 509924}),
('Philadelphia, PA', {'location': (-75, 39), 'population': 1553165}),
('Houston, TX', {'location': (-95, 29), 'population': 2195914}),
('Boston, MA', {'location': (-71, 42), 'population': 645966}),
('Kansas City, MO', {'location': (-94, 39), 'population': 467007}),
('San Diego, CA', {'location': (-117, 32), 'population': 1355896}),
('Chicago, IL', {'location': (-87, 41), 'population': 2718782}),
('Charlotte, NC', {'location': (-80, 35), 'population': 792862}),
('Washington D.C.', {'location': (-77, 38), 'population': 646449}),
('San Antonio, TX', {'location': (-98, 29), 'population': 1409019}),
('Phoenix, AZ', {'location': (-112, 33), 'population': 1513367}),
('San Francisco, CA', {'location': (-122, 37), 'population': 837442}),
('Memphis, TN', {'location': (-90, 35), 'population': 653450}),
('Los Angeles, CA', {'location': (-118, 34), 'population': 3884307}),
('New York, NY', {'location': (-74, 40), 'population': 8405837}),
('Denver, CO', {'location': (-104, 39), 'population': 649495}),
('Omaha, NE', {'location': (-95, 41), 'population': 434353}),
('Seattle, WA', {'location': (-122, 47), 'population': 652405}),
('Portland, OR', {'location': (-122, 45), 'population': 609456}),
('Tulsa, OK', {'location': (-95, 36), 'population': 398121}),
('Austin, TX', {'location': (-97, 30), 'population': 885400}),
('Minneapolis, MN', {'location': (-93, 44), 'population': 400070}),
('Colorado Springs, CO', {'location': (-104, 38), 'population': 439886}),
('Fort Worth, TX', {'location': (-97, 32), 'population': 792727}),
('Indianapolis, IN', {'location': (-86, 39), 'population': 843393}),
('Las Vegas, NV', {'location': (-115, 36), 'population': 603488}),
('Detroit, MI', {'location': (-83, 42), 'population': 688701}),
('Nashville-Davidson, TN', {'location': (-86, 36), 'population': 634464}),
('Milwaukee, WI', {'location': (-87, 43), 'population': 599164}),
('Columbus, OH', {'location': (-82, 39), 'population': 822553}),
('Virginia Beach, VA', {'location': (-75, 36), 'population': 448479}),
('Jacksonville, FL', {'location': (-81, 30), 'population': 842583})]

1.2 Create Layouts for Plotting

Dictionary for node positioning methods:

[x for x in nx.__dir__() if x.endswith('_layout')]
['circular_layout',
'random_layout',
'shell_layout',
'spring_layout',
'spectral_layout',
'fruchterman_reingold_layout']

1.2.1 Spring Layout (default) Node Positioning: (1) As few crossing edges as possible; (2) Keep edge length similar.

plt.figure(figsize=(10,9))
nx.draw_networkx(G)

1.2.2 Random Layout

plt.figure(figsize=(10,9))
pos = nx.random_layout(G)
nx.draw_networkx(G, pos)

1.2.3 Cicular Layout

plt.figure(figsize=(10,9))
pos = nx.circular_layout(G)
nx.draw_networkx(G, pos)

1.2.4 Custom Layout

plt.figure(figsize=(10,7))
pos = nx.get_node_attributes(G, 'location')
nx.draw_networkx(G, pos)

plt.figure(figsize=(10,7))
nx.draw_networkx(G, pos, alpha=0.7, with_labels=False, edge_color='.4')
plt.axis('off')
plt.tight_layout();

Set size of nodes based on population, multiply pop with small number so plots won't be large.

Get weights of transportation costs and pass it to edges.

plt.figure(figsize=(10,7))
node_color = [G.degree(v) for v in G]
node_size = [0.0005 * nx.get_node_attributes(G, 'population')[v] for v in G]
edge_width = [0.0015*G[u][v]['weight'] for u,v in G.edges()]
nx.draw_networkx(G, pos, node_size=node_size,
node_color=node_color, alpha=0.7, with_labels=False,
width=edge_width, edge_color='.4', cmap=plt.cm.Blues)
plt.axis('off')
plt.tight_layout();

Display the most expensive costs, i.e., separately add specific labels and edges.

plt.figure(figsize=(10,7))
node_color = [G.degree(v) for v in G]
node_size = [0.0005*nx.get_node_attributes(G, 'population')[v] for v in G]
edge_width = [0.0015*G[u][v]['weight'] for u,v in G.edges()]
nx.draw_networkx(G, pos, node_size=node_size,
node_color=node_color, alpha=0.7, with_labels=False,
width=edge_width, edge_color='.4', cmap=plt.cm.Blues)
greater_than_770 = [x for x in G.edges(data=True) if x[2]['weight']>770]
nx.draw_networkx_edges(G, pos, edgelist=greater_than_770, edge_color='r', alpha=0.4, width=6)
nx.draw_networkx_labels(G, pos, labels={'Los Angeles, CA': 'LA', 'New York, NY': 'NYC'}, font_size=18, font_color='w')
plt.axis('off')
plt.tight_layout();

1.3 Degree Distribution

Probability distributions over entire network

# function degree() returns a dictionary with keys being nodes and
# values being degrees of nodes
degrees = G.degree()
degree_values = sorted(set(degrees.values()))
histogram = [list(degrees.values()).count(i)/float(nx.number_of_nodes(G)) for i in degree_values]
import matplotlib.pyplot as plt
plt.bar(degree_values, histogram)
plt.xlabel('Degree')
plt.ylabel('Fraction of Nodes')
plt.show()

1.4 Extracting Attributes

1.4.1 Node-based Method

Transform into DataFrame columns, initialize the dataframe, using the nodes as the index:

df = pd.DataFrame(index = G.nodes())
df['location'] = pd.Series(nx.get_node_attributes(G, 'location'))
df['population'] = pd.Series(nx.get_node_attributes(G, 'population'))
df.head()

Add features:

df['clustering'] = pd.Series(nx.clustering(G))
df['degree'] = pd.Series(G.degree())
df

1.4.2 Edge-based Features

Initialize the DataFrame, using the edges as the index:

G.edges(data=True)
df = pd.DataFrame(index=G.edges())
df['weight'] = pd.Series(nx.get_edge_attributes(G, 'weight'))
df

df['preferential attachment'] = [i[2] for i in nx.preferential_attachment(G, df.index)]
df['Common Neighbors'] = df.index.map(lambda city: len(list(nx.common_neighbors(G, city[0], city[1]))))
df

Link Analysis_2_Application的更多相关文章

  1. oracle db link的查看创建与删除

    1.查看dblink select owner,object_name from dba_objects where object_type='DATABASE LINK'; 或者 select * ...

  2. 解决Java程序连接mysql数据库出现CommunicationsException: Communications link failure错误的问题

    一.背景 最近在家里捣鼓一个公司自己搭建的demo的时候,发现程序一启动就会出现CommunicationsException: Communications link failure错误,经过一番排 ...

  3. 解决绝对定位div position: absolute 后面的<a> Link不能点击

    今天布局的时候,遇到一个bug,当DIV设置为绝对定位时,这个div后面的相对定位的层里面的<a>Link标签无法点击. 网上的解决方案是在绝对定位层里面添加:pointer-events ...

  4. LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    同时安装了VS2012和VS2010,用VS2010 时 >LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 问题说明:当安装VS2012之后 ...

  5. VS2013的 Browser Link 引起的问题

    环境:vs2013 问题:在调用一个WebApi的时候出现了错误: 于是我用Fiddler 4直接调用这个WebApi,状态码是200(正常的),JSon里却提示在位置9409处文本非法, 以Text ...

  6. angular中的compile和link函数

    angular中的compile和link函数 前言 这篇文章,我们将通过一个实例来了解 Angular 的 directives (指令)是如何处理的.Angular 是如何在 HTML 中找到这些 ...

  7. AngularJS之指令中controller与link(十二)

    前言 在指令中存在controller和link属性,对这二者心生有点疑问,于是找了资料学习下. 话题 首先我们来看看代码再来分析分析. 第一次尝试 页面: <custom-directive& ...

  8. Visual Studio 2013中因SignalR的Browser Link引起的Javascript错误一则

    众所周知Visual Studio 2013中有一个由SignalR机制实现的Browser Link功能,意思是开发人员可以同时使用多个浏览器进行调试,当按下IDE中的Browser Link按钮后 ...

  9. link与@import的区别

    我们都知道link与@import都可以引入css样式表,那么这两种的区别是什么呢?先说说它们各自的链接方式,然后说说它们的区别~~~ link链入的方式: <link rel="st ...

随机推荐

  1. QLabel设置伙伴关系和快捷键(Alt+首字母)

    Qlabe中设置伙伴关系是使用Setbuddy函数: Qlabel.Setbuddy(QLineEdit) #将Qlabel和QLineEdit之间设置伙伴关系 另外,需要配合热键(快捷键)进行使用, ...

  2. Kubernetes的控制器之Deployment的定义

    Deploy 的控制器定义参数介绍 [root@master manifests]# kubectl explain deploy KIND: Deployment VERSION: extensio ...

  3. Spring学习(八)

    AOP的重要概念 1.切面 : 切点(Pointcut) + Advice[ 在哪里 .加什么 ] 2.Advice: 在 切点 选中的 连接点 "加入" 的 代码 就是 Advi ...

  4. UDLD(Unidirectional Link Detection)

    1.UDLD(单向链路检测协议)工作原理          为了在生成转发环路之前检测到单向链路,Cisco 设计并实施了 UDLD 协议.UDLD 是与第 1 层 (L1) 机制一起工作以确定链路物 ...

  5. MAVLINK协议

    参考https://cloud.tencent.com/developer/news/48344 https://www.cnblogs.com/lovechen/p/5801679.html htt ...

  6. Dubbo 18 问

    dubbo是什么 dubbo是一个分布式框架,远程服务调用的分布式框架,其核心部分包含: 集群容错:提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集 ...

  7. (转)notepad++去重

    notepad++  真是强大,几乎你能想到的处理文本方法都可以用它来实现,因为他有强大的插件团! 例如1:去除重复行 先安装TextFx插件 在菜单TextFX-->TextFX Tools下 ...

  8. linux#自启动脚本

    编写脚本: /etc/init.d/myscriptname # chkconfig: # description: 描述信息,描述信息,上面的90表示在众多开机启动脚本的优先级,10表示在众多关机启 ...

  9. rhel7 系统服务——unit(单元)

    Linux内核版本从3.10后开始使用systemd管理服务,这也是系统开机后的第一个服务.systemd通过unit单元文件来管理服务. 它保存了服务.设备.挂载点和操作系统其他信息的配置文件,并能 ...

  10. Write-up-Bulldog2

    关于 下载地址:点我 哔哩哔哩:哔哩哔哩 信息收集 网卡:vboxnet0,192.168.56.1/24,Nmap扫存活主机发现IP为192.168.56.101 ➜ ~ nmap -sn 192. ...