-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from x-tabdeveloping/merging
Implemented Topic merging and original C-TF-IDF in clustering models.
- Loading branch information
Showing
6 changed files
with
209 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ line-length=79 | |
|
||
[tool.poetry] | ||
name = "turftopic" | ||
version = "0.2.8" | ||
version = "0.2.9" | ||
description = "Topic modeling with contextual representations from sentence transformers." | ||
authors = ["Márton Kardos <[email protected]>"] | ||
license = "MIT" | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import numpy as np | ||
import scipy.sparse as spr | ||
from sklearn.metrics import pairwise_distances | ||
from sklearn.preprocessing import normalize | ||
|
||
|
||
def cluster_centroid_distance( | ||
cluster_centroids: np.ndarray, | ||
vocab_embeddings: np.ndarray, | ||
metric="cosine", | ||
) -> np.ndarray: | ||
distances = pairwise_distances( | ||
cluster_centroids, vocab_embeddings, metric=metric | ||
) | ||
similarities = -distances / np.max(distances) | ||
# Z-score transformation | ||
similarities = (similarities - np.mean(similarities)) / np.std( | ||
similarities | ||
) | ||
return similarities | ||
|
||
|
||
def soft_ctf_idf( | ||
doc_topic_matrix: np.ndarray, doc_term_matrix: spr.csr_matrix | ||
) -> np.ndarray: | ||
eps = np.finfo(float).eps | ||
term_importance = doc_topic_matrix.T @ doc_term_matrix | ||
overall_in_topic = np.abs(term_importance).sum(axis=1) | ||
n_docs = len(doc_topic_matrix) | ||
tf = (term_importance.T / (overall_in_topic + eps)).T | ||
idf = np.log(n_docs / (np.abs(term_importance).sum(axis=0) + eps)) | ||
ctf_idf = tf * idf | ||
return ctf_idf | ||
|
||
|
||
def ctf_idf( | ||
doc_topic_matrix: np.ndarray, doc_term_matrix: spr.csr_matrix | ||
) -> np.ndarray: | ||
labels = np.argmax(doc_topic_matrix, axis=1) | ||
n_topics = doc_topic_matrix.shape[1] | ||
components = [] | ||
overall_freq = np.ravel(np.asarray(doc_term_matrix.sum(axis=0))) | ||
average = overall_freq.sum() / n_topics | ||
for i_topic in range(n_topics): | ||
freq = np.ravel( | ||
np.asarray(doc_term_matrix[labels == i_topic].sum(axis=0)) | ||
) | ||
component = freq * np.log(1 + average / overall_freq) | ||
components.append(component) | ||
return np.stack(components) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.