-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtopic_model_utils.py
More file actions
55 lines (45 loc) · 1.82 KB
/
topic_model_utils.py
File metadata and controls
55 lines (45 loc) · 1.82 KB
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
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 8 20:54:15 2017
@author: DIP
"""
import numpy as np
# prints components of all the topics
# obtained from topic modeling
def print_topics_udf(topics, total_topics=1,
weight_threshold=0.0001,
display_weights=False,
num_terms=None):
for index in range(total_topics):
topic = topics[index]
topic = [(term, float(wt))
for term, wt in topic]
topic = [(word, round(wt,2))
for word, wt in topic
if abs(wt) >= weight_threshold]
if display_weights:
print('Topic #'+str(index+1)+' with weights')
print(topic[:num_terms]) if num_terms else topic
else:
print('Topic #'+str(index+1)+' without weights')
tw = [term for term, wt in topic]
print(tw[:num_terms]) if num_terms else tw
print()
# extracts topics with their terms and weights
# format is Topic N: [(term1, weight1), ..., (termn, weightn)]
def get_topics_terms_weights(weights, feature_names):
feature_names = np.array(feature_names)
sorted_indices = np.array([list(row[::-1])
for row
in np.argsort(np.abs(weights))])
sorted_weights = np.array([list(wt[index])
for wt, index
in zip(weights,sorted_indices)])
sorted_terms = np.array([list(feature_names[row])
for row
in sorted_indices])
topics = [np.vstack((terms.T,
term_weights.T)).T
for terms, term_weights
in zip(sorted_terms, sorted_weights)]
return topics