-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_kmeans.py
78 lines (66 loc) · 2.01 KB
/
basic_kmeans.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
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import clusters
import plot
data = clusters.get_data()
num_clusters = 4
pipe = clusters.get_pipeline_kmeans(num_clusters)
pipe.fit(data)
_, _, data_pca = clusters.get_pca_data_kmeans(num_clusters)
data_with_clusters = data
data_with_clusters['predicted_cluster'] = pipe.predict(data)
mean_vals = data_with_clusters.groupby('predicted_cluster', as_index=False).mean()
max_vals = data_with_clusters.groupby('predicted_cluster', as_index=False).max()
min_vals = data_with_clusters.groupby('predicted_cluster', as_index=False).min()
std_dev_vals = data_with_clusters.groupby('predicted_cluster', as_index=False).std()
# print(min_vals.to_markdown())
# Finally we plot all of our data and make it look a bit pretty
plt.style.use("fivethirtyeight")
plt.figure(figsize=(8, 8))
sns.scatterplot(
x="component_1",
y="component_2",
s=50,
data=data_pca,
hue="predicted_cluster",
style="predicted_cluster",
palette="Set2",
)
plt.title("K-Means clustering")
plt.show()
plt.style.use("fivethirtyeight")
plt.figure(figsize=(8, 8))
sns.scatterplot(
x="component_1",
y="component_3",
s=50,
data=data_pca,
hue="predicted_cluster",
style="predicted_cluster",
palette="Set2",
)
plt.title("K-Means clustering")
plt.show()
plt.style.use("fivethirtyeight")
plt.figure(figsize=(8, 8))
sns.scatterplot(
x="component_2",
y="component_3",
s=50,
data=data_pca,
hue="predicted_cluster",
style="predicted_cluster",
palette="Set2",
)
plt.title("K-Means clustering")
plt.show()
plt.style.use("fivethirtyeight")
plt.figure(figsize=(8, 8))
ax = plt.axes(projection='3d')
ax.scatter3D(data_pca['component_1'], data_pca['component_2'], data_pca['component_3'], c=data_pca['predicted_cluster'], cmap='Greens')
plt.show()
# Create a data frame containing our centroids
plot.display_parallel_coordinates(data_with_clusters, num_clusters)
plot.display_parallel_coordinates_centroids(mean_vals, num_clusters)