forked from jbesomi/texthero
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
306 lines (243 loc) · 8.96 KB
/
visualization.py
File metadata and controls
306 lines (243 loc) · 8.96 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
Visualize insights and statistics of a text-based Pandas DataFrame.
"""
import pandas as pd
import numpy as np
import plotly.express as px
from wordcloud import WordCloud
from texthero import preprocessing
from texthero._types import TextSeries, InputSeries
import string
from matplotlib.colors import LinearSegmentedColormap as lsg
import matplotlib.pyplot as plt
from collections import Counter
def scatterplot(
df: pd.DataFrame,
col: str,
color: str = None,
hover_name: str = None,
hover_data: [] = None,
title="",
return_figure=False,
):
"""
Show scatterplot of DataFrame column using python plotly scatter.
Plot the values in column col. For example, if every cell in df[col]
is a list of three values (e.g. from doing PCA with 3 components),
a 3D-Plot is created and every cell entry [x, y, z] is visualized
as the point (x, y, z).
Parameters
----------
df: DataFrame with a column to be visualized.
col: str
The name of the column of the DataFrame to use for x and y (and z)
axis.
color: str, optional, default=None
Name of the column to use for coloring (rows with same value get same
color).
hover_name: str, optional, default=None
Name of the column to supply title of hover data when hovering over a
point.
hover_data: List[str], optional, default=[]
List of column names to supply data when hovering over a point.
title: str, default to "".
Title of the plot.
return_figure: bool, optional, default=False
Function returns the figure instead of showing it if set to True.
Examples
--------
>>> import texthero as hero
>>> import pandas as pd
>>> df = pd.DataFrame(["Football, Sports, Soccer",
... "music, violin, orchestra", "football, fun, sports",
... "music, fun, guitar"], columns=["texts"])
>>> df["texts"] = hero.clean(df["texts"]).pipe(hero.tokenize)
>>> df["pca"] = (
... hero.tfidf(df["texts"])
... .pipe(hero.pca, n_components=3)
... )
>>> df["topics"] = (
... hero.tfidf(df["texts"])
... .pipe(hero.kmeans, n_clusters=2)
... )
>>> hero.scatterplot(df, col="pca", color="topics",
... hover_data=["texts"]) # doctest: +SKIP
"""
plot_values = np.stack(df[col], axis=1)
dimension = len(plot_values)
if dimension < 2 or dimension > 3:
raise ValueError(
"The column you want to visualize has dimension < 2 or dimension > 3."
" The function can only visualize 2- and 3-dimensional data."
)
if dimension == 2:
x, y = plot_values[0], plot_values[1]
fig = px.scatter(
df,
x=x,
y=y,
color=color,
hover_data=hover_data,
title=title,
hover_name=hover_name,
)
else:
x, y, z = plot_values[0], plot_values[1], plot_values[2]
fig = px.scatter_3d(
df,
x=x,
y=y,
z=z,
color=color,
hover_data=hover_data,
title=title,
hover_name=hover_name,
)
if return_figure:
return fig
else:
fig.show()
"""
Wordcloud
"""
@InputSeries(TextSeries)
def wordcloud(
s: TextSeries,
font_path: str = None,
width: int = 400,
height: int = 200,
max_words=200,
mask=None,
contour_width=0,
contour_color="PAPAYAWHIP",
min_font_size=4,
background_color="PAPAYAWHIP",
max_font_size=None,
relative_scaling="auto",
colormap=None,
return_figure=False,
):
"""
Plot wordcloud image using WordCloud from word_cloud package.
Most of the arguments are very similar if not equal to the mother
function. In constrast, all words are taken into account when computing
the wordcloud, inclusive stopwords. They can be easily removed with
preprocessing.remove_stopwords.
Words are computed using generate_from_frequencies.
To reduce blur in the wordcloud image, `width` and `height` should be at
least 400.
Parameters
----------
s : :class:`texthero._types.TextSeries`
font_path : str, optional, default=None
Font path to the font that will be used (OTF or TTF). Defaults to
DroidSansMono path on a Linux machine. If you are on another OS or
don't have this font, you need to adjust this path.
width : int, optional, default=400
Width of the canvas.
height : int, optional, default=200
Height of the canvas.
max_words : int, optional, default=200
The maximum number of words.
mask : nd-array or None, optional, default=None
When set, gives a binary mask on where to draw words. When set, width
and height will be ignored and the shape of mask will be used instead.
All white (#FF or #FFFFFF) entries will be considerd "masked out"
while other entries will be free to draw on.
contour_width: float, optional, default=0
If mask is not None and contour_width > 0, draw the mask contour.
contour_color: str, optional, default="PAPAYAWHIP"
Mask contour color.
min_font_size : int, optional, default=4
Smallest font size to use. Will stop when there is no more room in
this size.
background_color : str, optional, default="PAPAYAWHIP"
Background color for the word cloud image.
max_font_size : int or None, optional, default=None
Maximum font size for the largest word. If None, height of the image
is used.
relative_scaling : float, optional, default="auto"
Importance of relative word frequencies for font-size. With
relative_scaling=0, only word-ranks are considered. With
relative_scaling=1, a word that is twice as frequent will have twice
the size. If you want to consider the word frequencies and not only
their rank, relative_scaling around .5 often looks good.
If 'auto' it will be set to 0.5 unless repeat is true, in which
case it will be set to 0.
colormap : string or matplotlib colormap, optional, default="viridis"
Matplotlib colormap to randomly draw colors from for each word.
"""
text = s.str.cat(sep=" ")
if colormap is None:
# Custom palette.
# TODO move it under tools.
corn = (255.0 / 256, 242.0 / 256, 117.0 / 256)
mango_tango = (255.0 / 256, 140.0 / 256, 66.0 / 256)
crayola = (63.0 / 256, 136.0 / 256, 197.0 / 256)
crimson = (215.0 / 256, 38.0 / 256, 61.0 / 256)
oxford_blue = (2.0 / 256, 24.0 / 256, 43.0 / 256)
texthero_cm = lsg.from_list(
"texthero", [corn, mango_tango, crayola, crimson, oxford_blue]
)
colormap = texthero_cm
words = s.str.cat(sep=" ").split()
wordcloud = WordCloud(
font_path=font_path,
width=width,
height=height,
max_words=max_words,
mask=mask,
contour_width=contour_width,
contour_color=contour_color,
min_font_size=min_font_size,
background_color=background_color,
max_font_size=max_font_size,
relative_scaling=relative_scaling,
colormap=colormap,
# stopwords=[], # TODO. Will use generate from frequencies.
# normalize_plurals=False, # TODO.
).generate_from_frequencies(dict(Counter(words)))
# fig = px.imshow(wordcloud)
# fig.show()
fig, ax = plt.subplots(figsize=(20, 10))
ax.imshow(wordcloud, interpolation="bilinear")
ax.axis("off")
if return_figure:
return fig
@InputSeries(TextSeries)
def top_words(s: TextSeries, normalize=False) -> pd.Series:
r"""
Return a pandas series with index the top words and as value the count.
Tokenization: split by space and remove all punctuations that are not
between characters.
Parameters
----------
normalize : bool, optional, default=False.
When set to true, return normalized values.
Examples
--------
>>> import pandas as pd
>>> import texthero as hero
>>> s = pd.Series("one two two three three three")
>>> hero.top_words(s)
three 3
two 2
one 1
dtype: int64
"""
# Replace all punctuation that are NOT in-between chacarters
# This means, they have either a non word-bounding \B, are at the start ^, or at the end $
# As re.sub replace all and not just the matching group, add matching parenthesis to the character
# to keep during replacement.
# TODO replace it with tokenizer.
pattern = (
rf"((\w)[{string.punctuation}](?:\B|$)|(?:^|\B)[{string.punctuation}](\w))"
)
return (
s.str.replace(
pattern, r"\2 \3"
) # \2 and \3 permits to keep the character around the punctuation.
.str.split() # now split by space
.explode() # one word for each line
.value_counts(normalize=normalize)
)