-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-vis.py
91 lines (72 loc) · 2.3 KB
/
data-vis.py
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
import plotly_express as px
import pandas as pd
import numpy as np
import streamlit as st
import seaborn as sns
from datasets import fake_br_corpus
train = fake_br_corpus.loadTrain()#.sample(100)
train
st.write(
px.pie(train, names='label'),
px.pie(train, names='category'),
px.histogram(train, x='category', color='label', barmode='group'),
)
from preprocessing import preprocess
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer(
preprocessor=preprocess,
ngram_range=(1,3),
max_features=1000
)
def countFeatures(X):
return pd.DataFrame(
[(label, (row>0).sum()) for label, row in zip(train.label, X)], columns=['label', 'numFeatures']
)
st.cache()
def extractFeatures(texts):
# texts = train.text
return tfidf.fit_transform(texts).toarray()
X = extractFeatures(train.text)
featureCounts = countFeatures(X)
st.write(
X,
featureCounts,
featureCounts.sort_values('numFeatures'),
px.violin(featureCounts, y='numFeatures', color='label'),
px.histogram(featureCounts, x='numFeatures', color='label', barmode='stack'),
)
shouldCutFeatures = st.checkbox('Cut Features')
numFeatures = 50
# numFeatures = featureCounts['numFeatures'].min()
st.write(f'Cutting to {50}')
def aaa(a, cut):
orderFactor = np.arange(len(a))*1E-20
a = a + orderFactor
return np.where(a >= np.sort(a)[-cut], a - orderFactor, 0)
X = np.array(
[aaa(a, numFeatures) for a in X]
)
featureCounts = countFeatures(X)
st.write(
X,
featureCounts,
featureCounts.sort_values('numFeatures'),
px.violin(featureCounts, y='numFeatures', color='label'),
)
# from sklearn.decomposition import PCA
# reducer = PCA(2)
from sklearn.decomposition import TruncatedSVD
reducer = TruncatedSVD(2)
# from sklearn.decomposition import LatentDirichletAllocation
# lda = LatentDirichletAllocation(50)
# X = lda.fit_transform(X)
points = reducer.fit_transform(X)
points = pd.DataFrame(points, columns=['x', 'y'])
points['label'] = featureCounts.label
points['numFeatures'] = featureCounts.numFeatures
points['numWords'] = train.text.apply(lambda text: len(text.split(' '))).values
# train[['pcax','y','z']] = points
st.write(
px.scatter(points, x='x', y='y', color='label', size='numFeatures'),
px.scatter(points, x='x', y='y', color='label', size='numWords'),
)