-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathdemo_pair_plot_20_newsgroups.py
More file actions
30 lines (25 loc) · 1 KB
/
Copy pathdemo_pair_plot_20_newsgroups.py
File metadata and controls
30 lines (25 loc) · 1 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
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
import scattertext as st
import time
import scattertext.categoryprojector.pairplot
t0 = time.time()
newsgroups_train = fetch_20newsgroups(subset='train', remove=('headers', 'footers', 'quotes'))
print(time.time() - t0)
vectorizer = TfidfVectorizer()
tfidf_X = vectorizer.fit_transform(newsgroups_train.data)
print(time.time() - t0)
corpus = st.CorpusFromScikit(
X=CountVectorizer(vocabulary=vectorizer.vocabulary_).fit_transform(newsgroups_train.data),
y=newsgroups_train.target,
feature_vocabulary=vectorizer.vocabulary_,
category_names=newsgroups_train.target_names,
raw_texts=newsgroups_train.data
).build().get_unigram_corpus()
print(time.time() - t0)
html = scattertext.produce_pairplot(corpus, enable_zoom=True)
print(time.time() - t0)
file_name = 'demo_pair_plot.html'
open(file_name, 'wb').write(html.encode('utf-8'))
print('Open ./%s in Chrome.' % (file_name))
print(time.time() - t0)