Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.5 kB
1
Indexable
Never
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D


features = ['MO_dealer_monthly_ton_GZPN', 'KP_dealer_monthly_ton_GZPN', 'total_monthly_ton_dil']
df_clustering = df[features].dropna()  # Удаляем пропущенные значения

# Стандартизация признаков
scaler = StandardScaler()
df_scaled = scaler.fit_transform(df_clustering)

# Применяем KMeans
kmeans = KMeans(n_clusters=4, random_state=42)
df_clustering['cluster'] = kmeans.fit_predict(df_clustering)
y_kmeans = kmeans.labels_




# Выполняем PCA для уменьшения размерности до 3
pca = PCA(n_components=3)
features_pca_3d = pca.fit_transform(df_scaled)

# Визуализация результатов PCA
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Различные цвета для различных кластеров
colors = ['blue', 'green', 'red', 'purple']

for i in range(4):
    ax.scatter(features_pca_3d[y_kmeans == i, 0], features_pca_3d[y_kmeans == i, 1], features_pca_3d[y_kmeans == i, 2], 
               c=colors[i], label=f'Cluster {i}')

ax.set_title('3D PCA Visualization of Client Clusters')
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_zlabel('PC3')
ax.legend()

plt.show()
Leave a Comment