python入门知识图谱_知识图谱之社交网络分析(SNA)之python处理

知识图谱如火如荼,首先推荐在python下进行社交网络分析networkx

建立图网络

无向图

import networkx as nxG = nx.Graph() #建立一个空的无向图GG.add_node(1) #添加一个节点1G.add_edge(2,3) #添加一条边2-3(隐含着添加了两个节点2、3)G.add_edge(3,2) #对于无向图,边3-2与边2-3被认为是一条边print (G.nodes()) #输出全部的节点: [1, 2, 3]print (G.edges()) #输出全部的边:[(2, 3)]print (G.number_of_edges()) #输出边的数量:1[1, 2, 3][(2, 3)]1

有向图

将G = nx.Graph() 改为 G = nx.DiGraph()即进行有向图,表示不同的边

import networkx as nxG = nx.DiGraph() #建立一个空的无向图GG.add_node(1) #添加一个节点1G.add_edge(2,3) #添加一条边2-3(隐含着添加了两个节点2、3)G.add_edge(3,2) #对于无向图,边3-2与边2-3被认为是一条边print (G.nodes()) #输出全部的节点: [1, 2, 3]print (G.edges()) #输出全部的边:[(2, 3), (3, 2)]print (G.number_of_edges()) #输出边的数量:[1, 2, 3][(2, 3), (3, 2)]2

同时,有向图和无向图是可以相互转化的,分别用到Graph.to_undirected() 和 Graph.to_directed()两个方法。

带权图

有向图和无向图都可以给边赋予权重,用到的方法是add_weighted_edges_from,它接受1个或多个三元组[u,v,w]作为参数,其中u是起点,v是终点,w是权重。例如:

G.add_weighted_edges_from([(0,1,3.0),(1,2,7.5)])

添加0-1和1-2两条边,权重分别是3.0和7.5。

如果想读取权重,可以使用get_edge_data方法,它接受两个参数u和v,即边的起讫点。例如:

import networkx as nx

G = nx.DiGraph()

G.add_node(1)

G.add_edge(2,3)

G.add_edge(3,2)

G.add_weighted_edges_from([(0,1,3.0),(1,2,7.5)])

print (G.nodes())

print (G.edges())

print (G.number_of_edges())

print (G.get_edge_data(1,2))

[1, 2, 3, 0]

[(1, 2), (2, 3), (3, 2), (0, 1)]

4

{‘weight’: 7.5}

NetworkX提供了常用的图论经典算法,例如DFS、BFS、最短路、最小生成树、最大流等等

#调用多源最短路径算法,计算图G所有节点间的最短路径

path=dict(nx.all_pairs_shortest_path(G))print (path[0][2])

[0, 1, 2]

一个完整官方小案例

import networkx as nx G = nx.Graph()G.add_edge('A', 'B', weight=4) G.add_edge('B', 'D', weight=2) G.add_edge('A', 'C', weight=3) G.add_edge('C', 'D', weight=4)nx.shortest_path(G, 'A', 'D', weight='weight')

[‘A’, ‘B’, ‘D’]

图数据的保存与绘图

import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()G.add_node(1)G.add_edge(2,3)G.add_edge(3,2)nx.write_edgelist(G, path="grid.edgelist", delimiter=":")# write edgelist to grid.edgelistnx.write_edgelist(G, path="grid.edgelist", delimiter=":")# read edgelist from grid.edgelistH = nx.read_edgelist(path="grid.edgelist", delimiter=":")nx.draw(H)plt.show()

003038i3qphmygq3mm1r69.jpg

数据库基本统计

!usr/bin/env python

* coding:utf-8 *

import matplotlib.pyplot as plt

from networkx import nx

G = nx.lollipop_graph(4, 6)

pathlengths = []

print(“source vertex {target:length, }”)

for v in G.nodes():

spl = dict(nx.single_source_shortest_path_length(G, v))

print(‘{} {} ‘.format(v, spl))

for p in spl:

pathlengths.append(spl[p])

print(”)

print(“average shortest path length %s” % (sum(pathlengths) / len(pathlengths)))

histogram of path lengthsdist = {}for p in pathlengths: if p in dist: dist[p] += 1 else: dist[p] = 1print('')print("length #paths")verts = dist.keys()for d in sorted(verts): print('%s %d' % (d, dist[d]))print("radius: %d" % nx.radius(G))print("diameter: %d" % nx.diameter(G))print("eccentricity: %s" % nx.eccentricity(G))print("center: %s" % nx.center(G))print("periphery: %s" % nx.periphery(G))print("density: %s" % nx.density(G))nx.draw(G, with_labels=True)plt.show()

source vertex {target:length, }

0 {0: 0, 1: 1, 2: 1, 3: 1, 4: 2, 5: 3, 6: 4, 7: 5, 8: 6, 9: 7}

1 {1: 0, 0: 1, 2: 1, 3: 1, 4: 2, 5: 3, 6: 4, 7: 5, 8: 6, 9: 7}

2 {2: 0, 0: 1, 1: 1, 3: 1, 4: 2, 5: 3, 6: 4, 7: 5, 8: 6, 9: 7}

3 {3: 0, 0: 1, 1: 1, 2: 1, 4: 1, 5: 2, 6: 3, 7: 4, 8: 5, 9: 6}

4 {4: 0, 5: 1, 3: 1, 6: 2, 0: 2, 1: 2, 2: 2, 7: 3, 8: 4, 9: 5}

5 {5: 0, 4: 1, 6: 1, 3: 2, 7: 2, 0: 3, 1: 3, 2: 3, 8: 3, 9: 4}

6 {6: 0, 5: 1, 7: 1, 4: 2, 8: 2, 3: 3, 9: 3, 0: 4, 1: 4, 2: 4}

7 {7: 0, 6: 1, 8: 1, 5: 2, 9: 2, 4: 3, 3: 4, 0: 5, 1: 5, 2: 5}

8 {8: 0, 7: 1, 9: 1, 6: 2, 5: 3, 4: 4, 3: 5, 0: 6, 1: 6, 2: 6}

9 {9: 0, 8: 1, 7: 2, 6: 3, 5: 4, 4: 5, 3: 6, 0: 7, 1: 7, 2: 7}

average shortest path length 2.86

length #paths

0 10

1 24

2 16

3 14

4 12

5 10

6 8

7 6

radius: 4

diameter: 7

eccentricity: {0: 7, 1: 7, 2: 7, 3: 6, 4: 5, 5: 4, 6: 4, 7: 5, 8: 6, 9: 7}

center: [5, 6]

periphery: [0, 1, 2, 9]

density: 0.26666666666666666

003038yqivct0epzz1hoo2.jpg

#程序员#

#真相来了#

本文仅代表作者个人观点,不代表SEO研究协会网官方发声,对观点有疑义请先联系作者本人进行修改,若内容非法请联系平台管理员,邮箱cxb5918@163.com。更多相关资讯,请到SEO研究协会网www.seoxiehui.cn学习互联网营销技术请到巨推学院www.jutuiedu.com。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值