File size: 15,856 Bytes
19b102a |
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 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "AXHLDxJdRzBi"
},
"source": [
"# **BERTopic - Tutorial**\n",
"We start with installing bertopic from pypi before preparing the data. \n",
"\n",
"**NOTE**: Make sure to select a GPU runtime. Otherwise, the model can take quite some time to create the document embeddings!"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Y3VGFZ1USMTu"
},
"source": [
"# **Prepare data**\n",
"For this example, we use the popular 20 Newsgroups dataset which contains roughly 18000 newsgroups posts on 20 topics."
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JJij3WP6SEQD",
"outputId": "6b4d3f7b-9f7f-426f-dea8-ab1e5083eb94"
},
"outputs": [],
"source": [
"from bertopic import BERTopic\n",
"from sklearn.datasets import fetch_20newsgroups\n",
"\n",
"docs = fetch_20newsgroups(subset='all', remove=('headers', 'footers', 'quotes'))['data']"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SBcNmZJzSTY8"
},
"source": [
"# **Create Topics**\n",
"We select the \"english\" as the main language for our documents. If you want a multilingual model that supports 50+ languages, please select \"multilingual\" instead. "
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TfhfzqkoSJ1I",
"outputId": "d51e1f2c-d5db-44b6-d172-58881d54d8e6"
},
"outputs": [],
"source": [
"model = BERTopic(language=\"english\")\n",
"topics, probs = model.fit_transform(docs)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0ua80usww-rj"
},
"source": [
"We can then extract most frequent topics:"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
},
"id": "nNptKBzHSbyS",
"outputId": "25855ec5-d642-4864-cc64-404df61fabc6"
},
"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</th>\n",
" <th>Count</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>-1</td>\n",
" <td>6224</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>1833</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>586</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>526</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>3</td>\n",
" <td>480</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Topic Count\n",
"0 -1 6224\n",
"1 0 1833\n",
"2 1 586\n",
"3 2 526\n",
"4 3 480"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.get_topic_freq().head(5)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-BtOgifV7Q-H"
},
"source": [
"-1 refers to all outliers and should typically be ignored. Next, let's take a look at the most frequent topic that was generated:"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "UCw_q8I7Sb03",
"outputId": "0076f8e0-3355-409f-f3f2-6e3478c6c1ba"
},
"outputs": [
{
"data": {
"text/plain": [
"[('game', 0.010436520264926533),\n",
" ('team', 0.009084781216657336),\n",
" ('games', 0.007234962878545154),\n",
" ('he', 0.007131864903744603),\n",
" ('players', 0.006351685439415695),\n",
" ('season', 0.006282132628187317),\n",
" ('hockey', 0.006157176094578879),\n",
" ('play', 0.005817036121709545),\n",
" ('25', 0.005687199271496157),\n",
" ('year', 0.0056711166453823885)]"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.get_topic(0)[:10]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "u_XSeuhs7bpK"
},
"source": [
"Note that the model is stocastich which mmeans that the topics might differ across runs. \n",
"\n",
"For a full list of support languages, see the values below:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aY3ZkwLE7nvy",
"outputId": "c913a643-5946-4b7d-9ce7-72c1ea7c7fd1"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['afrikaans', 'albanian', 'amharic', 'arabic', 'armenian', 'assamese', 'azerbaijani', 'basque', 'belarusian', 'bengali', 'bengali romanize', 'bosnian', 'breton', 'bulgarian', 'burmese', 'burmese zawgyi font', 'catalan', 'chinese (simplified)', 'chinese (traditional)', 'croatian', 'czech', 'danish', 'dutch', 'english', 'esperanto', 'estonian', 'filipino', 'finnish', 'french', 'galician', 'georgian', 'german', 'greek', 'gujarati', 'hausa', 'hebrew', 'hindi', 'hindi romanize', 'hungarian', 'icelandic', 'indonesian', 'irish', 'italian', 'japanese', 'javanese', 'kannada', 'kazakh', 'khmer', 'korean', 'kurdish (kurmanji)', 'kyrgyz', 'lao', 'latin', 'latvian', 'lithuanian', 'macedonian', 'malagasy', 'malay', 'malayalam', 'marathi', 'mongolian', 'nepali', 'norwegian', 'oriya', 'oromo', 'pashto', 'persian', 'polish', 'portuguese', 'punjabi', 'romanian', 'russian', 'sanskrit', 'scottish gaelic', 'serbian', 'sindhi', 'sinhala', 'slovak', 'slovenian', 'somali', 'spanish', 'sundanese', 'swahili', 'swedish', 'tamil', 'tamil romanize', 'telugu', 'telugu romanize', 'thai', 'turkish', 'ukrainian', 'urdu', 'urdu romanize', 'uyghur', 'uzbek', 'vietnamese', 'welsh', 'western frisian', 'xhosa', 'yiddish']\n"
]
}
],
"source": [
"from bertopic.backend import languages\n",
"print(languages)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attributes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are a number of attributes that you can access after having trained your BERTopic model:\n",
"\n",
"\n",
"| Attribute | Description |\n",
"|------------------------|---------------------------------------------------------------------------------------------|\n",
"| topics_ | The topics that are generated for each document after training or updating the topic model. |\n",
"| probabilities_ | The probabilities that are generated for each document if HDBSCAN is used. |\n",
"| topic_sizes_ | The size of each topic |\n",
"| topic_mapper_ | A class for tracking topics and their mappings anytime they are merged/reduced. |\n",
"| topic_representations_ | The top *n* terms per topic and their respective c-TF-IDF values. |\n",
"| c_tf_idf_ | The topic-term matrix as calculated through c-TF-IDF. |\n",
"| topic_labels_ | The default labels for each topic. |\n",
"| custom_labels_ | Custom labels for each topic as generated through `.set_topic_labels`. |\n",
"| topic_embeddings_ | The embeddings for each topic if `embedding_model` was used. |\n",
"| representative_docs_ | The representative documents for each topic if HDBSCAN is used. |\n",
"\n",
"For example, to access the predicted topics for the first 10 documents, we simply run the following:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, -1, -1, -1, 30, -1, -1, 0, 0, -1]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.topics_[:10]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2eyImbal7lb8"
},
"source": [
"# **Embedding model**\n",
"You can select any model from `sentence-transformers` and use it instead of the preselected models by simply passing the model through \n",
"BERTopic with `embedding_model`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "1dR2ckNK782p"
},
"outputs": [],
"source": [
"# st_model = BERTopic(embedding_model=\"xlm-r-bert-base-nli-stsb-mean-tokens\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SoMc1W-x7-b5"
},
"source": [
"Click [here](https://www.sbert.net/docs/pretrained_models.html) for a list of supported sentence transformers models. \n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "M8c8LenB8Zyl"
},
"source": [
"# **Visualize Topics**\n",
"After having trained our `BERTopic` model, we can iteratively go through perhaps a hundred topic to get a good \n",
"understanding of the topics that were extract. However, that takes quite some time and lacks a global representation. \n",
"Instead, we can visualize the topics that were generated in a way very similar to \n",
"[LDAvis](https://github.com/cpsievert/LDAvis):"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 667
},
"id": "AQKBcla28bY0",
"outputId": "582cefe4-a82c-4915-f2e0-3ad307d3c723"
},
"outputs": [],
"source": [
"model.visualize_topics()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9antKpdC91A-"
},
"source": [
"# **Topic Reduction**\n",
"Finally, we can also reduce the number of topics after having trained a BERTopic model. The advantage of doing so, \n",
"is that you can decide the number of topics after knowing how many are actually created. It is difficult to \n",
"predict before training your model how many topics that are in your documents and how many will be extracted. \n",
"Instead, we can decide afterwards how many topics seems realistic:\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"id": "5m4Nd7Us-Peg"
},
"outputs": [
{
"data": {
"text/plain": [
"<bertopic._bertopic.BERTopic at 0x2c1b7ba8fd0>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.reduce_topics(docs, nr_topics=60)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "c4m3UMsw-Zxk"
},
"source": [
"# **Topic Representation**\n",
"When you have trained a model and viewed the topics and the words that represent them,\n",
"you might not be satisfied with the representation. Perhaps you forgot to remove\n",
"stop_words or you want to try out a different n_gram_range. We can use the function `update_topics` to update \n",
"the topic representation with new parameters for `c-TF-IDF`: \n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"id": "KWm7B-FJ-iYW"
},
"outputs": [],
"source": [
"model.update_topics(docs, n_gram_range=(1, 3))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "LXYJ745O-03Z"
},
"source": [
"# **Search Topics**\n",
"After having trained our model, we can use `find_topics` to search for topics that are similar \n",
"to an input search_term. Here, we are going to be searching for topics that closely relate the \n",
"search term \"gpu\". Then, we extract the most similar topic and check the results: "
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lAdiVYej-2i-",
"outputId": "29d69ab2-1e0d-4561-8cee-e43654e4479e"
},
"outputs": [
{
"data": {
"text/plain": [
"[18, 21, 24, 28, 36]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"similar_topics, similarity = model.find_topics(\"gpu\", top_n=5); similar_topics"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "T-DaaqSA-2nH",
"outputId": "99027647-f939-4f9d-9707-7e2f7dbb7b9a"
},
"outputs": [
{
"data": {
"text/plain": [
"[('monitor', 0.02604954056000538),\n",
" ('the', 0.011592478015427971),\n",
" ('monitors', 0.011386430026081376),\n",
" ('vga', 0.0104858000492717),\n",
" ('video', 0.008950171344439876),\n",
" ('with', 0.008770262226959034),\n",
" ('to', 0.008627947931402424),\n",
" ('it', 0.008211951060402312),\n",
" ('and', 0.00815957039585335),\n",
" ('horizontal', 0.007798501042289411)]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.get_topic(18)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wekNoQNuUVoU"
},
"source": [
"# **Model serialization**\n",
"The model and its internal settings can easily be saved. Note that the documents and embeddings will not be saved. However, UMAP and HDBSCAN will be saved. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "nWUF1uxiSb_a"
},
"outputs": [],
"source": [
"# Save model\n",
"model.save(\"my_model\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "y_eHBI1jSb6i"
},
"outputs": [],
"source": [
"# Load model\n",
"my_model = BERTopic.load(\"my_model\")"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "BERTopic.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "bertopic",
"language": "python",
"name": "bertopic"
},
"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.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|