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

LinkageStats.py 4.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
155
156
157
158
159
160
161
162
163
164
165
166
  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 Data Linkage Statistics
  17. #
  18. # This notebook presents statistics of the book data integration.
  19. # %% [markdown]
  20. # ## Setup
  21. # %% tags=[]
  22. import pandas as pd
  23. import matplotlib as mpl
  24. import matplotlib.pyplot as plt
  25. import numpy as np
  26. # %% [markdown]
  27. # ## Load Link Stats
  28. #
  29. # We compute dataset linking statitsics as `gender-stats.csv` using DataFusion. Let's load those:
  30. # %% tags=[]
  31. link_stats = pd.read_csv('book-links/gender-stats.csv')
  32. link_stats.head()
  33. # %% [markdown]
  34. # Now let's define variables for our variou codes. We are first going to define our gender codes. We'll start with the resolved codes:
  35. # %% tags=[]
  36. link_codes = ['female', 'male', 'ambiguous', 'unknown']
  37. # %% [markdown]
  38. # We want the unlink codes in order, so the last is the first link failure:
  39. # %% tags=[]
  40. unlink_codes = ['no-author-rec', 'no-book-author', 'no-book']
  41. # %% tags=[]
  42. all_codes = link_codes + unlink_codes
  43. # %% [markdown]
  44. # ## Processing Statistics
  45. #
  46. # Now we'll pivot each of our count columns into a table for easier reference.
  47. # %% tags=[]
  48. book_counts = link_stats.pivot('dataset', 'gender', 'n_books')
  49. book_counts = book_counts.reindex(columns=all_codes)
  50. book_counts.assign(total=book_counts.sum(axis=1))
  51. # %% tags=[]
  52. act_counts = link_stats.pivot('dataset', 'gender', 'n_actions')
  53. act_counts = act_counts.reindex(columns=all_codes)
  54. act_counts.drop(index='LOC-MDS', inplace=True)
  55. act_counts
  56. # %% [markdown]
  57. # We're going to want to compute versions of this table as fractions, e.g. the fraction of books that are written by women. We will use the following helper function:
  58. # %%
  59. def fractionalize(data, columns, unlinked=None):
  60. fracs = data[columns]
  61. fracs.columns = fracs.columns.astype('str')
  62. if unlinked:
  63. fracs = fracs.assign(unlinked=data[unlinked].sum(axis=1))
  64. totals = fracs.sum(axis=1)
  65. return fracs.divide(totals, axis=0)
  66. # %% [markdown]
  67. # And a helper function for plotting bar charts:
  68. # %%
  69. def plot_bars(fracs, ax=None, cmap=mpl.cm.Dark2):
  70. if ax is None:
  71. ax = plt.gca()
  72. size = 0.5
  73. ind = np.arange(len(fracs))
  74. start = pd.Series(0, index=fracs.index)
  75. for i, col in enumerate(fracs.columns):
  76. vals = fracs.iloc[:, i]
  77. rects = ax.barh(ind, vals, size, left=start, label=col, color=cmap(i))
  78. for j, rec in enumerate(rects):
  79. if vals.iloc[j] < 0.1 or np.isnan(vals.iloc[j]): continue
  80. y = rec.get_y() + rec.get_height() / 2
  81. x = start.iloc[j] + vals.iloc[j] / 2
  82. ax.annotate('{:.1f}%'.format(vals.iloc[j] * 100),
  83. xy=(x,y), ha='center', va='center', color='white',
  84. fontweight='bold')
  85. start += vals.fillna(0)
  86. ax.set_xlabel('Fraction of Books')
  87. ax.set_ylabel('Data Set')
  88. ax.set_yticks(ind)
  89. ax.set_yticklabels(fracs.index)
  90. ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
  91. # %% [markdown]
  92. # ## Resolution of Books
  93. #
  94. # What fraction of *unique books* are resolved from each source?
  95. # %%
  96. fractionalize(book_counts, link_codes + unlink_codes)
  97. # %%
  98. plot_bars(fractionalize(book_counts, link_codes + unlink_codes))
  99. # %%
  100. fractionalize(book_counts, link_codes, unlink_codes)
  101. # %%
  102. plot_bars(fractionalize(book_counts, link_codes, unlink_codes))
  103. # %%
  104. plot_bars(fractionalize(book_counts, ['female', 'male']))
  105. # %% [markdown]
  106. # ## Resolution of Ratings
  107. #
  108. # What fraction of *rating actions* have each resolution result?
  109. # %%
  110. fractionalize(act_counts, link_codes + unlink_codes)
  111. # %%
  112. plot_bars(fractionalize(act_counts, link_codes + unlink_codes))
  113. # %%
  114. fractionalize(act_counts, link_codes, unlink_codes)
  115. # %%
  116. plot_bars(fractionalize(act_counts, link_codes, unlink_codes))
  117. # %%
  118. plot_bars(fractionalize(act_counts, ['female', 'male']))
  119. # %% [markdown]
  120. # ## Metrics
  121. #
  122. # Finally, we're going to write coverage metrics.
  123. # %%
  124. book_tots = book_counts.sum(axis=1)
  125. book_link = book_counts['male'] + book_counts['female'] + book_counts['ambiguous']
  126. book_cover = book_link / book_tots
  127. book_cover
  128. # %%
  129. book_cover.to_json('book-coverage.json')
  130. # %%
Tip!

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

Comments

Loading...