-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsift_kmeans
More file actions
69 lines (49 loc) · 1.54 KB
/
Copy pathsift_kmeans
File metadata and controls
69 lines (49 loc) · 1.54 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 4 16:24:51 2017
@author: jmarnat
"""
#from PIL import Image
#import cv2
import matplotlib.pyplot as plt
from os import listdir
from os import chdir
#from multiprocessing import Pool
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score
from scipy.spatial.distance import cdist
chdir('/home/jmarnat/Documents/CV/cvp-master')
path = "../VOCdevkit/VOC2007/JPEGImages/"
list_files = listdir(path)
list_files.sort()
X_train = pd.read_csv('X_sifts-0.20.csv')
X_train = X_train.drop('Unnamed: 0',axis=1)
X_train = np.array(X_train)
y_train = pd.read_csv("y_sifts-0.20.csv")
y_train = y_train.drop(y_train.columns[[0]],axis=1)
#y_train = y_train.values
# train // test
#from sklearn.model_selection import train_test_split
#X_train, X_test, y_train, y_test = train_test_split(
# X_train, y_train, test_size=0.33, random_state=42)
#
# testing kmeans on 10k / 500k lines
from sklearn.cluster import KMeans
X = X_train[[range(10000)]]
K = range(100,2000,100)
#K = range(100,200,10)
distortions = []
for k in K:
print("next k;")
kmeanModel = KMeans(n_clusters=k)
kmeanModel.fit(X)
distortions.append(sum(np.min(cdist(X, kmeanModel.cluster_centers_, 'euclidean'), axis=1)) / X.shape[0])
print(distortions[len(distortions)-1])
# for the whole dataset and k=200, it takes (09:47:20 - 09:36:20)
plt.plot(K, distortions, 'bx-')
plt.xlabel('k')
plt.ylabel('Distortion')
plt.title('The Elbow Method showing the optimal k')
plt.show()