-
Notifications
You must be signed in to change notification settings - Fork 4
/
cluster.py
78 lines (56 loc) · 1.8 KB
/
cluster.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
"""
Use TruncatedSVD
"""
# Basic Imports
import os
from pprint import pprint
import csv
from itertools import *
from operator import itemgetter
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import rand
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import PCA
from sklearn.decomposition import TruncatedSVD
from matplotlib.pyplot import figure, show
from itertools import *
from operator import itemgetter
# Scientific computing imports
import matplotlib.pyplot as plt
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import networkx as nx
from scipy import sparse
# Import other files in package
from models.Comment import Comment
# Prepare Data into mutli-dimensional vectors
vectorizer = CountVectorizer()
lsa = TruncatedSVD(3)
comments = Comment.select()
texts = np.array([comment.text for comment in comments])
metadata = [comment.id for comment in comments]
vectors = vectorizer.fit_transform(texts)
# Transform data into two dimensions using LSA
X = lsa.fit_transform(vectors)
# Plot
x = X[:,0]
y = X[:,1]
import mpld3
fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
N = 100
scatter = ax.scatter(x,
y,
# c=colors,
# s=1000 * np.random.random(size=N),
alpha=0.3,
cmap=plt.cm.jet)
ax.grid(color='white', linestyle='solid')
ax.set_title("Comments Clustering", size=21)
# tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
# mpld3.plugins.connect(fig, tooltip)
for i, txt in enumerate(metadata):
ax.annotate(txt, (x[i],y[i]), size=5)
# mpld3.save_html(fig, open('scatter.html','w'))
mpld3.show()