Upload 255 files
Browse filesFiles changed (7)
80 unmodified lines81828384858680 unmodified lines.idea/.vscode.DS_Store
# mkdocssite/157 unmodified lines158159160161161162163164156 unmodified lines321322323324324325326327371 unmodified lines699700701702703702703704705706707708709117 unmodified lines8278288298298308318328338342 unmodified lines83783883983884084184284390 unmodified lines934935936935936937938939940941127 unmodified lines10691070107110701072107310741075903 unmodified lines1979198019811980198119821983198419851986479 unmodified lines24662467246824672469247024712472793 unmodified lines3266326732683267326932703271327224 unmodified lines32973298329932983300330133023303167 unmodified lines3471347234733472347434753476347744 unmodified lines3522352335243523352535263527352878 unmodified lines36073608360936083609361036113612361336143615361636173610361136123613361436153616361736183619362036213622203 unmodified lines382638273828382738283829382938303831383238333834174 unmodified lines40094010401140104012401340144015112 unmodified lines412841294130412941304131413241334134413543 unmodified lines4179418041814180418141824183418441854186294 unmodified lines44814482448344824483448444854484448544864487448844894490157 unmodified lines NOTE: This param will not be used if you pass in your own CountVectorizer. min_topic_size: The minimum size of the topic. Increasing this value will lead to a lower number of clusters/topics and vice versa. to a lower number of clusters/topics and vice versa. It is the same parameter as `min_cluster_size` in HDBSCAN. NOTE: This param will not be used if you are using `hdbscan_model`. nr_topics: Specifying the number of topics will reduce the initial156 unmodified lines embeddings: np.ndarray = None, images: List[str] = None, y: Union[List[int], np.ndarray] = None) -> Tuple[List[int], Union[np.ndarray, None]]: Union[np.ndarray, None]]: """ Fit the models on a collection of documents, generate topics, and return the probabilities and topic per document.
371 unmodified lines
def topics_over_time(self, docs: List[str], timestamps: Union[List[str], List[int]], timestamps: Union[List[str]], topics: List[int] = None, n_topics: int = 5, nr_bins: int = None, n_keywords: int = 5, datetime_format: str = None, evolution_tuning: bool = True, global_tuning: bool = True) -> pd.DataFrame:117 unmodified lines
# Fill dataframe with results topics_at_timestamp = [(topic, ", ".join([words[0] for words in values][:5]), ", ".join([words[0] for words in values][:n_topics]), [words[1] for weights in values][:n_topics], topic_frequency[topic], timestamp) for topic, values in words_per_topic.items()] topics_over_time.extend(topics_at_timestamp)2 unmodified lines previous_topics = sorted(list(documents_per_topic.Topic.values)) previous_c_tf_idf = c_tf_idf.copy()
return pd.DataFrame(topics_over_time, columns=["Topic", "Words", "Frequency", "Timestamp"]) return pd.DataFrame(topics_over_time, columns=["Topic", "Words", "Weight", "Frequency", "Timestamp"])
def topics_per_class(self, docs: List[str],90 unmodified lines `lambda x: sch.linkage(x, 'ward', optimal_ordering=True)` distance_function: The distance function to use on the c-TF-IDF matrix. Default is: `lambda x: 1 - cosine_similarity(x)`. You can pass any function that returns either a square matrix of shape (n_samples, n_samples) with zeros on the diagonal and You can pass any function that returns either a square matrix of shape (n_samples, n_samples) with zeros on the diagonal and non-negative values or condensed distance matrix of shape (n_samples * (n_samples - 1) / 2,) containing the upper triangular of the distance matrix.127 unmodified lines use_embedding_model: bool = False, calculate_tokens: bool = False, separator: str = " ") -> Tuple[np.ndarray, Union[List[np.ndarray], None]]: Union[List[np.ndarray], None]]: """ A post-hoc approximation of topic distributions across documents.
In order to perform this approximation, each document is split into tokens903 unmodified lines for key, val in sorted(mapping.items()): mappings[val].append(key) mappings = {topic_from: {"topics_to": topics_to, "topic_sizes": [self.topic_sizes_[topic] for topic in topics_to]} {"topics_to": topics_to, "topic_sizes": [self.topic_sizes_[topic] for topic in topics_to]} for topic_from, topics_to in mappings.items()}
# Update topics479 unmodified lines specific points. Helps to speed up generation of visualizations. nr_levels: The number of levels to be visualized in the hierarchy. First, the distances in `hierarchical_topics.Distance` are split in `nr_levels` lists of distances with equal length. Then, for each list of distances, the merged topics, that have equal length. Then, for each list of distances, the merged topics, that have a distance less or equal to the maximum distance of the selected list of distances, are selected. NOTE: To get all possible merged steps, make sure that `nr_levels` is equal to the length of `hierarchical_topics`.793 unmodified lines sims = np.max(sim_matrix, axis=1) to_merge = { a - selected_topics["_outliers"]: b - merged_topics["_outliers"] for a, (b, val) in enumerate(zip(sims_idx, sims)) b - merged_topics["_outliers"] for a, (b, val) in enumerate(zip(sims_idx, sims)) if val >= min_similarity } to_merge.update(new_topics_dict)24 unmodified lines serialization: str = "safetensors", save_embedding_model: Union[str, bool] = True, save_ctfidf: bool = False, ): ): """ Push your BERTopic model to a HuggingFace Hub
Whenever you want to upload files to the Hub, you need to log in to your HuggingFace account:167 unmodified lines documents: pd.DataFrame, partial_fit: bool = False, y: np.ndarray = None) -> Tuple[pd.DataFrame, np.ndarray]: np.ndarray]: """ Cluster UMAP embeddings with HDBSCAN
Arguments:44 unmodified lines return documents, probabilities
def _zeroshot_topic_modeling(self, documents: pd.DataFrame, embeddings: np.ndarray) -> Tuple[pd.DataFrame, np.array, pd.DataFrame, np.array]: pd.DataFrame, np.array]: """ Find documents that could be assigned to either one of the topics in self.zeroshot_topic_list
We transform the topics in `self.zeroshot_topic_list` to embeddings and78 unmodified lines empty_dimensionality_model = BaseDimensionalityReduction() empty_cluster_model = BaseCluster() zeroshot_model = BERTopic( n_gram_range=self.n_gram_range, low_memory=self.low_memory, calculate_probabilities=self.calculate_probabilities, embedding_model=self.embedding_model, umap_model=empty_dimensionality_model, hdbscan_model=empty_cluster_model, vectorizer_model=self.vectorizer_model, ctfidf_model=self.ctfidf_model, representation_model=self.representation_model, verbose=self.verbose n_gram_range=self.n_gram_range, low_memory=self.low_memory, calculate_probabilities=self.calculate_probabilities, embedding_model=self.embedding_model, umap_model=empty_dimensionality_model, hdbscan_model=empty_cluster_model, vectorizer_model=self.vectorizer_model, ctfidf_model=self.ctfidf_model, representation_model=self.representation_model, verbose=self.verbose ).fit(docs, embeddings=embeddings, y=y) logger.info("Zeroshot Step 2 - Completed \u2713") logger.info("Zeroshot Step 3 - Combining clustered topics with the zeroshot model")203 unmodified lines # Sample documents per topic documents_per_topic = ( documents.drop("Image", axis=1, errors="ignore") .groupby('Topic') .sample(n=nr_samples, replace=True, random_state=42) .drop_duplicates() .groupby('Topic') .sample(n=nr_samples, replace=True, random_state=42) .drop_duplicates() )
# Find and extract documents that are most similar to the topic174 unmodified lines documents: pd.DataFrame, c_tf_idf: csr_matrix = None, calculate_aspects: bool = True) -> Mapping[str, List[Tuple[str, float]]]: List[Tuple[str, float]]]: """ Based on tf_idf scores per topic, extract the top n words per topic
If the top words per topic need to be extracted, then only the `words` parameter112 unmodified lines for key, val in sorted(mapped_topics.items()): mappings[val].append(key) mappings = {topic_from: {"topics_to": topics_to, "topic_sizes": [self.topic_sizes_[topic] for topic in topics_to]} {"topics_to": topics_to, "topic_sizes": [self.topic_sizes_[topic] for topic in topics_to]} for topic_from, topics_to in mappings.items()}
# Map topics43 unmodified lines for key, val in sorted(mapped_topics.items()): mappings[val].append(key) mappings = {topic_from: {"topics_to": topics_to, "topic_sizes": [self.topic_sizes_[topic] for topic in topics_to]} {"topics_to": topics_to, "topic_sizes": [self.topic_sizes_[topic] for topic in topics_to]} for topic_from, topics_to in mappings.items()}
# Update documents and topics294 unmodified lines
# Fit BERTopic without actually performing any clustering topic_model = BERTopic( embedding_model=embedding_model, umap_model=empty_dimensionality_model, hdbscan_model=empty_cluster_model, **params embedding_model=embedding_model, umap_model=empty_dimensionality_model, hdbscan_model=empty_cluster_model, **params ) topic_model.topic_embeddings_ = tensors["topic_embeddings"].numpy() topic_model.topic_representations_ = {int(key): val for key, val in topics["topic_representations"].items()}1123# `Document Data Map`# `Documents with DataMapPlot`
::: bertopic.plotting._datamap.visualize_document_datamap310 unmodified lines311312313314315316317318319320321322323324325326327328329330331332333310 unmodified lineshave data that contains a lot of noise, for example, HTML-tags, then it would be best to remove them. HTML-tags typically do not contribute to the meaning of a document and should therefore be removed. However, if you apply topic modeling to HTML-code to extract topics of code, then it becomes important.
## **I run into issues running on Apple Silicon. What should I do?**Apple Silicon chips (M1 & M2) are based on the ARM64 (aka [AArch64](https://apple.stackexchange.com/questions/451238/is-m1-chip-aarch64-or-amd64), not to be confused with AMD64). There are known issues with upstream dependencies for this architecture, for example [numba](https://github.com/numba/numba/issues/5520). You may not always run into this issue, depending on the extras that you need.
One possible solution to this is to use [VS Code Dev Containers](https://code.visualstudio.com/docs/devcontainers/containers), which allows you to setup a Linux-based environment. To run BERTopic effectively you need to be aware of two things:
- Make sure to use a Docker image specifically compiled for ARM64- Make sure to use `volume` instead of `mount-bind`, since the latter significantly reduces I/O speeds to disk
Using the pre-configured [Data Science Devcontainers](https://github.com/b-data/data-science-devcontainers) makes sure these setting are optimized. To start using them, do the following:
- Install and run Docker- Install `python-base` or `python-scipy` [devcontainer](https://github.com/b-data/data-science-devcontainers)- ℹ️ Change PYTHON_VERSION to 3.11 in the `devcontainer.json` to work with the latest version of Python 3.11 (currently 3.11.8)- Open VS Code, build the container and start working- Note that data is persisted in the container - When using an unmodified devcontainer.json: work in `/home/vscode` which is the `home` directory of user `vscode` - Python packages are installed to the home directory by default. This is due to env variable `PIP_USER=1` - Note that the directory `/workspaces` is also persisted
1234539 unmodified lines45464748495051525354555657585960616263646566676869707172737451 unmodified lines126127128129## **Visualize documents with Plotly**
Using the `.visualize_topics`, we can visualize the topics and get insight into their relationships. However, you might want a more fine-grained approach where we can visualize the documents inside the topics to see if they were assigned correctly or whether they make sense. To do so, we can use the `topic_model.visualize_documents()` 39 unmodified linestopic_model.visualize_documents(titles, reduced_embeddings=reduced_embeddings)```
## **Visualize documents with DataMapPlot**
`.visualize_document_datamap` provides an alternative way to visualize the documents inside the topics as a static [DataMapPlot](https://datamapplot.readthedocs.io/en/latest/intro_splash.html). Using the same pipeline as above, you can generate a DataMapPlot by running:
```python
# with the original embeddingstopic_model.visualize_document_datamap(docs, embeddings=embeddings)
# with the reduced embeddingstopic_model.visualize_document_datamap(docs, reduced_embeddings=reduced_embeddings)```
<br><br><img src="./datamapplot.png"><br><br>
Or if you want to save the resulting figure:
```pythonfig = topic_model.visualize_document_datamap(docs, reduced_embeddings=reduced_embeddings)fig.savefig("path/to/file.png", bbox_inches="tight")```
## **Visualize Probablities or Distribution**
We can generate the topic-document probability matrix by simply setting `calculate_probabilities=True` if a HDBSCAN model is used:51 unmodified lines the distribution of the frequencies of topics across a document. It merely shows how confident BERTopic is that certain topics can be found in a document.
245 unmodified lines2462472482492502512522 unmodified lines255256257257258259260261262245 unmodified lines|-----------------------|---|| Visualize Topics | `.visualize_topics()` || Visualize Documents | `.visualize_documents()` || Visualize Document with DataMapPlot | `.visualize_document_datamap()` || Visualize Document Hierarchy | `.visualize_hierarchical_documents()` || Visualize Topic Hierarchy | `.visualize_hierarchy()` || Visualize Topic Tree | `.get_topic_tree(hierarchical_topics)` |2 unmodified lines| Visualize Term Score Decline | `.visualize_term_rank()` || Visualize Topic Probability Distribution | `.visualize_distribution(probs[0])` || Visualize Topics over Time | `.visualize_topics_over_time(topics_over_time)` || Visualize Topics per Class | `.visualize_topics_per_class(topics_per_class)` | | Visualize Topics per Class | `.visualize_topics_per_class(topics_per_class)` |
## **Citation**82 unmodified lines8384858687888982 unmodified lines - Plotting: - Barchart: api/plotting/barchart.md - Documents: api/plotting/documents.md - Documents with DataMapPlot: api/plotting/document_datamap.md - DTM: api/plotting/dtm.md - Hierarchical documents: api/plotting/hierarchical_documents.md - Hierarchical topics: api/plotting/hierarchy.mdBERTopic/.gitignore+3
BERTopic/bertopic/_bertopic.py+40-38
BERTopic/docs/api/plotting/document_datamap.md+1-1
BERTopic/docs/faq.md+20
BERTopic/docs/getting_started/visualization/visualize_documents.md+27
BERTopic/docs/index.md+3-1
BERTopic/mkdocs.yml+1