Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

ClusterStats.py 3.3 KB

You have to be logged in to leave a comment. Sign In
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
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
150
151
152
153
154
  1. # ---
  2. # jupyter:
  3. # jupytext:
  4. # formats: ipynb,py:percent
  5. # text_representation:
  6. # extension: .py
  7. # format_name: percent
  8. # format_version: '1.3'
  9. # jupytext_version: 1.13.0
  10. # kernelspec:
  11. # display_name: Python 3 (ipykernel)
  12. # language: python
  13. # name: python3
  14. # ---
  15. # %% [markdown]
  16. # # Book Clustering Statistics
  17. #
  18. # This notebook provides statistics on the results of our book clustering.
  19. # %% [markdown]
  20. # ## Setup
  21. # %%
  22. import pandas as pd
  23. import seaborn as sns
  24. import matplotlib as mpl
  25. import matplotlib.pyplot as plt
  26. import numpy as np
  27. import json
  28. # %%
  29. from bookdata.db import db_url
  30. # %% [markdown]
  31. # ## Load Data
  32. #
  33. # Let's start by getting our clusters and their statistics:
  34. # %%
  35. clusters = pd.read_sql_table('cluster_stats', db_url())
  36. clusters.info()
  37. # %%
  38. clusters.set_index('cluster', inplace=True)
  39. # %% [markdown]
  40. # Describe the count columns for basic descriptive stats:
  41. # %%
  42. clusters.describe()
  43. # %% [markdown]
  44. # 75% of clusters only contain 2 ISBNs (probably -10 and -13) and one book. OpenLibrary also contributes to the largest number of clusters.
  45. # %%
  46. tot = clusters.sum(axis=1)
  47. tot.head()
  48. # %%
  49. with open('book-links/cluster-stats.json', 'w') as csf:
  50. json.dump({
  51. 'clusters': len(tot),
  52. 'largest': tot.max()
  53. }, csf)
  54. # %% [markdown]
  55. # ## Clusters per Source
  56. #
  57. # How many clusters are connected to each source?
  58. # %%
  59. src_counts = pd.Series(dict(
  60. (c, np.sum(clusters[c] > 0)) for c in clusters.columns
  61. ))
  62. src_counts
  63. # %%
  64. src_counts.plot.barh()
  65. plt.xlabel('# of Clusters')
  66. plt.show()
  67. # %% [markdown]
  68. # ## Distributions
  69. #
  70. # Let's look at the distributions of cluster sizes.
  71. # %%
  72. size_dist = pd.concat(dict(
  73. (c, clusters[c].value_counts()) for c in clusters.columns
  74. ), names=['RecType'])
  75. size_dist.index.set_names(['RecType', 'RecCount'], inplace=True)
  76. size_dist = size_dist.reset_index(name='Clusters')
  77. size_dist.head()
  78. # %%
  79. for rt, data in size_dist.groupby('RecType'):
  80. plt.scatter(data['RecCount'], data['Clusters'], marker='1', label=rt)
  81. plt.legend()
  82. plt.xscale('log')
  83. plt.yscale('log')
  84. # %% [markdown]
  85. # Looks mostly fine - we expect a lot of power laws - but the number of clusters with merged GoodReads works is concerning.
  86. # %% [markdown]
  87. # ## GoodReads Work Merging
  88. #
  89. # Why are GoodReads works merging? Let's look at those.
  90. # %%
  91. gr_big = clusters[clusters['gr_works'] > 1].sort_values('gr_works', ascending=False)
  92. gr_big.info()
  93. # %% [markdown]
  94. # We have 6K of these clusters. What fraction of the GoodReads-affected clusters is this?
  95. # %%
  96. len(gr_big) / clusters['gr_books'].count()
  97. # %% [markdown]
  98. # Less than 1%. Not bad, but let's look.
  99. # %%
  100. gr_big.head()
  101. # %% [markdown]
  102. # What's that big cluster?
  103. # %%
  104. biggest = gr_big.index[0]
  105. cluster_titles = pd.read_sql(f'''
  106. SELECT DISTINCT gr_work_id, work_title
  107. FROM gr.book_cluster
  108. JOIN gr.book_ids USING (gr_book_id)
  109. JOIN gr.work_title USING (gr_work_id)
  110. WHERE cluster = {biggest} AND work_title IS NOT NULL;
  111. ''', db_url())
  112. cluster_titles
  113. # %% [markdown]
  114. # Let's also find out how many *ratings* are affected by this.
  115. # %%
  116. pd.read_sql('''
  117. SELECT SUM((gr_works > 1)::int), AVG((gr_works > 1)::int)
  118. FROM gr.add_action
  119. JOIN gr.cluster_stats ON (book_id = cluster)
  120. ''', db_url())
  121. # %% [markdown]
  122. # Almost 9% of our add-to-shelf actions are for such a book - that's disappointing.
  123. # %%
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...