-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_kmeans_levels.py
52 lines (39 loc) · 1.65 KB
/
app_kmeans_levels.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
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt
import matplotlib.ticker as mpticker
import pandas as pd
import numpy
def get_optimum_clusters(df, saturation_point=0.05):
'''
:param df: dataframe
:param saturation_point: The amount of difference we are willing to detect
:return: clusters with optimum K centers
This method uses elbow method to find the optimum number of K clusters
We initialize different K-means with 1..10 centers and compare the inertias
If the difference is no more than saturation_point, we choose that as K and move on
'''
wcss = []
k_models = []
size = min(11, len(df.index))
for i in range(1, size):
kmeans = KMeans(n_clusters=i, init='k-means++', max_iter=300, n_init=10, random_state=0)
kmeans.fit(df)
wcss.append(kmeans.inertia_)
k_models.append(kmeans)
# Compare differences in inertias until it's no more than saturation_point
optimum_k = len(wcss)-1
for i in range(0, len(wcss)-1):
diff = abs(wcss[i+1] - wcss[i])
if diff < saturation_point:
optimum_k = i
break
optimum_clusters = k_models[optimum_k]
lows = pd.DataFrame(data=df, index=df.index, columns=["low"])
highs = pd.DataFrame(data=df, index=df.index, columns=["high"])
low_clusters = get_optimum_clusters(lows)
low_centers = low_clusters.cluster_centers_
low_centers = numpy.sort(low_centers, axis=0)
high_clusters = get_optimum_clusters(highs)
high_centers = high_clusters.cluster_centers_
high_centers = numpy.sort(high_centers, axis=0)
return optimum_clusters , low_centers, high_centers