link_prediction.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. """
  2. Link prediction algorithms.
  3. """
  4. from math import log
  5. import networkx as nx
  6. from networkx.utils import not_implemented_for
  7. __all__ = [
  8. "resource_allocation_index",
  9. "jaccard_coefficient",
  10. "adamic_adar_index",
  11. "preferential_attachment",
  12. "cn_soundarajan_hopcroft",
  13. "ra_index_soundarajan_hopcroft",
  14. "within_inter_cluster",
  15. "common_neighbor_centrality",
  16. ]
  17. def _apply_prediction(G, func, ebunch=None):
  18. """Applies the given function to each edge in the specified iterable
  19. of edges.
  20. `G` is an instance of :class:`networkx.Graph`.
  21. `func` is a function on two inputs, each of which is a node in the
  22. graph. The function can return anything, but it should return a
  23. value representing a prediction of the likelihood of a "link"
  24. joining the two nodes.
  25. `ebunch` is an iterable of pairs of nodes. If not specified, all
  26. non-edges in the graph `G` will be used.
  27. """
  28. if ebunch is None:
  29. ebunch = nx.non_edges(G)
  30. else:
  31. for u, v in ebunch:
  32. if u not in G:
  33. raise nx.NodeNotFound(f"Node {u} not in G.")
  34. if v not in G:
  35. raise nx.NodeNotFound(f"Node {v} not in G.")
  36. return ((u, v, func(u, v)) for u, v in ebunch)
  37. @not_implemented_for("directed")
  38. @not_implemented_for("multigraph")
  39. @nx._dispatchable
  40. def resource_allocation_index(G, ebunch=None):
  41. r"""Compute the resource allocation index of all node pairs in ebunch.
  42. Resource allocation index of `u` and `v` is defined as
  43. .. math::
  44. \sum_{w \in \Gamma(u) \cap \Gamma(v)} \frac{1}{|\Gamma(w)|}
  45. where $\Gamma(u)$ denotes the set of neighbors of $u$.
  46. Parameters
  47. ----------
  48. G : graph
  49. A NetworkX undirected graph.
  50. ebunch : iterable of node pairs, optional (default = None)
  51. Resource allocation index will be computed for each pair of
  52. nodes given in the iterable. The pairs must be given as
  53. 2-tuples (u, v) where u and v are nodes in the graph. If ebunch
  54. is None then all nonexistent edges in the graph will be used.
  55. Default value: None.
  56. Returns
  57. -------
  58. piter : iterator
  59. An iterator of 3-tuples in the form (u, v, p) where (u, v) is a
  60. pair of nodes and p is their resource allocation index.
  61. Raises
  62. ------
  63. NetworkXNotImplemented
  64. If `G` is a `DiGraph`, a `Multigraph` or a `MultiDiGraph`.
  65. NodeNotFound
  66. If `ebunch` has a node that is not in `G`.
  67. Examples
  68. --------
  69. >>> G = nx.complete_graph(5)
  70. >>> preds = nx.resource_allocation_index(G, [(0, 1), (2, 3)])
  71. >>> for u, v, p in preds:
  72. ... print(f"({u}, {v}) -> {p:.8f}")
  73. (0, 1) -> 0.75000000
  74. (2, 3) -> 0.75000000
  75. References
  76. ----------
  77. .. [1] T. Zhou, L. Lu, Y.-C. Zhang.
  78. Predicting missing links via local information.
  79. Eur. Phys. J. B 71 (2009) 623.
  80. https://arxiv.org/pdf/0901.0553.pdf
  81. """
  82. def predict(u, v):
  83. return sum(1 / G.degree(w) for w in nx.common_neighbors(G, u, v))
  84. return _apply_prediction(G, predict, ebunch)
  85. @not_implemented_for("directed")
  86. @not_implemented_for("multigraph")
  87. @nx._dispatchable
  88. def jaccard_coefficient(G, ebunch=None):
  89. r"""Compute the Jaccard coefficient of all node pairs in ebunch.
  90. Jaccard coefficient of nodes `u` and `v` is defined as
  91. .. math::
  92. \frac{|\Gamma(u) \cap \Gamma(v)|}{|\Gamma(u) \cup \Gamma(v)|}
  93. where $\Gamma(u)$ denotes the set of neighbors of $u$.
  94. Parameters
  95. ----------
  96. G : graph
  97. A NetworkX undirected graph.
  98. ebunch : iterable of node pairs, optional (default = None)
  99. Jaccard coefficient will be computed for each pair of nodes
  100. given in the iterable. The pairs must be given as 2-tuples
  101. (u, v) where u and v are nodes in the graph. If ebunch is None
  102. then all nonexistent edges in the graph will be used.
  103. Default value: None.
  104. Returns
  105. -------
  106. piter : iterator
  107. An iterator of 3-tuples in the form (u, v, p) where (u, v) is a
  108. pair of nodes and p is their Jaccard coefficient.
  109. Raises
  110. ------
  111. NetworkXNotImplemented
  112. If `G` is a `DiGraph`, a `Multigraph` or a `MultiDiGraph`.
  113. NodeNotFound
  114. If `ebunch` has a node that is not in `G`.
  115. Examples
  116. --------
  117. >>> G = nx.complete_graph(5)
  118. >>> preds = nx.jaccard_coefficient(G, [(0, 1), (2, 3)])
  119. >>> for u, v, p in preds:
  120. ... print(f"({u}, {v}) -> {p:.8f}")
  121. (0, 1) -> 0.60000000
  122. (2, 3) -> 0.60000000
  123. References
  124. ----------
  125. .. [1] D. Liben-Nowell, J. Kleinberg.
  126. The Link Prediction Problem for Social Networks (2004).
  127. http://www.cs.cornell.edu/home/kleinber/link-pred.pdf
  128. """
  129. def predict(u, v):
  130. union_size = len(set(G[u]) | set(G[v]))
  131. if union_size == 0:
  132. return 0
  133. return len(nx.common_neighbors(G, u, v)) / union_size
  134. return _apply_prediction(G, predict, ebunch)
  135. @not_implemented_for("directed")
  136. @not_implemented_for("multigraph")
  137. @nx._dispatchable
  138. def adamic_adar_index(G, ebunch=None):
  139. r"""Compute the Adamic-Adar index of all node pairs in ebunch.
  140. Adamic-Adar index of `u` and `v` is defined as
  141. .. math::
  142. \sum_{w \in \Gamma(u) \cap \Gamma(v)} \frac{1}{\log |\Gamma(w)|}
  143. where $\Gamma(u)$ denotes the set of neighbors of $u$.
  144. This index leads to zero-division for nodes only connected via self-loops.
  145. It is intended to be used when no self-loops are present.
  146. Parameters
  147. ----------
  148. G : graph
  149. NetworkX undirected graph.
  150. ebunch : iterable of node pairs, optional (default = None)
  151. Adamic-Adar index will be computed for each pair of nodes given
  152. in the iterable. The pairs must be given as 2-tuples (u, v)
  153. where u and v are nodes in the graph. If ebunch is None then all
  154. nonexistent edges in the graph will be used.
  155. Default value: None.
  156. Returns
  157. -------
  158. piter : iterator
  159. An iterator of 3-tuples in the form (u, v, p) where (u, v) is a
  160. pair of nodes and p is their Adamic-Adar index.
  161. Raises
  162. ------
  163. NetworkXNotImplemented
  164. If `G` is a `DiGraph`, a `Multigraph` or a `MultiDiGraph`.
  165. NodeNotFound
  166. If `ebunch` has a node that is not in `G`.
  167. Examples
  168. --------
  169. >>> G = nx.complete_graph(5)
  170. >>> preds = nx.adamic_adar_index(G, [(0, 1), (2, 3)])
  171. >>> for u, v, p in preds:
  172. ... print(f"({u}, {v}) -> {p:.8f}")
  173. (0, 1) -> 2.16404256
  174. (2, 3) -> 2.16404256
  175. References
  176. ----------
  177. .. [1] D. Liben-Nowell, J. Kleinberg.
  178. The Link Prediction Problem for Social Networks (2004).
  179. http://www.cs.cornell.edu/home/kleinber/link-pred.pdf
  180. """
  181. def predict(u, v):
  182. return sum(1 / log(G.degree(w)) for w in nx.common_neighbors(G, u, v))
  183. return _apply_prediction(G, predict, ebunch)
  184. @not_implemented_for("directed")
  185. @not_implemented_for("multigraph")
  186. @nx._dispatchable
  187. def common_neighbor_centrality(G, ebunch=None, alpha=0.8):
  188. r"""Return the CCPA score for each pair of nodes.
  189. Compute the Common Neighbor and Centrality based Parameterized Algorithm(CCPA)
  190. score of all node pairs in ebunch.
  191. CCPA score of `u` and `v` is defined as
  192. .. math::
  193. \alpha \cdot (|\Gamma (u){\cap }^{}\Gamma (v)|)+(1-\alpha )\cdot \frac{N}{{d}_{uv}}
  194. where $\Gamma(u)$ denotes the set of neighbors of $u$, $\Gamma(v)$ denotes the
  195. set of neighbors of $v$, $\alpha$ is parameter varies between [0,1], $N$ denotes
  196. total number of nodes in the Graph and ${d}_{uv}$ denotes shortest distance
  197. between $u$ and $v$.
  198. This algorithm is based on two vital properties of nodes, namely the number
  199. of common neighbors and their centrality. Common neighbor refers to the common
  200. nodes between two nodes. Centrality refers to the prestige that a node enjoys
  201. in a network.
  202. .. seealso::
  203. :func:`common_neighbors`
  204. Parameters
  205. ----------
  206. G : graph
  207. NetworkX undirected graph.
  208. ebunch : iterable of node pairs, optional (default = None)
  209. Preferential attachment score will be computed for each pair of
  210. nodes given in the iterable. The pairs must be given as
  211. 2-tuples (u, v) where u and v are nodes in the graph. If ebunch
  212. is None then all nonexistent edges in the graph will be used.
  213. Default value: None.
  214. alpha : Parameter defined for participation of Common Neighbor
  215. and Centrality Algorithm share. Values for alpha should
  216. normally be between 0 and 1. Default value set to 0.8
  217. because author found better performance at 0.8 for all the
  218. dataset.
  219. Default value: 0.8
  220. Returns
  221. -------
  222. piter : iterator
  223. An iterator of 3-tuples in the form (u, v, p) where (u, v) is a
  224. pair of nodes and p is their Common Neighbor and Centrality based
  225. Parameterized Algorithm(CCPA) score.
  226. Raises
  227. ------
  228. NetworkXNotImplemented
  229. If `G` is a `DiGraph`, a `Multigraph` or a `MultiDiGraph`.
  230. NetworkXAlgorithmError
  231. If self loops exist in `ebunch` or in `G` (if `ebunch` is `None`).
  232. NodeNotFound
  233. If `ebunch` has a node that is not in `G`.
  234. Examples
  235. --------
  236. >>> G = nx.complete_graph(5)
  237. >>> preds = nx.common_neighbor_centrality(G, [(0, 1), (2, 3)])
  238. >>> for u, v, p in preds:
  239. ... print(f"({u}, {v}) -> {p}")
  240. (0, 1) -> 3.4000000000000004
  241. (2, 3) -> 3.4000000000000004
  242. References
  243. ----------
  244. .. [1] Ahmad, I., Akhtar, M.U., Noor, S. et al.
  245. Missing Link Prediction using Common Neighbor and Centrality based Parameterized Algorithm.
  246. Sci Rep 10, 364 (2020).
  247. https://doi.org/10.1038/s41598-019-57304-y
  248. """
  249. # When alpha == 1, the CCPA score simplifies to the number of common neighbors.
  250. if alpha == 1:
  251. def predict(u, v):
  252. if u == v:
  253. raise nx.NetworkXAlgorithmError("Self loops are not supported")
  254. return len(nx.common_neighbors(G, u, v))
  255. else:
  256. spl = dict(nx.shortest_path_length(G))
  257. inf = float("inf")
  258. def predict(u, v):
  259. if u == v:
  260. raise nx.NetworkXAlgorithmError("Self loops are not supported")
  261. path_len = spl[u].get(v, inf)
  262. n_nbrs = len(nx.common_neighbors(G, u, v))
  263. return alpha * n_nbrs + (1 - alpha) * len(G) / path_len
  264. return _apply_prediction(G, predict, ebunch)
  265. @not_implemented_for("directed")
  266. @not_implemented_for("multigraph")
  267. @nx._dispatchable
  268. def preferential_attachment(G, ebunch=None):
  269. r"""Compute the preferential attachment score of all node pairs in ebunch.
  270. Preferential attachment score of `u` and `v` is defined as
  271. .. math::
  272. |\Gamma(u)| |\Gamma(v)|
  273. where $\Gamma(u)$ denotes the set of neighbors of $u$.
  274. Parameters
  275. ----------
  276. G : graph
  277. NetworkX undirected graph.
  278. ebunch : iterable of node pairs, optional (default = None)
  279. Preferential attachment score will be computed for each pair of
  280. nodes given in the iterable. The pairs must be given as
  281. 2-tuples (u, v) where u and v are nodes in the graph. If ebunch
  282. is None then all nonexistent edges in the graph will be used.
  283. Default value: None.
  284. Returns
  285. -------
  286. piter : iterator
  287. An iterator of 3-tuples in the form (u, v, p) where (u, v) is a
  288. pair of nodes and p is their preferential attachment score.
  289. Raises
  290. ------
  291. NetworkXNotImplemented
  292. If `G` is a `DiGraph`, a `Multigraph` or a `MultiDiGraph`.
  293. NodeNotFound
  294. If `ebunch` has a node that is not in `G`.
  295. Examples
  296. --------
  297. >>> G = nx.complete_graph(5)
  298. >>> preds = nx.preferential_attachment(G, [(0, 1), (2, 3)])
  299. >>> for u, v, p in preds:
  300. ... print(f"({u}, {v}) -> {p}")
  301. (0, 1) -> 16
  302. (2, 3) -> 16
  303. References
  304. ----------
  305. .. [1] D. Liben-Nowell, J. Kleinberg.
  306. The Link Prediction Problem for Social Networks (2004).
  307. http://www.cs.cornell.edu/home/kleinber/link-pred.pdf
  308. """
  309. def predict(u, v):
  310. return G.degree(u) * G.degree(v)
  311. return _apply_prediction(G, predict, ebunch)
  312. @not_implemented_for("directed")
  313. @not_implemented_for("multigraph")
  314. @nx._dispatchable(node_attrs="community")
  315. def cn_soundarajan_hopcroft(G, ebunch=None, community="community"):
  316. r"""Count the number of common neighbors of all node pairs in ebunch
  317. using community information.
  318. For two nodes $u$ and $v$, this function computes the number of
  319. common neighbors and bonus one for each common neighbor belonging to
  320. the same community as $u$ and $v$. Mathematically,
  321. .. math::
  322. |\Gamma(u) \cap \Gamma(v)| + \sum_{w \in \Gamma(u) \cap \Gamma(v)} f(w)
  323. where $f(w)$ equals 1 if $w$ belongs to the same community as $u$
  324. and $v$ or 0 otherwise and $\Gamma(u)$ denotes the set of
  325. neighbors of $u$.
  326. Parameters
  327. ----------
  328. G : graph
  329. A NetworkX undirected graph.
  330. ebunch : iterable of node pairs, optional (default = None)
  331. The score will be computed for each pair of nodes given in the
  332. iterable. The pairs must be given as 2-tuples (u, v) where u
  333. and v are nodes in the graph. If ebunch is None then all
  334. nonexistent edges in the graph will be used.
  335. Default value: None.
  336. community : string, optional (default = 'community')
  337. Nodes attribute name containing the community information.
  338. G[u][community] identifies which community u belongs to. Each
  339. node belongs to at most one community. Default value: 'community'.
  340. Returns
  341. -------
  342. piter : iterator
  343. An iterator of 3-tuples in the form (u, v, p) where (u, v) is a
  344. pair of nodes and p is their score.
  345. Raises
  346. ------
  347. NetworkXNotImplemented
  348. If `G` is a `DiGraph`, a `Multigraph` or a `MultiDiGraph`.
  349. NetworkXAlgorithmError
  350. If no community information is available for a node in `ebunch` or in `G` (if `ebunch` is `None`).
  351. NodeNotFound
  352. If `ebunch` has a node that is not in `G`.
  353. Examples
  354. --------
  355. >>> G = nx.path_graph(3)
  356. >>> G.nodes[0]["community"] = 0
  357. >>> G.nodes[1]["community"] = 0
  358. >>> G.nodes[2]["community"] = 0
  359. >>> preds = nx.cn_soundarajan_hopcroft(G, [(0, 2)])
  360. >>> for u, v, p in preds:
  361. ... print(f"({u}, {v}) -> {p}")
  362. (0, 2) -> 2
  363. References
  364. ----------
  365. .. [1] Sucheta Soundarajan and John Hopcroft.
  366. Using community information to improve the precision of link
  367. prediction methods.
  368. In Proceedings of the 21st international conference companion on
  369. World Wide Web (WWW '12 Companion). ACM, New York, NY, USA, 607-608.
  370. http://doi.acm.org/10.1145/2187980.2188150
  371. """
  372. def predict(u, v):
  373. Cu = _community(G, u, community)
  374. Cv = _community(G, v, community)
  375. cnbors = nx.common_neighbors(G, u, v)
  376. neighbors = (
  377. sum(_community(G, w, community) == Cu for w in cnbors) if Cu == Cv else 0
  378. )
  379. return len(cnbors) + neighbors
  380. return _apply_prediction(G, predict, ebunch)
  381. @not_implemented_for("directed")
  382. @not_implemented_for("multigraph")
  383. @nx._dispatchable(node_attrs="community")
  384. def ra_index_soundarajan_hopcroft(G, ebunch=None, community="community"):
  385. r"""Compute the resource allocation index of all node pairs in
  386. ebunch using community information.
  387. For two nodes $u$ and $v$, this function computes the resource
  388. allocation index considering only common neighbors belonging to the
  389. same community as $u$ and $v$. Mathematically,
  390. .. math::
  391. \sum_{w \in \Gamma(u) \cap \Gamma(v)} \frac{f(w)}{|\Gamma(w)|}
  392. where $f(w)$ equals 1 if $w$ belongs to the same community as $u$
  393. and $v$ or 0 otherwise and $\Gamma(u)$ denotes the set of
  394. neighbors of $u$.
  395. Parameters
  396. ----------
  397. G : graph
  398. A NetworkX undirected graph.
  399. ebunch : iterable of node pairs, optional (default = None)
  400. The score will be computed for each pair of nodes given in the
  401. iterable. The pairs must be given as 2-tuples (u, v) where u
  402. and v are nodes in the graph. If ebunch is None then all
  403. nonexistent edges in the graph will be used.
  404. Default value: None.
  405. community : string, optional (default = 'community')
  406. Nodes attribute name containing the community information.
  407. G[u][community] identifies which community u belongs to. Each
  408. node belongs to at most one community. Default value: 'community'.
  409. Returns
  410. -------
  411. piter : iterator
  412. An iterator of 3-tuples in the form (u, v, p) where (u, v) is a
  413. pair of nodes and p is their score.
  414. Raises
  415. ------
  416. NetworkXNotImplemented
  417. If `G` is a `DiGraph`, a `Multigraph` or a `MultiDiGraph`.
  418. NetworkXAlgorithmError
  419. If no community information is available for a node in `ebunch` or in `G` (if `ebunch` is `None`).
  420. NodeNotFound
  421. If `ebunch` has a node that is not in `G`.
  422. Examples
  423. --------
  424. >>> G = nx.Graph()
  425. >>> G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
  426. >>> G.nodes[0]["community"] = 0
  427. >>> G.nodes[1]["community"] = 0
  428. >>> G.nodes[2]["community"] = 1
  429. >>> G.nodes[3]["community"] = 0
  430. >>> preds = nx.ra_index_soundarajan_hopcroft(G, [(0, 3)])
  431. >>> for u, v, p in preds:
  432. ... print(f"({u}, {v}) -> {p:.8f}")
  433. (0, 3) -> 0.50000000
  434. References
  435. ----------
  436. .. [1] Sucheta Soundarajan and John Hopcroft.
  437. Using community information to improve the precision of link
  438. prediction methods.
  439. In Proceedings of the 21st international conference companion on
  440. World Wide Web (WWW '12 Companion). ACM, New York, NY, USA, 607-608.
  441. http://doi.acm.org/10.1145/2187980.2188150
  442. """
  443. def predict(u, v):
  444. Cu = _community(G, u, community)
  445. Cv = _community(G, v, community)
  446. if Cu != Cv:
  447. return 0
  448. cnbors = nx.common_neighbors(G, u, v)
  449. return sum(1 / G.degree(w) for w in cnbors if _community(G, w, community) == Cu)
  450. return _apply_prediction(G, predict, ebunch)
  451. @not_implemented_for("directed")
  452. @not_implemented_for("multigraph")
  453. @nx._dispatchable(node_attrs="community")
  454. def within_inter_cluster(G, ebunch=None, delta=0.001, community="community"):
  455. """Compute the ratio of within- and inter-cluster common neighbors
  456. of all node pairs in ebunch.
  457. For two nodes `u` and `v`, if a common neighbor `w` belongs to the
  458. same community as them, `w` is considered as within-cluster common
  459. neighbor of `u` and `v`. Otherwise, it is considered as
  460. inter-cluster common neighbor of `u` and `v`. The ratio between the
  461. size of the set of within- and inter-cluster common neighbors is
  462. defined as the WIC measure. [1]_
  463. Parameters
  464. ----------
  465. G : graph
  466. A NetworkX undirected graph.
  467. ebunch : iterable of node pairs, optional (default = None)
  468. The WIC measure will be computed for each pair of nodes given in
  469. the iterable. The pairs must be given as 2-tuples (u, v) where
  470. u and v are nodes in the graph. If ebunch is None then all
  471. nonexistent edges in the graph will be used.
  472. Default value: None.
  473. delta : float, optional (default = 0.001)
  474. Value to prevent division by zero in case there is no
  475. inter-cluster common neighbor between two nodes. See [1]_ for
  476. details. Default value: 0.001.
  477. community : string, optional (default = 'community')
  478. Nodes attribute name containing the community information.
  479. G[u][community] identifies which community u belongs to. Each
  480. node belongs to at most one community. Default value: 'community'.
  481. Returns
  482. -------
  483. piter : iterator
  484. An iterator of 3-tuples in the form (u, v, p) where (u, v) is a
  485. pair of nodes and p is their WIC measure.
  486. Raises
  487. ------
  488. NetworkXNotImplemented
  489. If `G` is a `DiGraph`, a `Multigraph` or a `MultiDiGraph`.
  490. NetworkXAlgorithmError
  491. - If `delta` is less than or equal to zero.
  492. - If no community information is available for a node in `ebunch` or in `G` (if `ebunch` is `None`).
  493. NodeNotFound
  494. If `ebunch` has a node that is not in `G`.
  495. Examples
  496. --------
  497. >>> G = nx.Graph()
  498. >>> G.add_edges_from([(0, 1), (0, 2), (0, 3), (1, 4), (2, 4), (3, 4)])
  499. >>> G.nodes[0]["community"] = 0
  500. >>> G.nodes[1]["community"] = 1
  501. >>> G.nodes[2]["community"] = 0
  502. >>> G.nodes[3]["community"] = 0
  503. >>> G.nodes[4]["community"] = 0
  504. >>> preds = nx.within_inter_cluster(G, [(0, 4)])
  505. >>> for u, v, p in preds:
  506. ... print(f"({u}, {v}) -> {p:.8f}")
  507. (0, 4) -> 1.99800200
  508. >>> preds = nx.within_inter_cluster(G, [(0, 4)], delta=0.5)
  509. >>> for u, v, p in preds:
  510. ... print(f"({u}, {v}) -> {p:.8f}")
  511. (0, 4) -> 1.33333333
  512. References
  513. ----------
  514. .. [1] Jorge Carlos Valverde-Rebaza and Alneu de Andrade Lopes.
  515. Link prediction in complex networks based on cluster information.
  516. In Proceedings of the 21st Brazilian conference on Advances in
  517. Artificial Intelligence (SBIA'12)
  518. https://doi.org/10.1007/978-3-642-34459-6_10
  519. """
  520. if delta <= 0:
  521. raise nx.NetworkXAlgorithmError("Delta must be greater than zero")
  522. def predict(u, v):
  523. Cu = _community(G, u, community)
  524. Cv = _community(G, v, community)
  525. if Cu != Cv:
  526. return 0
  527. cnbors = nx.common_neighbors(G, u, v)
  528. within = {w for w in cnbors if _community(G, w, community) == Cu}
  529. inter = cnbors - within
  530. return len(within) / (len(inter) + delta)
  531. return _apply_prediction(G, predict, ebunch)
  532. def _community(G, u, community):
  533. """Get the community of the given node."""
  534. node_u = G.nodes[u]
  535. try:
  536. return node_u[community]
  537. except KeyError as err:
  538. raise nx.NetworkXAlgorithmError(
  539. f"No community information available for Node {u}"
  540. ) from err