|
a |
|
b/pipeline/main300/elbow340.py |
|
|
1 |
from __future__ import print_function |
|
|
2 |
from sklearn.decomposition import TruncatedSVD |
|
|
3 |
from sklearn.feature_extraction.text import TfidfVectorizer |
|
|
4 |
from sklearn.feature_extraction.text import HashingVectorizer |
|
|
5 |
from sklearn.feature_extraction.text import TfidfTransformer |
|
|
6 |
from sklearn.preprocessing import Normalizer |
|
|
7 |
from sklearn import metrics |
|
|
8 |
|
|
|
9 |
from sklearn.cluster import KMeans, MiniBatchKMeans |
|
|
10 |
|
|
|
11 |
import logging |
|
|
12 |
import sys |
|
|
13 |
from time import time |
|
|
14 |
|
|
|
15 |
import numpy as np |
|
|
16 |
import pandas as pd |
|
|
17 |
|
|
|
18 |
data = pd.read_csv("cleansmplinds.csv", sep = ",", quoting = 1, quotechar = '"') |
|
|
19 |
dataset = [] |
|
|
20 |
data = np.array(data) |
|
|
21 |
for x in data : |
|
|
22 |
count = 0 |
|
|
23 |
placeholder = "" |
|
|
24 |
for y in x : |
|
|
25 |
if (count == 2) : |
|
|
26 |
placeholder = y + ""; |
|
|
27 |
elif (count == 3) : |
|
|
28 |
if y == True : |
|
|
29 |
dataset.append(str(1) + " " + placeholder) |
|
|
30 |
else : |
|
|
31 |
dataset.append(str(0) + " " + placeholder) |
|
|
32 |
count=count+1 |
|
|
33 |
|
|
|
34 |
vectorizer = TfidfVectorizer(max_df=0.5, max_features=10000, min_df=2, stop_words='english', use_idf=True) |
|
|
35 |
X = vectorizer.fit_transform(dataset) |
|
|
36 |
print(X.shape) |
|
|
37 |
|
|
|
38 |
Ks = range(1, 2001, 50) |
|
|
39 |
|
|
|
40 |
km = [KMeans(n_clusters=i, init='k-means++', max_iter=300, n_init=1,verbose=True) for i in Ks] |
|
|
41 |
score = [km[i].fit(X).score(X) for i in range(len(km))] |
|
|
42 |
|
|
|
43 |
f1 = open("./elbow342test.txt", "a") |
|
|
44 |
for x in range(len(score)) : |
|
|
45 |
f1.write(str(Ks[x]) + " " + str(score[x]) + "\n") |
|
|
46 |
|