-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
178 lines (127 loc) · 5.48 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.cluster import AgglomerativeClustering, KMeans, DBSCAN
from sklearn.decomposition import PCA
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from clusters_class.agglomerative_cluster import AgglomerativeCluster
from clusters_class.kmean_cluster import KMeansCluster
from clusters_class.dbscan_cluster import DBSCANCluster
class Cluster(AgglomerativeCluster, KMeansCluster, DBSCANCluster):
cluster_dic = {'agglo': AgglomerativeClustering(),
'kmean': KMeans(),
'dbscan': DBSCAN()}
scaler_dic = {'StandardScaler': StandardScaler(),
'MinMaxScaler': MinMaxScaler()}
cluster = None
pipe = None
scaler = None
result_df = None
def __init__(self, X: pd.DataFrame):
self.df: pd.DataFrame = X
self.X: pd.DataFrame = X
def preprocessing(self, scaler='StandardScaler'):
# DESCRIPTION:
# Preprocess the data with MinMaxScaler & StandardScaler
self.scaler = self.scaler_dic[scaler]
scaled_data = self.scaler.fit_transform(self.X)
self.X = pd.DataFrame(scaled_data)
def build_cluster(self, cluster='kmean', **kwargs):
# DESCRIPTION:
# Build selected amount of cluster
self.cluster = self.cluster_dic[cluster](**kwargs)
cluster_labels = self.cluster.fit_predict(self.X)
self.result_df = self.df
self.result_df['cluster'] = cluster_labels
def build_pipe(self):
self.pipe = make_pipeline(self.scaler, self.cluster)
return self.pipe
def simple_check(self, mode='built', target='cluster', alpha=0.5):
# DESCRIPTION:
# Check data with PCA preprocessing including DataFrame
# built in build_knife and build_simple method
# ARGUMENTS:
# mode - tested mode, data from which DataFrame will be taken
# target - hue for scatterplot diagram in case of using cluster = 'origin'
# alpha - alpha for scatterplot
if mode == 'origin':
X = self.df
df = self.df
target = self.df[target]
elif mode == 'built':
X = self.result_df
df = self.result_df
elif mode == "outliers":
X = self.result_df
df = self.result_df
target = np.where(df['cluster'] == -1, 'outliers', 'normal')
scaler = StandardScaler()
X = scaler.fit_transform(X)
pca = PCA(n_components=2)
principal_components = pca.fit_transform(X)
X = pd.DataFrame(principal_components)
print(f'pca.explained_variance_ratio_ = '
f'{pca.explained_variance_ratio_}')
print(f'np.sum(pca.explained_variance_ratio_ = '
f'{np.sum(pca.explained_variance_ratio_)}')
plt.figure(figsize=(8,6))
sns.scatterplot(x=X[0], y=X[1], data=df, hue=target, alpha=alpha)
plt.xlabel('First principal component')
plt.ylabel('Second Principal Component')
plt.show()
def after_pie(self, bins=20):
# DESCRIPTION:
# Build distribution diagram
if len(self.result_df['cluster'].value_counts()) < 10:
cluster_counts = self.result_df['cluster'].value_counts()
plt.pie(cluster_counts, labels=cluster_counts.index, autopct='%1.1f%%')
else:
sns.displot(data=self.result_df, x='cluster', kde=True, color='green', bins=bins)
plt.show()
def after_heat(self, sh=12, vi=4):
# DESCRIPTION:
# Build feature correlation diagram for 'knife' or 'simple' algorithm
# ARGUMENTS:
# cluster - tested mode
# sh - height of diagram
# vi - length of diagram
cat_means = self.result_df.groupby('cluster').mean()
scaler = MinMaxScaler()
data = scaler.fit_transform(cat_means)
scaled_means = pd.DataFrame(data, cat_means.index, cat_means.columns)
if scaled_means.reset_index().iloc[0]['cluster'] == -1:
ax = plt.figure(figsize=(sh,vi), dpi=200)
ax = sns.heatmap(scaled_means.iloc[1:], annot=True, cmap='Greens')
else:
ax = plt.figure(figsize=(sh, vi), dpi=200)
ax = sns.heatmap(scaled_means, annot=True, cmap='Greens')
return ax
def dendrogram(self, n_clusters=2):
# DESCRIPTION:
# Check the optimal amount of cluster by scipy.cluster.hierarchy
# By using this diagram, it is possible to make an assessment
# of the chosen amount of clusters on actual data
Z = linkage(self.X, 'ward')
plt.figure(figsize=(10, 8))
dendrogram(Z, color_threshold=np.sqrt(len(self.X.columns)))
plt.xticks(rotation=90);
plt.title('Automatic Clustering')
plt.show()
# Plot dendrogram with specified number of clusters
plt.figure(figsize=(10, 8))
plt.title('Custom Clustering')
plt.xlabel('Sample index')
plt.ylabel('Distance')
dendrogram(
Z,
leaf_rotation=90,
leaf_font_size=8.,
labels=np.arange(1, len(self.X) + 1),
color_threshold=Z[-n_clusters + 1, 2]
)
plt.axhline(y=Z[-n_clusters + 1, 2], color='r', linestyle='--')
plt.xticks(rotation=90)
plt.show()