Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.2 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_scaled)
y_kmeans = kmeans.labels_


y_kmeans


from sklearn.metrics import adjusted_rand_score, normalized_mutual_info_score

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
labels_true = le.fit_transform(df['label'])

ari = adjusted_rand_score(labels_true,y_kmeans)
nmi = normalized_mutual_info_score(labels_true, y_kmeans, average_method='arithmetic')

print(f'Adjusted Rand Index {ari:.3f}')
print(f'Normaliz Mutual Info {nmi:.3f}')
Leave a Comment