API Reference
EHDBSCAN
Exhaustive HDBSCAN performs iterative clustering. The noise points left over from each iteration are remapped to a new density structure. At each step the clusters from the current iteration are assigned parents from the previous iteration. This results in a cluster of linked clusters.
This implementation uses HDBSCAN from the sci-kit library.
Overall, this lib relies heavily on sci-kit.
Initialize the EHDBSCAN to perform iterative clustering.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Attributes: |
|
|---|
Source code in src\exhaustive_hdbscan\ehdbscan.py
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 | |
fit(X, y=None)
The fit method, performs the iterative clustering.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\ehdbscan.py
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 | |
approx_predict(X)
Approximate prediction will assign unseen data to existing clusters.
HDBSCAN is not built for predictions, so this is not a perfect transform, only an approximate prediction.
It expects data to be passed in the same format as the fit method. But if text was passed to fit, it will still work with embeddings.
This method will not work with pairwise distances where metric is precomputed.
If EHDBSCAN fit was executed with pairwise distances, this method will not work, even if text or embeddings are passed for prediction.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\ehdbscan.py
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 | |
plot_tree(cluster_features=None, labeler=None, figsize=(20, 10), horizontal_spacing=3, vertical_spacing=1, ax=None, label_font_size=10, label_box_dim=100, label_box_style='round,pad=0.3', label_box_color='aliceblue', label_box_border_color='black', label_box_border_width=1)
It plots the tree of clusters with parent-child links.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\ehdbscan.py
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 | |
ClusterFeatures
The data layer for the EHDBSCAN. It provides storage and easy access of clusters, inputs, distances and embeddings.
The ClusterFeatures object is accessible as an attribute from the EHDBSCAN object after the fit is called. It is returned by the approx_predict method as a separate instance.
It is not intended for populating manually. Storage are automatically encoded with designated name where names are sequentially automatically generated.
Naming convention will assign names as {cluster type}_{iteration index}. For example second iteration will designate the generated labels as "parent_1" and the assigned child labels as "child_0". Here "parent_1" is the current iteration label and "child_0" is the child of the previous iteration "parent_0".
Note that any child labels will have the same unique indexes as its parent indicating which parent that child belongs to.
| Attributes: |
|
|---|
Source code in src\exhaustive_hdbscan\clusterfeatures.py
38 39 40 41 42 43 44 45 46 47 | |
get_parent(name=None, label=None, get_ids=None)
Get the label of parent by name, label and index ID.
If neither is specified, all labels are returned.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\clusterfeatures.py
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 | |
get_child(name=None, label=None, get_ids=None)
Get the label of child by name, label and index ID.
If neither is specified, all labels are returned.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\clusterfeatures.py
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 | |
get_input(name=None, label=None, get_ids=None)
Get the input text by parent or child name, label and index ID.
If neither is specified, all input is returned.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\clusterfeatures.py
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 | |
get_distances(name=None, label=None, get_ids=None, row_ids=None, col_ids=None)
Get the pairwise distances by parent or child name, label and index ID.
If neither is specified, all distances are returned.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\clusterfeatures.py
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 | |
get_embeddings(name=None, label=None, get_ids=None)
Get the text embeddings by parent or child name, label and index ID.
If neither is specified, all input is returned.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\clusterfeatures.py
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 | |
get_reduce_embeddings(name=None, label=None, get_ids=None)
Get the dimension reduction embeddings by parent or child name, label and index ID.
If neither is specified, all input is returned.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\clusterfeatures.py
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 | |
get_pandas_data()
Get all data as a Pandas dataframe.
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\clusterfeatures.py
528 529 530 531 532 533 534 535 536 537 | |
Utilities
Reducer
Dimension reduction method passed to EHDBSCAN must subclass this Reducer class.
Source code in src\exhaustive_hdbscan\utilities.py
73 74 75 76 77 | |
fit(X)
All subclasses must implement this method.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\utilities.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
transform(X)
All subclasses must implement this method and return an array-like object of shape (n_samples, n_features).
EHDBSCAN will call fit and transform separately. A fit_transform may be written for personal use but is not required.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\utilities.py
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 | |
Encoder
Encoder passed to the EHDBSCAN must subclass this Encoder class.
Source code in src\exhaustive_hdbscan\utilities.py
48 49 50 51 52 | |
encode(X)
All subclasses must implement the encode method.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\utilities.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
ClusterVectorOps
This performs some basic vector operations that are useful for interpreting EHDBSCAN outputs.
They are just raw vector operations. However it can be combined with LabelGeneratorConfig to generate labels for drawing the tree of clusters.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\clustervectorops.py
38 39 40 41 42 43 44 | |
cluster_centroid_neighbors(top_n=3, name=None, label=None, metric=None)
This will generate the neighbors for a parent or child cluster centroid, where the centroid is taken as the median.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\clustervectorops.py
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 | |
parent_child_transition(*, parent, child, top_n=3, metric=None)
In EHDBSCAN, parent-child links are based on core distance-type metric that relies on cluster centroids. Therefore, it is valuable to find out what was the common topical link between the two clusters. It helps interpreting the cluster tree.
Similarly it is also helpful to see, which topics are unique to the parent and the child. In experiments, it has been observed that topics unique to the child can be seen as the topical drift subtracted from the parent (parent_median - child_median) and vice versa.
For instance, if the parent cluster's central theme is Policy and Law and child cluster's theme is Malicious practices in the Law profession, the common link between the two is Law & Legal. The topical drift is Ethics, this is the difference unique to the child and will be reflected in the child IDs returned by this method.
This method performs these operations to return IDs unique to the parent and child and IDs common among the two.
It works with embeddings and distances, but it is highly recommended to use embeddings. The only distance estimations are simplification and not entirely representative. A lot of accuracy is lost in trying to work back differences in absolute positions with relative position metrics like distances.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\clustervectorops.py
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 | |
Label Generation
LabelGeneratorConfig
The base class for generating labels compatible with EHDBSCAN.
Source code in src\exhaustive_hdbscan\labelgenerator.py
20 21 | |
compute(cluster_features)
All subclasses must implement this method.
It expects an instance of ClusterFeatures and returns a list of labels in the same sequence as the clustering operation.
Cluster Iteration > Each Cluster in ascending sequence [0, 1, 2...].
Returned list must be single level, not nested. This is must for compatibility with EHDBSCAN.
See examples for implementation.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\labelgenerator.py
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 | |
MMR
Bases: LabelGeneratorConfig
Maximal Marginal Relevance implementation subclassed to LabelGeneratorConfig. It filters the labels for relevance and diversity.
This is a generalized standalone implementation and will work without EHDBSCAN inputs. But the compute method will only work with an instance of ClusterFeatures. Fit and transform may still be called with raw inputs.
| Parameters: |
|
|---|
| Attributes: |
|
|---|
Source code in src\exhaustive_hdbscan\labelgenerator.py
96 97 98 99 100 101 102 103 104 | |
fit(X)
Creates the similarity matrix from inputs. Expects text input or numeric embeddings.
X : {array-like} of shape (n_samples,) for text OR (n_samples, n_features) for embeddings Expects text or numeric embeddings. If text is provided, an Encoder must also be specified.
Source code in src\exhaustive_hdbscan\labelgenerator.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
transform(y)
Get the MMR labels.
y : {array-like} of shape (n_sample,) for text OR (n_sample, n_features) for embeddings Expects text or numeric embeddings. If text is provided, an Encoder must also be specified. It expects exactly one entry.
Source code in src\exhaustive_hdbscan\labelgenerator.py
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 | |
compute(cluster_feats)
Performs the fit and transforms the input labels as MMR labels and outputs list of labels compatible with EHDBSCAN. It expects a ClusterFeatures input.
For ranking, the entire cluster text is clubbed and encoded to compare against individual items in the cluster. If an encoder is provided, the combined cluster text is encoded with this encoder. If an encoder is not provided, the average embeddings of individual cluster items is used.
If the ClusterFeatures instance provided has no embeddings, an encoder must be provided.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\labelgenerator.py
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 | |
ClassTfidf
Bases: LabelGeneratorConfig
Class Term Frequency Inverse Document Frequency implementation subclassed to LabelGeneratorConfig. It assigns descriptive labels for each cluster.
This is a generalized standalone implementation and will work without EHDBSCAN inputs. But the compute method will only work with an instance of ClusterFeatures. Fit and transform may still be called with raw inputs.
It uses the sci-kit CountVectorizer and some of those parameters are exposed here.
| Parameters: |
|
|---|
| Attributes: |
|
|---|
Source code in src\exhaustive_hdbscan\labelgenerator.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
fit(X)
Fits a corpus of docs by vectorizing ngrams and generating normalized TF-IDF vectors for each doc.
It also caches the fit vectorizer and the Class Weights matrix for transform.
Unlike regular TF-IDF, the Class TF-IDF applies over a class of documents weighting the regular TF-IDF by a class-relative weighting formula.
This is a suitable TF-IDF labelling scheme for clusters, where each cluster is treated as a class. Labels are then generated for each cluster relative to all other clusters.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\labelgenerator.py
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 | |
transform(y)
Transform unseen data to current Class TF-IDF. It vectorizes the input to the current fit vocabulary and applies the Class TF-IDF weights to obtain a vector of TF-IDF for the current input.
This method does not assign new data to classes by design. That is a downstream task and really upto task and user preferences. Class assignment may be executed on similarity between TF-IDF vectors, label distances or even label embedding vector operations. Class assignment may not require doing this transform at all.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\labelgenerator.py
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 | |
compute(cluster_feats)
Extracts data from ClusterFeatures, packs them into classes by their cluster and generates Class TF-IDF labels.
If MMR is passed, it will perform an MMR ranking after class labels have been generated.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src\exhaustive_hdbscan\labelgenerator.py
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 | |