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.2 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
  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.1
  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 matplotlib.pyplot as plt
  24. import numpy as np
  25. # %% [markdown]
  26. # ## Load Data
  27. #
  28. # Let's start by getting our clusters and their statistics:
  29. # %%
  30. clusters = pd.read_parquet('book-links/cluster-stats.parquet')
  31. clusters.info()
  32. # %%
  33. clusters.set_index('cluster', inplace=True)
  34. # %% [markdown]
  35. # Describe the count columns for basic descriptive stats:
  36. # %%
  37. clusters.describe()
  38. # %% [markdown]
  39. # 75% of clusters only contain 2 ISBNs (probably -10 and -13) and one book. OpenLibrary also contributes to the largest number of clusters.
  40. # %% [markdown]
  41. # ## Clusters per Source
  42. #
  43. # How many clusters are connected to each source?
  44. # %%
  45. src_counts = pd.Series(dict(
  46. (c, np.sum(clusters[c] > 0)) for c in clusters.columns
  47. ))
  48. src_counts
  49. # %%
  50. src_counts.plot.barh()
  51. plt.xlabel('# of Clusters')
  52. plt.show()
  53. # %% [markdown]
  54. # ## Distributions
  55. #
  56. # Let's look at the distributions of cluster sizes.
  57. # %%
  58. size_dist = pd.concat(dict(
  59. (c, clusters[c].value_counts()) for c in clusters.columns if c != 'n_nodes'
  60. ), names=['RecType'])
  61. size_dist.index.set_names(['RecType', 'RecCount'], inplace=True)
  62. size_dist = size_dist.reset_index(name='Clusters')
  63. size_dist.head()
  64. # %%
  65. for rt, data in size_dist.groupby('RecType'):
  66. plt.scatter(data['RecCount'], data['Clusters'], marker='1', label=rt)
  67. plt.legend()
  68. plt.xlabel('# of Records')
  69. plt.xscale('log')
  70. plt.ylabel('# of Clusters')
  71. plt.yscale('log')
  72. plt.show()
  73. # %% [markdown]
  74. # Looks mostly fine - we expect a lot of power laws - but the number of clusters with merged GoodReads works is concerning.
  75. # %% [markdown]
  76. # ## GoodReads Work Merging
  77. #
  78. # Why are GoodReads works merging? Let's look at those.
  79. # %%
  80. gr_big = clusters[clusters['n_gr_works'] > 1].sort_values('n_gr_works', ascending=False)
  81. gr_big.info()
  82. # %% [markdown]
  83. # We have a lot of these clusters. What fraction of the GoodReads-affected clusters is this?
  84. # %%
  85. len(gr_big) / clusters['n_gr_books'].count()
  86. # %% [markdown]
  87. # Less than 1%. Not bad, but let's look.
  88. # %%
  89. gr_big.head()
  90. # %% [markdown]
  91. # ## Large Cluster Debugging
  92. #
  93. # We have some pretty big clusters:
  94. # %%
  95. big = clusters.nlargest(5, 'n_nodes')
  96. big
  97. # %% [markdown]
  98. # What is up with this? We should figure out what went wrong, if we can. What are its ISBNs?
  99. # %%
  100. isbns = pd.read_parquet('book-links/all-isbns.parquet').set_index('isbn_id')
  101. isbns.head()
  102. # %%
  103. links = pd.read_parquet('book-links/isbn-clusters.parquet', columns=['isbn_id', 'cluster'])
  104. links.head()
  105. # %%
  106. big_id = big.index[0]
  107. bl = links[links['cluster'] == big_id].drop(columns=['cluster'])
  108. bl = bl.join(isbns, on='isbn_id')
  109. bl.sort_values('isbn')
  110. # %% [markdown]
  111. # What are the things with the highest record count (besides ratings)?
  112. # %%
  113. bl['btot'] = bl.iloc[:, 2:-2].sum(axis=1)
  114. bl.nlargest(20, 'btot')
  115. # %%
Tip!

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

Comments

Loading...