1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
|
{
"cells": [
{
"cell_type": "markdown",
"id": "2d190a78-7253-4fad-9d9c-6b4fb33c8bf2",
"metadata": {
"tags": []
},
"source": [
"<img src=\"./images/DLI_Header.png\" width=400/>"
]
},
{
"cell_type": "markdown",
"id": "8a2c4abf-6278-4edd-83f8-f0afac4c834f",
"metadata": {},
"source": [
"# Fundamentals of Accelerated Data Science #"
]
},
{
"cell_type": "markdown",
"id": "e1e78ef4-c0de-433e-8616-bd946f69d30e",
"metadata": {},
"source": [
"## 04 - cuGraph as a NetworkX backend ##"
]
},
{
"cell_type": "markdown",
"id": "0828e0b4-7935-4b77-95ef-e06b72f0319e",
"metadata": {},
"source": [
"**Table of Contents**\n",
"<br>\n",
"This notebook introduces the various methods of utilizing the cuGraph backend for NetworkX and runs centrality algorithms on the dataset. This notebook covers the below sections:\n",
"1. [Background](#Background)\n",
"2. [Installation](#Installation)\n",
"3. [Utilizing nx-cugraph](#Utilizing-nx-cugraph)\n",
" * [Runtime Environment Variable](#Runtime-Environment-Variable)\n",
" * [Backend Keyword Argument](#Backend-Keyword-Argument)\n",
" * [Type-Based Dispatching](#Type-Based-Dispatching)\n",
"4. [Computing Centrality](#Computing-Centrality)\n",
" * [Creating Graph](#Creating-Graph)\n",
" * [Running Centrality Algorithms](#Running-Centrality-Algorithms)\n",
" * [Betweenness Centrality](#Betweenness-Centrality)\n",
" * [Degree Centrality](#Degree-Centrality)\n",
" * [Katz Centrality](#Katz-Centrality)\n",
" * [Pagerank Centrality](#Pagerank-Centrality)\n",
" * [Eigenvector Centrality](#Eigenvector-Centrality)\n",
" * [Visualize Results](#Visualize-Results)\n",
" * [Exercise #1 - Type Dispatch](#Exercise-#1---Type-Dispatch)"
]
},
{
"cell_type": "markdown",
"id": "c57b79ba-c7c7-49d2-9e21-c388bbe6ca98",
"metadata": {},
"source": [
"## Background ##\n",
"RAPIDS recently introduced a new backend to NetworkX called nx-cugraph. With this backend, you can automatically accelerate supported algorithms. In this notebook, we will cover the various methods of enabling the cugraph backend, and use the backend to run different centrality algorithms."
]
},
{
"cell_type": "markdown",
"id": "697ea4c9-b416-43d5-9d2c-28aa41ef2561",
"metadata": {},
"source": [
"## Installation ##\n",
"We have already prepared the environment with nx-cugraph installed. When you are using your own environment, below is the command for installation. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8bce324e-ec19-4d87-96e5-bc134ab53baa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Looking in indexes: https://pypi.org/simple, https://pypi.anaconda.org/rapidsai-wheels-nightly/simple\n",
"Requirement already satisfied: nx-cugraph-cu12 in /opt/conda/lib/python3.10/site-packages (25.4.0)\n",
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable.It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.\u001b[0m\u001b[33m\n",
"\u001b[0m"
]
}
],
"source": [
"!pip install nx-cugraph-cu12 --no-deps --extra-index-url https://pypi.anaconda.org/rapidsai-wheels-nightly/simple"
]
},
{
"cell_type": "markdown",
"id": "a9ea09f4-6c93-4785-bcc3-44c6f040dfc6",
"metadata": {},
"source": [
"## Utilizing nx-cugraph ##\n",
"There are 3 ways to utilize nx-cugraph\n",
"\n",
"1. **Environment Variable at Runtime**\n",
"2. **Backend keyword argument**\n",
"3. **Type-Based dispatching**\n",
"\n",
"Let's dig a little deeper in to each of these methods."
]
},
{
"cell_type": "markdown",
"id": "8b4322fd-9f56-4cbc-a00c-8fac4b2b2fe1",
"metadata": {},
"source": [
"### Runtime Environment Variable ###\n",
"The NETWORKX_AUTOMATIC_BACKENDS environment variable can be used to have NetworkX automatically dispatch to specified backends. Set NETWORKX_AUTOMATIC_BACKENDS=cugraph to use nx-cugraph to GPU accelerate supported APIs with no code changes. We will also be loading the cuDF pandas module to accelerate csv loading."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b41fef7f-5d43-4481-98a7-d9f3cb54066c",
"metadata": {},
"outputs": [],
"source": [
"!NETWORKX_AUTOMATIC_BACKENDS=cugraph python -m cudf.pandas scripts/networkx.py"
]
},
{
"cell_type": "markdown",
"id": "5ffb6c4b-a03a-4bfb-9b92-14c59e6dcd75",
"metadata": {},
"source": [
"### Backend Keyword Argument ###\n",
"NetworkX also supports explicitly specifying a particular backend for supported APIs with the backend= keyword argument. This argument takes precedence over the NETWORKX_AUTOMATIC_BACKENDS environment variable. This method also requires that the specified backend already be installed."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8183ecc7-8544-4914-8c07-c904ba12225a",
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"%load_ext cudf.pandas\n",
"import networkx as nx\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Load the CSV file\n",
"road_graph = pd.read_csv('./data/road_graph.csv', dtype=['int32', 'int32', 'float32'], nrows=1000)\n",
"\n",
"# Create an empty graph\n",
"G = nx.from_pandas_edgelist(road_graph, source='src', target='dst', edge_attr='length')\n",
"b = nx.betweenness_centrality(G, k=1000, backend=\"cugraph\")"
]
},
{
"cell_type": "markdown",
"id": "e588aa65-6281-4c19-a51c-42f044636ac0",
"metadata": {},
"source": [
"### Type-Based Dispatching ###\n",
"For users wanting to ensure a particular behavior, without the potential for runtime conversions, NetworkX offers type-based dispatching. To utilize this method, users must import the desired backend and create a Graph instance for it."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5fea9300-8d75-443a-9ec0-ee65c8ccaf0f",
"metadata": {},
"outputs": [],
"source": [
"import networkx as nx\n",
"import nx_cugraph as nxcg\n",
"\n",
"# Loading data from previous cell\n",
"G = nx.from_pandas_edgelist(road_graph, source='src', target='dst', edge_attr='length') \n",
"\n",
"nxcg_G = nxcg.from_networkx(G) # conversion happens once here\n",
"b = nx.betweenness_centrality(nxcg_G, k=1000) # nxcg Graph type causes cugraph backend to be used, no conversion necessary"
]
},
{
"cell_type": "markdown",
"id": "cb5a17e1-d886-4d20-8d4b-ce900280279c",
"metadata": {},
"source": [
"## Computing Centrality ##\n",
"Now that we learned how to enable nx-cugraph, let's try to use it in a workflow! We will be using the backend argument for this example. First let's create a graph."
]
},
{
"cell_type": "markdown",
"id": "19bea37c-bccf-4815-81bd-aa1de553812d",
"metadata": {},
"source": [
"### Creating Graph ###"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2b4420d7-7c89-4914-809f-4e323a12f47f",
"metadata": {},
"outputs": [],
"source": [
"# Create a graph from already loaded dataframe\n",
"G = nx.from_pandas_edgelist(road_graph, source='src', target='dst', edge_attr='length')"
]
},
{
"cell_type": "markdown",
"id": "7dc1ad5b-8454-4277-9568-0cdacbebd9f1",
"metadata": {},
"source": [
"### Running Centrality Algorithms ###\n",
"Now, let's run the various centrality algorithms!"
]
},
{
"cell_type": "markdown",
"id": "1c52b7b3-6c23-45be-9ace-34a667f132aa",
"metadata": {},
"source": [
"### Betweenness Centrality ###\n",
"Quantifies the number of times a node acts as a bridge along the shortest path between two other nodes, highlighting its importance in information flow\n",
"\n",
"For every pair of vertices in a connected graph, there exists at least one shortest path between the vertices, that is, there exists at least one path such that either the number of edges that the path passes through (for unweighted graphs) or the sum of the weights of the edges (for weighted graphs) is minimized.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "281374af-c7cf-4592-a34d-796c1158dab6",
"metadata": {},
"outputs": [],
"source": [
"b = nx.betweenness_centrality(G, backend=\"cugraph\")"
]
},
{
"cell_type": "markdown",
"id": "f98b2975-1f72-4bff-83c7-ace7aab65d98",
"metadata": {},
"source": [
"### Degree Centrality ###\n",
"Measures the number of direct connections a node has, indicating how well-connected it is within the network"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "3e0c4460-6d25-4a2b-8b8f-8f8c6ef617b0",
"metadata": {},
"outputs": [],
"source": [
"d = nx.degree_centrality(G, backend=\"cugraph\")"
]
},
{
"cell_type": "markdown",
"id": "0665a659-16b1-48b4-b3bb-9aa5659ef91c",
"metadata": {},
"source": [
"### Katz Centrality ###\n",
"Measures a node's centrality based on its global influence in the network, considering both direct and indirect connections\n",
"\n",
"\n",
"Katz centrality measures influence by taking into account the total number of walks between a pair of actors"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8ce418d2-9eda-40bc-9733-b82d8d7556b1",
"metadata": {},
"outputs": [],
"source": [
"k = nx.katz_centrality(G, backend=\"cugraph\")"
]
},
{
"cell_type": "markdown",
"id": "0712cedb-87ba-4a08-a74d-24997d02a636",
"metadata": {},
"source": [
"### Pagerank Centrality ###\n",
"Determines a node's importance based on the quantity and quality of links to it, similar to Google's original PageRank algorithm\n",
"\n",
"PageRank’s main difference from EigenCentrality is that it accounts for link direction. Each node in a network is assigned a score based on its number of incoming links (its ‘indegree’). These links are also weighted depending on the relative score of its originating node."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a17ee15b-8758-484b-82b9-a158187231c5",
"metadata": {},
"outputs": [],
"source": [
"p = nx.pagerank(G, max_iter=10, tol=1.0e-3, backend=\"cugraph\")"
]
},
{
"cell_type": "markdown",
"id": "c5f57a5e-95e4-47f7-a9ec-04a99fa2c1dc",
"metadata": {},
"source": [
"### Eigenvector Centrality ###\n",
"Assigns scores to nodes based on the principle that connections to high-scoring nodes contribute more to the node's own score than connections to low-scoring nodes\n",
"\n",
"connections to high-scoring nodes contribute more to the score of the node in question than equal connections to low-scoring nodes. A high eigenvector score means that a node is connected to many nodes who themselves have high scores."
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "3eb1e358-ae8e-4399-bf45-90616b663e9d",
"metadata": {},
"outputs": [],
"source": [
"e = nx.eigenvector_centrality(G, max_iter=1000, tol=1.0e-3, backend=\"cugraph\")"
]
},
{
"cell_type": "markdown",
"id": "0bc9178c-e66a-4c75-bf91-0c5d668b5634",
"metadata": {},
"source": [
"### Visualize Results ###\n",
"Now let's visualize results! We will only display the top 5 rows for readibility. "
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "69b6c23d-78a0-4dbb-be19-913ad180fe94",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style type=\"text/css\">\n",
"</style>\n",
"<table id=\"T_89e5e\" style='display:inline'>\n",
" <caption>Degree</caption>\n",
" <thead>\n",
" <tr>\n",
" <th id=\"T_89e5e_level0_col0\" class=\"col_heading level0 col0\" >vertex</th>\n",
" <th id=\"T_89e5e_level0_col1\" class=\"col_heading level0 col1\" >degree_centrality</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td id=\"T_89e5e_row0_col0\" class=\"data row0 col0\" >24</td>\n",
" <td id=\"T_89e5e_row0_col1\" class=\"data row0 col1\" >0.002847</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_89e5e_row1_col0\" class=\"data row1 col0\" >72</td>\n",
" <td id=\"T_89e5e_row1_col1\" class=\"data row1 col1\" >0.002847</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_89e5e_row2_col0\" class=\"data row2 col0\" >86</td>\n",
" <td id=\"T_89e5e_row2_col1\" class=\"data row2 col1\" >0.002847</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_89e5e_row3_col0\" class=\"data row3 col0\" >127</td>\n",
" <td id=\"T_89e5e_row3_col1\" class=\"data row3 col1\" >0.002847</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_89e5e_row4_col0\" class=\"data row4 col0\" >133</td>\n",
" <td id=\"T_89e5e_row4_col1\" class=\"data row4 col1\" >0.002847</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<style type=\"text/css\">\n",
"</style>\n",
"<table id=\"T_b0826\" style='display:inline'>\n",
" <caption>Betweenness</caption>\n",
" <thead>\n",
" <tr>\n",
" <th id=\"T_b0826_level0_col0\" class=\"col_heading level0 col0\" >vertex</th>\n",
" <th id=\"T_b0826_level0_col1\" class=\"col_heading level0 col1\" >betweenness_centrality</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td id=\"T_b0826_row0_col0\" class=\"data row0 col0\" >222</td>\n",
" <td id=\"T_b0826_row0_col1\" class=\"data row0 col1\" >0.000007</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_b0826_row1_col0\" class=\"data row1 col0\" >381</td>\n",
" <td id=\"T_b0826_row1_col1\" class=\"data row1 col1\" >0.000007</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_b0826_row2_col0\" class=\"data row2 col0\" >24</td>\n",
" <td id=\"T_b0826_row2_col1\" class=\"data row2 col1\" >0.000006</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_b0826_row3_col0\" class=\"data row3 col0\" >72</td>\n",
" <td id=\"T_b0826_row3_col1\" class=\"data row3 col1\" >0.000006</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_b0826_row4_col0\" class=\"data row4 col0\" >86</td>\n",
" <td id=\"T_b0826_row4_col1\" class=\"data row4 col1\" >0.000006</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<style type=\"text/css\">\n",
"</style>\n",
"<table id=\"T_dcc2a\" style='display:inline'>\n",
" <caption>Katz</caption>\n",
" <thead>\n",
" <tr>\n",
" <th id=\"T_dcc2a_level0_col0\" class=\"col_heading level0 col0\" >vertex</th>\n",
" <th id=\"T_dcc2a_level0_col1\" class=\"col_heading level0 col1\" >katz_centrality</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td id=\"T_dcc2a_row0_col0\" class=\"data row0 col0\" >24</td>\n",
" <td id=\"T_dcc2a_row0_col1\" class=\"data row0 col1\" >0.033058</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_dcc2a_row1_col0\" class=\"data row1 col0\" >72</td>\n",
" <td id=\"T_dcc2a_row1_col1\" class=\"data row1 col1\" >0.033058</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_dcc2a_row2_col0\" class=\"data row2 col0\" >86</td>\n",
" <td id=\"T_dcc2a_row2_col1\" class=\"data row2 col1\" >0.033058</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_dcc2a_row3_col0\" class=\"data row3 col0\" >127</td>\n",
" <td id=\"T_dcc2a_row3_col1\" class=\"data row3 col1\" >0.033058</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_dcc2a_row4_col0\" class=\"data row4 col0\" >133</td>\n",
" <td id=\"T_dcc2a_row4_col1\" class=\"data row4 col1\" >0.033058</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<style type=\"text/css\">\n",
"</style>\n",
"<table id=\"T_a76f4\" style='display:inline'>\n",
" <caption>PageRank</caption>\n",
" <thead>\n",
" <tr>\n",
" <th id=\"T_a76f4_level0_col0\" class=\"col_heading level0 col0\" >vertex</th>\n",
" <th id=\"T_a76f4_level0_col1\" class=\"col_heading level0 col1\" >pagerank</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td id=\"T_a76f4_row0_col0\" class=\"data row0 col0\" >24</td>\n",
" <td id=\"T_a76f4_row0_col1\" class=\"data row0 col1\" >0.002525</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_a76f4_row1_col0\" class=\"data row1 col0\" >72</td>\n",
" <td id=\"T_a76f4_row1_col1\" class=\"data row1 col1\" >0.002525</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_a76f4_row2_col0\" class=\"data row2 col0\" >86</td>\n",
" <td id=\"T_a76f4_row2_col1\" class=\"data row2 col1\" >0.002525</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_a76f4_row3_col0\" class=\"data row3 col0\" >127</td>\n",
" <td id=\"T_a76f4_row3_col1\" class=\"data row3 col1\" >0.002525</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_a76f4_row4_col0\" class=\"data row4 col0\" >133</td>\n",
" <td id=\"T_a76f4_row4_col1\" class=\"data row4 col1\" >0.002525</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<style type=\"text/css\">\n",
"</style>\n",
"<table id=\"T_6a3c1\" style='display:inline'>\n",
" <caption>EigenVector</caption>\n",
" <thead>\n",
" <tr>\n",
" <th id=\"T_6a3c1_level0_col0\" class=\"col_heading level0 col0\" >vertex</th>\n",
" <th id=\"T_6a3c1_level0_col1\" class=\"col_heading level0 col1\" >eigenvector_centrality</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td id=\"T_6a3c1_row0_col0\" class=\"data row0 col0\" >24</td>\n",
" <td id=\"T_6a3c1_row0_col1\" class=\"data row0 col1\" >0.064086</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_6a3c1_row1_col0\" class=\"data row1 col0\" >72</td>\n",
" <td id=\"T_6a3c1_row1_col1\" class=\"data row1 col1\" >0.064086</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_6a3c1_row2_col0\" class=\"data row2 col0\" >86</td>\n",
" <td id=\"T_6a3c1_row2_col1\" class=\"data row2 col1\" >0.064086</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_6a3c1_row3_col0\" class=\"data row3 col0\" >127</td>\n",
" <td id=\"T_6a3c1_row3_col1\" class=\"data row3 col1\" >0.064086</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_6a3c1_row4_col0\" class=\"data row4 col0\" >133</td>\n",
" <td id=\"T_6a3c1_row4_col1\" class=\"data row4 col1\" >0.064086</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display_html\n",
"dc_top = pd.DataFrame(sorted(d.items(), key=lambda x:x[1], reverse=True)[:5], columns=[\"vertex\", \"degree_centrality\"])\n",
"bc_top = pd.DataFrame(sorted(b.items(), key=lambda x:x[1], reverse=True)[:5], columns=[\"vertex\", \"betweenness_centrality\"])\n",
"katz_top = pd.DataFrame(sorted(k.items(), key=lambda x:x[1], reverse=True)[:5], columns=[\"vertex\", \"katz_centrality\"])\n",
"pr_top = pd.DataFrame(sorted(p.items(), key=lambda x:x[1], reverse=True)[:5], columns=[\"vertex\", \"pagerank\"])\n",
"ev_top = pd.DataFrame(sorted(e.items(), key=lambda x:x[1], reverse=True)[:5], columns=[\"vertex\", \"eigenvector_centrality\"])\n",
"\n",
"df1_styler = dc_top.style.set_table_attributes(\"style='display:inline'\").set_caption('Degree').hide(axis='index')\n",
"df2_styler = bc_top.style.set_table_attributes(\"style='display:inline'\").set_caption('Betweenness').hide(axis='index')\n",
"df3_styler = katz_top.style.set_table_attributes(\"style='display:inline'\").set_caption('Katz').hide(axis='index')\n",
"df4_styler = pr_top.style.set_table_attributes(\"style='display:inline'\").set_caption('PageRank').hide(axis='index')\n",
"df5_styler = ev_top.style.set_table_attributes(\"style='display:inline'\").set_caption('EigenVector').hide(axis='index')\n",
"\n",
"display_html(df1_styler._repr_html_()+df2_styler._repr_html_()+df3_styler._repr_html_()+df4_styler._repr_html_()+df5_styler._repr_html_(), raw=True)"
]
},
{
"cell_type": "markdown",
"id": "1a653ca9-9448-4ba5-85b2-f6c885c273a9",
"metadata": {},
"source": [
"### Exercise #1 - Type Dispatch ###\n",
"Use the type dispatching method to obtain pagerank centrality results with the cugraph backend."
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "6eb90078-1479-4847-97b7-eb119e9d5478",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>vertex</th>\n",
" <th>pagerank</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>24</td>\n",
" <td>0.002525</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>72</td>\n",
" <td>0.002525</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>86</td>\n",
" <td>0.002525</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>127</td>\n",
" <td>0.002525</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>133</td>\n",
" <td>0.002525</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" vertex pagerank\n",
"0 24 0.002525\n",
"1 72 0.002525\n",
"2 86 0.002525\n",
"3 127 0.002525\n",
"4 133 0.002525"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = nx.pagerank(G, max_iter=10, tol=1.0e-3, backend=\"cugraph\")\n",
"\n",
"pd.DataFrame(sorted(p.items(), key=lambda x:x[1], reverse=True)[:5], columns=[\"vertex\", \"pagerank\"])"
]
},
{
"cell_type": "raw",
"id": "1771c759-fbae-4831-b9b9-663514e460d7",
"metadata": {},
"source": [
"\n",
"import networkx as nx\n",
"import nx_cugraph as nxcg\n",
"\n",
"# Loading data from previous cell\n",
"G = nx.from_pandas_edgelist(road_graph, source='src', target='dst', edge_attr='length') \n",
"\n",
"nxcg_G = nxcg.from_networkx(G) # conversion happens once here\n",
"p = nx.pagerank(nxcg_G, max_iter=10, tol=1.0e-3) # nxcg Graph type causes cugraph backend to be used, no conversion necessary\n",
"\n",
"pd.DataFrame(sorted(p.items(), key=lambda x:x[1], reverse=True)[:5], columns=[\"vertex\", \"pagerank\"])"
]
},
{
"cell_type": "markdown",
"id": "bcdee8d8-c8e0-4521-b7ec-1f85f014e3ca",
"metadata": {},
"source": [
"Click ... for solution. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d70c78b7-551d-4d9e-b428-32b26adcd3c4",
"metadata": {},
"outputs": [],
"source": [
"import IPython\n",
"app = IPython.Application.instance()\n",
"app.kernel.do_shutdown(True)"
]
},
{
"cell_type": "markdown",
"id": "2279fdf1-82c0-4c6e-ac8e-b952f4777562",
"metadata": {},
"source": [
"**Well Done!** "
]
},
{
"cell_type": "markdown",
"id": "3fbc12b2-585c-48a9-a176-b2572040d378",
"metadata": {
"tags": []
},
"source": [
"<img src=\"./images/DLI_Header.png\" width=400/>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|