|
15 | 15 | print("Please install it") |
16 | 16 | raise |
17 | 17 |
|
18 | | -try: |
19 | | - from mpltools import style |
20 | | - style.use('ggplot') |
21 | | -except: |
22 | | - print("Could not import mpltools: plots will not be styled correctly") |
23 | | - |
24 | 18 | import matplotlib.pyplot as plt |
25 | 19 | import numpy as np |
26 | 20 | from os import path |
|
39 | 33 | model = models.ldamodel.LdaModel( |
40 | 34 | corpus, num_topics=NUM_TOPICS, id2word=corpus.id2word, alpha=None) |
41 | 35 |
|
42 | | -ti = 0 |
43 | 36 | # Iterate over all the topics in the model |
44 | | -for ti in xrange(model.num_topics): |
| 37 | +for ti in range(model.num_topics): |
45 | 38 | words = model.show_topic(ti, 64) |
46 | 39 | tf = sum(f for f, w in words) |
47 | 40 | with open('topics.txt', 'w') as output: |
|
68 | 61 | create_cloud('cloud_blei_lda.png', words) |
69 | 62 |
|
70 | 63 | num_topics_used = [len(model[doc]) for doc in corpus] |
71 | | -plt.hist(num_topics_used, np.arange(42)) |
72 | | -plt.ylabel('Nr of documents') |
73 | | -plt.xlabel('Nr of topics') |
74 | | -plt.savefig('Figure_04_01.png') |
75 | | -plt.clf() |
| 64 | +fig,ax = plt.subplots() |
| 65 | +ax.hist(num_topics_used, np.arange(42)) |
| 66 | +ax.set_ylabel('Nr of documents') |
| 67 | +ax.set_xlabel('Nr of topics') |
| 68 | +fig.tight_layout() |
| 69 | +fig.savefig('Figure_04_01.png') |
76 | 70 |
|
77 | 71 |
|
78 | 72 | # Now, repeat the same exercise using alpha=1.0 |
|
83 | 77 | corpus, num_topics=NUM_TOPICS, id2word=corpus.id2word, alpha=ALPHA) |
84 | 78 | num_topics_used1 = [len(model1[doc]) for doc in corpus] |
85 | 79 |
|
86 | | -plt.hist([num_topics_used, num_topics_used1], np.arange(42)) |
87 | | -plt.ylabel('Nr of documents') |
88 | | -plt.xlabel('Nr of topics') |
| 80 | +fig,ax = plt.subplots() |
| 81 | +ax.hist([num_topics_used, num_topics_used1], np.arange(42)) |
| 82 | +ax.set_ylabel('Nr of documents') |
| 83 | +ax.set_xlabel('Nr of topics') |
89 | 84 |
|
90 | 85 | # The coordinates below were fit by trial and error to look good |
91 | | -plt.text(9, 223, r'default alpha') |
92 | | -plt.text(26, 156, 'alpha=1.0') |
93 | | -plt.savefig('Figure_04_02.png') |
| 86 | +ax.text(9, 223, r'default alpha') |
| 87 | +ax.text(26, 156, 'alpha=1.0') |
| 88 | +fig.tight_layout() |
| 89 | +fig.savefig('Figure_04_02.png') |
94 | 90 |
|
0 commit comments