[c09aa8]: / pipeline / main300 / affcenter2.py

Download this file

52 lines (39 with data), 1.5 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
from __future__ import print_function
from sklearn.decomposition import TruncatedSVD
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.preprocessing import Normalizer
from sklearn import metrics
from sklearn.cluster import KMeans, MiniBatchKMeans, AffinityPropagation
import logging
import sys
from time import time
import numpy as np
import pandas as pd
data = open("cleancenters.txt", 'r').readlines()
dataset = []
data = np.array(data)
for x in data :
dataset.append(x)
vectorizer = TfidfVectorizer(max_df=0.5, max_features=10000, min_df=2, stop_words='english', use_idf=True)
X = vectorizer.fit_transform(dataset)
print(X.shape)
km = AffinityPropagation(verbose=True).fit(X)
f2 = open('./centerclusters/centers2.txt', 'a')
centers = km.cluster_centers_
terms = vectorizer.get_feature_names()
for i in range(centers.shape[0]):
f2.write("Cluster %d:" % i)
for ind in centers[i]:
indi = [list(line.nonzero()[1]) for line in ind]
for k in indi[0] :
f2.write(' %s' % terms[k])
f2.write("\n")
cluster_map = pd.DataFrame()
cluster_map['cluster'] = km.labels_
for x in range(centers.shape[0]) :
f1 = open('./centerclusters/clust_' + str(x) + '.txt', 'a')
y = cluster_map[cluster_map.cluster == x]['cluster'].index
for n in y :
f1.write(data[n])