Spaces:
Build error
Build error
File size: 33,195 Bytes
0dec8a3 |
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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "46322fb5-5918-4b70-9689-9e0781439ac4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"workding dir: /Users/inflaton/code/engd/papers/maritime/global-incidents\n",
"loading env vars from: /Users/inflaton/code/engd/papers/maritime/global-incidents/.env\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%load_ext autoreload\n",
"%autoreload 2\n",
"\n",
"import os\n",
"import sys\n",
"from pathlib import Path\n",
"\n",
"workding_dir = str(Path.cwd().parent)\n",
"os.chdir(workding_dir)\n",
"sys.path.append(workding_dir)\n",
"print(\"workding dir:\", workding_dir)\n",
"\n",
"from dotenv import find_dotenv, load_dotenv\n",
"\n",
"found_dotenv = find_dotenv(\".env\")\n",
"\n",
"if len(found_dotenv) == 0:\n",
" found_dotenv = find_dotenv(\".env.example\")\n",
"print(f\"loading env vars from: {found_dotenv}\")\n",
"load_dotenv(found_dotenv, override=True)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "daf1e3d1-75ac-4299-8bed-2f413a49f9a6",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import nltk\n",
"from nltk.tokenize import sent_tokenize\n",
"from nltk.tokenize import word_tokenize\n",
"\n",
"import gensim\n",
"from gensim import corpora\n",
"from gensim import similarities\n",
"from gensim import models\n",
"from gensim.models import CoherenceModel\n",
"\n",
"# from wordcloud import WordCloud, ImageColorGenerator\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"import pandas as pd\n",
"import re\n",
"import os\n",
"import datetime\n",
"\n",
"import warnings\n",
"\n",
"warnings.filterwarnings(\"ignore\")\n",
"\n",
"from pprint import pprint\n",
"import pyLDAvis\n",
"import pyLDAvis.gensim_models as gensimvis"
]
},
{
"cell_type": "markdown",
"id": "49e6de6b-71bd-4948-8827-52601406058f",
"metadata": {},
"source": [
"# Import Data with only the news headline and details"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "49222182-7811-4fa6-8c0a-21d3a546863e",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_parquet(\"data/processed_data.parquet\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "3fb59a30",
"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>id</th>\n",
" <th>Headline</th>\n",
" <th>Details</th>\n",
" <th>Severity</th>\n",
" <th>Category</th>\n",
" <th>Region</th>\n",
" <th>Datetime</th>\n",
" <th>Year</th>\n",
" <th>lat</th>\n",
" <th>lon</th>\n",
" <th>maritime_label</th>\n",
" <th>found_ports</th>\n",
" <th>contains_port_info</th>\n",
" <th>if_labeled</th>\n",
" <th>Month</th>\n",
" <th>Week</th>\n",
" <th>Headline_Details</th>\n",
" <th>cleaned_Headline_Details</th>\n",
" <th>binary_Headline_Details</th>\n",
" <th>word_count</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.0</td>\n",
" <td>Grasberg Mine- Grasberg mine workers extend st...</td>\n",
" <td>Media sources indicate that workers at the Gra...</td>\n",
" <td>Moderate</td>\n",
" <td>Mine Workers Strike</td>\n",
" <td>Indonesia</td>\n",
" <td>28/5/17 17:08</td>\n",
" <td>2017.0</td>\n",
" <td>-4.05608</td>\n",
" <td>137.11302</td>\n",
" <td>False</td>\n",
" <td>['freeport']</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>5.0</td>\n",
" <td>21.0</td>\n",
" <td>Grasberg Mine- Grasberg mine workers extend st...</td>\n",
" <td>[grasberg, grasberg, mine, worker, extend, str...</td>\n",
" <td>[worker_grasberg_mine]</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2.0</td>\n",
" <td>Indonesia: Undersea internet cables damaged by...</td>\n",
" <td>News sources are stating that recent typhoons ...</td>\n",
" <td>Minor</td>\n",
" <td>Travel Warning</td>\n",
" <td>Indonesia</td>\n",
" <td>4/9/17 14:30</td>\n",
" <td>2017.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>False</td>\n",
" <td>['hong kong']</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>4.0</td>\n",
" <td>14.0</td>\n",
" <td>Indonesia: Undersea internet cables damaged by...</td>\n",
" <td>[indonesia, undersea, internet, cable, damage,...</td>\n",
" <td>[undersea_internet_cable]</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3.0</td>\n",
" <td>Shanghai port congestion impacts terminals in ...</td>\n",
" <td>The persisting port congestion at Shanghai’s Y...</td>\n",
" <td>Minor</td>\n",
" <td>Port Congestion</td>\n",
" <td>China</td>\n",
" <td>27/4/17 9:16</td>\n",
" <td>2017.0</td>\n",
" <td>29.52000</td>\n",
" <td>121.33190</td>\n",
" <td>True</td>\n",
" <td>['ningbo', 'qingdao', 'shanghai']</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>4.0</td>\n",
" <td>17.0</td>\n",
" <td>Shanghai port congestion impacts terminals in ...</td>\n",
" <td>[shanghai, port, congestion, impact, terminal,...</td>\n",
" <td>[]</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.0</td>\n",
" <td>UPDATE - Indonesia: Explosion at KP Terminal i...</td>\n",
" <td>Updated local media sources from Jakarta indic...</td>\n",
" <td>Extreme</td>\n",
" <td>Bombing, Police Operations</td>\n",
" <td>Indonesia</td>\n",
" <td>24/5/17 15:15</td>\n",
" <td>2017.0</td>\n",
" <td>-6.22465</td>\n",
" <td>106.86700</td>\n",
" <td>True</td>\n",
" <td>['jakarta']</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>5.0</td>\n",
" <td>21.0</td>\n",
" <td>UPDATE - Indonesia: Explosion at KP Terminal i...</td>\n",
" <td>[update, indonesia, explosion, at, kp, termina...</td>\n",
" <td>[]</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>UPDATE - Indonesia: Police confirm two explosi...</td>\n",
" <td>According to local police in Jakarta, two expl...</td>\n",
" <td>Extreme</td>\n",
" <td>Bombing, Police Operations</td>\n",
" <td>Indonesia</td>\n",
" <td>24/5/17 16:20</td>\n",
" <td>2017.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>True</td>\n",
" <td>['jakarta']</td>\n",
" <td>1.0</td>\n",
" <td>True</td>\n",
" <td>5.0</td>\n",
" <td>21.0</td>\n",
" <td>UPDATE - Indonesia: Police confirm two explosi...</td>\n",
" <td>[update, indonesia, police, confirm, two, expl...</td>\n",
" <td>[]</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" id Headline \\\n",
"0 1.0 Grasberg Mine- Grasberg mine workers extend st... \n",
"1 2.0 Indonesia: Undersea internet cables damaged by... \n",
"2 3.0 Shanghai port congestion impacts terminals in ... \n",
"3 4.0 UPDATE - Indonesia: Explosion at KP Terminal i... \n",
"4 5.0 UPDATE - Indonesia: Police confirm two explosi... \n",
"\n",
" Details Severity \\\n",
"0 Media sources indicate that workers at the Gra... Moderate \n",
"1 News sources are stating that recent typhoons ... Minor \n",
"2 The persisting port congestion at Shanghai’s Y... Minor \n",
"3 Updated local media sources from Jakarta indic... Extreme \n",
"4 According to local police in Jakarta, two expl... Extreme \n",
"\n",
" Category Region Datetime Year lat \\\n",
"0 Mine Workers Strike Indonesia 28/5/17 17:08 2017.0 -4.05608 \n",
"1 Travel Warning Indonesia 4/9/17 14:30 2017.0 NaN \n",
"2 Port Congestion China 27/4/17 9:16 2017.0 29.52000 \n",
"3 Bombing, Police Operations Indonesia 24/5/17 15:15 2017.0 -6.22465 \n",
"4 Bombing, Police Operations Indonesia 24/5/17 16:20 2017.0 NaN \n",
"\n",
" lon maritime_label found_ports \\\n",
"0 137.11302 False ['freeport'] \n",
"1 NaN False ['hong kong'] \n",
"2 121.33190 True ['ningbo', 'qingdao', 'shanghai'] \n",
"3 106.86700 True ['jakarta'] \n",
"4 NaN True ['jakarta'] \n",
"\n",
" contains_port_info if_labeled Month Week \\\n",
"0 1.0 False 5.0 21.0 \n",
"1 1.0 False 4.0 14.0 \n",
"2 1.0 False 4.0 17.0 \n",
"3 1.0 False 5.0 21.0 \n",
"4 1.0 True 5.0 21.0 \n",
"\n",
" Headline_Details \\\n",
"0 Grasberg Mine- Grasberg mine workers extend st... \n",
"1 Indonesia: Undersea internet cables damaged by... \n",
"2 Shanghai port congestion impacts terminals in ... \n",
"3 UPDATE - Indonesia: Explosion at KP Terminal i... \n",
"4 UPDATE - Indonesia: Police confirm two explosi... \n",
"\n",
" cleaned_Headline_Details \\\n",
"0 [grasberg, grasberg, mine, worker, extend, str... \n",
"1 [indonesia, undersea, internet, cable, damage,... \n",
"2 [shanghai, port, congestion, impact, terminal,... \n",
"3 [update, indonesia, explosion, at, kp, termina... \n",
"4 [update, indonesia, police, confirm, two, expl... \n",
"\n",
" binary_Headline_Details word_count \n",
"0 [worker_grasberg_mine] 1 \n",
"1 [undersea_internet_cable] 1 \n",
"2 [] 0 \n",
"3 [] 0 \n",
"4 [] 0 "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "09113e88-66cc-414c-a953-da04db83c4ae",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5778, 20)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "be6fc947",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DataFrames are identical: True\n"
]
}
],
"source": [
"df2 = pd.read_parquet(\"IS424_Data_Mining/code/LDA/processed_data.parquet\")\n",
"# Check if two DataFrames are exactly the same\n",
"are_identical = df.equals(df2)\n",
"print(f\"DataFrames are identical: {are_identical}\")"
]
},
{
"cell_type": "markdown",
"id": "037e74fc-bbcd-43e3-8346-799920cca8d8",
"metadata": {},
"source": [
"# Vectorisation"
]
},
{
"cell_type": "markdown",
"id": "d67cef3a-59fb-4dd8-adc8-2cf288b90728",
"metadata": {},
"source": [
"NLP vectorization refers to the process of converting text data into numerical vectors that machine learning algorithms can understand and process. \n",
"\n",
"Bag-of-Words (BoW) is used here that represents text as a collection of unique words along with their frequencies. Each word is assigned an index, and the vector contains the count of each word present in the document."
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "c95b7b8a-9767-469d-812d-c9a9d9fee0e9",
"metadata": {},
"outputs": [],
"source": [
"cleaned = df.copy()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "de71c523-a59e-44b2-aa96-5f17d872c9c6",
"metadata": {},
"outputs": [],
"source": [
"headline = cleaned.binary_Headline_Details"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "5b1e34e1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['worker_grasberg_mine'], dtype=object)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"headline[0]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "677055b4-978e-4253-90f4-3f903662e225",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# vectorise the words\n",
"doc_dict = gensim.corpora.Dictionary(headline)\n",
"docs_vecs = [doc_dict.doc2bow(doc) for doc in headline]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "a54d1768-b069-4936-a156-deaf0b506d93",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of unique tokens: 5319\n",
"Number of articles: 5778\n"
]
}
],
"source": [
"print(\"Number of unique tokens: %d\" % len(doc_dict))\n",
"print(\"Number of articles: %d\" % len(docs_vecs))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "9147fa86-1503-4252-bd9b-92fea1e6a926",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('due_dense_fog', 20),\n",
" ('strong_wind', 19),\n",
" ('indicate_average', 19),\n",
" ('day_port', 17),\n",
" ('san_antonio', 17),\n",
" ('vessel_port', 16),\n",
" ('low_visibility', 15),\n",
" ('average_wait', 13),\n",
" ('port_qingdao', 12),\n",
" ('east_coast_parkway', 12),\n",
" ('port_shanghai', 11),\n",
" ('port_hong_kong', 11),\n",
" ('severe_wind', 11),\n",
" ('blank_week_service', 11),\n",
" ('congestion_port_manila', 10),\n",
" ('wait_hour', 10),\n",
" ('day_situation', 9),\n",
" ('port_ningbo', 9),\n",
" ('coastal_area', 9),\n",
" ('strong_wind_forecast', 9),\n",
" ('vessel_arrival', 8),\n",
" ('high_wind', 8),\n",
" ('pomeranian_voivodeship', 8),\n",
" ('wait_day', 8),\n",
" ('previous_week', 8),\n",
" ('vessel_wait', 8),\n",
" ('average_wait_port', 8),\n",
" ('affect_operation_port', 7),\n",
" ('berth_manila_south', 7),\n",
" ('day_port_saigon', 7),\n",
" ('vessel_port_hong', 7),\n",
" ('vessel_port_shanghai', 7),\n",
" ('high_risk_port', 7),\n",
" ('wait_hour_port', 7),\n",
" ('qianwan_container', 7),\n",
" ('day_port_shanghai', 6),\n",
" ('international_container', 6),\n",
" ('tropical_storm', 6),\n",
" ('arrive_window', 6),\n",
" ('moderate_risk_port', 6),\n",
" ('vessel_port_qingdao', 6),\n",
" ('landside_rail_operation', 6),\n",
" ('dp_world_southampton', 6),\n",
" ('high_yard_density', 6),\n",
" ('vessel_port_ningbo', 6),\n",
" ('port_busan', 6),\n",
" ('strong_wind_area', 6),\n",
" ('new_york_city', 6),\n",
" ('duration_closure', 6),\n",
" ('vessel_berth_port', 5),\n",
" ('wait_decrease_day', 5),\n",
" ('disrupt_operation_port', 5),\n",
" ('waterside_landside_operation', 5),\n",
" ('congest_vessel', 5),\n",
" ('port_charleston', 5),\n",
" ('san_antonio_puerto', 5),\n",
" ('yantian_shipping', 5),\n",
" ('port_saigon', 5),\n",
" ('port_ho_chi', 5),\n",
" ('engine_failure', 5),\n",
" ('port_hong', 5),\n",
" ('dense_fog', 5),\n",
" ('disrupt_port_operation', 5),\n",
" ('high_wind_port', 5),\n",
" ('vessel_wait_day', 5),\n",
" ('vessel_port_tanjung', 5),\n",
" ('landside_waterside_operation', 5),\n",
" ('port_durban', 5),\n",
" ('port_manila', 5),\n",
" ('next_day_morning', 5),\n",
" ('strong_wind_halt', 5),\n",
" ('houston_ship_channel', 5),\n",
" ('low_visibility_forecast', 5),\n",
" ('full_port', 5),\n",
" ('tropical_storm_hanna', 5),\n",
" ('likely_port_closure', 5),\n",
" ('strong_wind_industry', 5),\n",
" ('hour_qingdao_qianwan', 5),\n",
" ('antonio_halt_operation', 5),\n",
" ('high_chance_port', 4),\n",
" ('pilot_service', 4),\n",
" ('low_productivity', 4),\n",
" ('heavy_rain', 4),\n",
" ('resume_operation', 4),\n",
" ('terminal_indicate', 4),\n",
" ('port_fuzhou', 4),\n",
" ('disrupt_port', 4),\n",
" ('nhava_sheva', 4),\n",
" ('berth_day', 4),\n",
" ('congestion_port', 4),\n",
" ('indicate_waterside_operation', 4),\n",
" ('injury_damage_incident', 4),\n",
" ('due_fog', 4),\n",
" ('wait_vessel_port', 4),\n",
" ('kong_industry', 4),\n",
" ('hong_kong', 4),\n",
" ('wait_vessel_berth', 4),\n",
" ('terminal_port', 4),\n",
" ('wait_vessel', 4),\n",
" ('affect_port_operation', 4)]\n"
]
}
],
"source": [
"# Calculate word frequencies\n",
"word_frequencies = {doc_dict[word_id]: freq for word_id, freq in doc_dict.cfs.items()}\n",
"sorted_words = sorted(word_frequencies.items(), key=lambda x: x[1], reverse=True)\n",
"\n",
"pprint(sorted_words[:100])"
]
},
{
"cell_type": "markdown",
"id": "5ed78239-2ce1-4784-a8f4-4c7438c8627b",
"metadata": {},
"source": [
"# LDA Modelling"
]
},
{
"cell_type": "markdown",
"id": "9db83273-461d-4f70-b23f-ec967579d94f",
"metadata": {},
"source": [
"## Benchmark Model"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "e6d577bd-9936-4d45-be90-345af2eb4827",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Build LDA benchmark model\n",
"lda_model = gensim.models.LdaMulticore(\n",
" corpus=docs_vecs,\n",
" id2word=doc_dict,\n",
" num_topics=4,\n",
" random_state=42,\n",
" chunksize=100,\n",
" passes=10,\n",
" per_word_topics=True,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "c4f1521f-5f43-40d2-a3a3-a8ac2ca6fec2",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(0,\n",
" '0.003*\"indicate_average\" + 0.002*\"coastal_area\" + 0.002*\"vessel_port_hong\" '\n",
" '+ 0.002*\"east_coast_parkway\" + 0.002*\"port_charleston\" + '\n",
" '0.002*\"average_wait_port\" + 0.002*\"severe_wind\" + '\n",
" '0.002*\"pomeranian_voivodeship\" + 0.002*\"engine_failure\" + '\n",
" '0.002*\"due_dense_fog\"'),\n",
" (1,\n",
" '0.005*\"san_antonio\" + 0.003*\"blank_week_service\" + 0.003*\"wait_hour\" + '\n",
" '0.003*\"day_port_saigon\" + 0.002*\"low_visibility\" + 0.002*\"high_wind\" + '\n",
" '0.002*\"waterside_landside_operation\" + 0.002*\"tropical_storm\" + '\n",
" '0.002*\"qianwan_container\" + 0.002*\"port_ningbo\"'),\n",
" (2,\n",
" '0.004*\"strong_wind\" + 0.002*\"port_shanghai\" + 0.002*\"port_qingdao\" + '\n",
" '0.002*\"vessel_port_qingdao\" + 0.002*\"day_port_shanghai\" + '\n",
" '0.002*\"indicate_average\" + 0.002*\"disrupt_operation_port\" + '\n",
" '0.002*\"strong_wind_area\" + 0.002*\"port_ho_chi\" + '\n",
" '0.001*\"operation_pier_port\"'),\n",
" (3,\n",
" '0.004*\"port_hong_kong\" + 0.003*\"vessel_port\" + 0.003*\"day_port\" + '\n",
" '0.003*\"congestion_port_manila\" + 0.002*\"berth_manila_south\" + '\n",
" '0.002*\"arrive_window\" + 0.002*\"landside_rail_operation\" + '\n",
" '0.002*\"international_container\" + 0.002*\"day_situation\" + '\n",
" '0.002*\"congestion_port\"')]\n"
]
}
],
"source": [
"from pprint import pprint\n",
"\n",
"# Print the Keyword in the 10 topics\n",
"pprint(lda_model.print_topics())\n",
"doc_lda = lda_model[docs_vecs]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "fd57b1f4-a6cd-41e8-964f-d8a1d30aa3c9",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Coherence Score LDAModel: 0.7011993291597081\n"
]
}
],
"source": [
"# Compute Benchmark Coherence Score\n",
"coherence_model_lda = CoherenceModel(\n",
" model=lda_model, texts=headline, dictionary=doc_dict, coherence=\"c_v\"\n",
")\n",
"coherence_lda = coherence_model_lda.get_coherence()\n",
"print(\"\\nCoherence Score LDAModel: \", coherence_lda)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "152e5a3a-7afe-4fb8-a02f-d7492ad80936",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Perplexity for LDAModel: -9.594271136114548\n"
]
}
],
"source": [
"# Compute Benchmark Perplexity\n",
"perplex = lda_model.log_perplexity(docs_vecs, total_docs=None) # For LDAModel\n",
"# a measure of how good the model is. lower the better.\n",
"\n",
"print(\"\\nPerplexity for LDAModel: \", perplex)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "7dd3a60a-5c6f-4249-9868-30528a5b0ac8",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n",
"/Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/joblib/externals/loky/backend/fork_exec.py:38: DeprecationWarning: This process (pid=30092) is multi-threaded, use of fork() may lead to deadlocks in the child.\n",
" pid = os.fork()\n"
]
}
],
"source": [
"from pprint import pprint\n",
"import pyLDAvis\n",
"import pyLDAvis.gensim_models as gensimvis\n",
"\n",
"# feed the LDA model into the pyLDAvis instance\n",
"pyLDAvis.enable_notebook()\n",
"visual = gensimvis.prepare(lda_model, docs_vecs, doc_dict)\n",
"\n",
"# Save the output to the html file\n",
"pyLDAvis.save_html(visual, \"topic_viz_benchmark.html\")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "48bd84a0-ce9b-4117-bf6e-d9afb9936147",
"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>Topic Keywords</th>\n",
" <th>Topic ID</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.003*\"indicate_average\" + 0.002*\"coastal_area\" + 0.002*\"vessel_port_hong\" + 0.002*\"east_coast_parkway\" + 0.002*\"port_charleston\" + 0.002*\"average_wait_port\"</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.005*\"san_antonio\" + 0.003*\"blank_week_service\" + 0.003*\"wait_hour\" + 0.003*\"day_port_saigon\" + 0.002*\"low_visibility\" + 0.002*\"high_wind\"</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.004*\"strong_wind\" + 0.002*\"port_shanghai\" + 0.002*\"port_qingdao\" + 0.002*\"vessel_port_qingdao\" + 0.002*\"day_port_shanghai\" + 0.002*\"indicate_average\"</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.004*\"port_hong_kong\" + 0.003*\"vessel_port\" + 0.003*\"day_port\" + 0.003*\"congestion_port_manila\" + 0.002*\"berth_manila_south\" + 0.002*\"arrive_window\"</td>\n",
" <td>3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Topic Keywords \\\n",
"0 0.003*\"indicate_average\" + 0.002*\"coastal_area\" + 0.002*\"vessel_port_hong\" + 0.002*\"east_coast_parkway\" + 0.002*\"port_charleston\" + 0.002*\"average_wait_port\" \n",
"1 0.005*\"san_antonio\" + 0.003*\"blank_week_service\" + 0.003*\"wait_hour\" + 0.003*\"day_port_saigon\" + 0.002*\"low_visibility\" + 0.002*\"high_wind\" \n",
"2 0.004*\"strong_wind\" + 0.002*\"port_shanghai\" + 0.002*\"port_qingdao\" + 0.002*\"vessel_port_qingdao\" + 0.002*\"day_port_shanghai\" + 0.002*\"indicate_average\" \n",
"3 0.004*\"port_hong_kong\" + 0.003*\"vessel_port\" + 0.003*\"day_port\" + 0.003*\"congestion_port_manila\" + 0.002*\"berth_manila_south\" + 0.002*\"arrive_window\" \n",
"\n",
" Topic ID \n",
"0 0 \n",
"1 1 \n",
"2 2 \n",
"3 3 "
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.set_option(\"max_colwidth\", 200)\n",
"# Get the topics and their top keywords into a dataframe\n",
"topics = lda_model.show_topics(num_words=6)\n",
"\n",
"topic_keywords = pd.DataFrame()\n",
"for topic_id, topic in topics:\n",
" topic_keywords.at[topic_id, \"Topic Keywords\"] = topic\n",
"\n",
"topic_keywords[\"Topic ID\"] = topic_keywords.index\n",
"# topic_keywords['Topic Name'] = topic_mapping\n",
"topic_keywords"
]
},
{
"cell_type": "markdown",
"id": "3247fe12",
"metadata": {},
"source": [
"## Conclusion\n",
"\n",
"No significant insights gained from the model result as we cannot identify any topics, maybe more data is needed, will deploy web scraping for full news content after mid term."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|