3.Sklearn kutubxonasidan foydalangan holda K-Means va Ierarxik klasterlash modelini qurish(klasterlar sonini variantdan olish).
Ierarxik klasterlash:
from sklearn.cluster import AgglomerativeClustering
cluster = AgglomerativeClustering(
n_clusters=2, affinity='euclidean', linkage='ward')
cluster.fit(X_train)
Y_hierarchical = cluster.labels_
Y_hierarchical
i mport scipy
from scipy.cluster import hierarchy
clusters = hierarchy.linkage(X_train, method="single")
def plot_dendrogram(clusters):
plt.figure(figsize=(20,6))
dendrogram = hierarchy.dendrogram(clusters, labels=Y_hierarchical, orientation="top",leaf_font_size=9, leaf_rotation=360)
plt.ylabel('Euclidean Distance');
plot_dendrogram(clusters)
plt.figure(figsize=(12,6))
plt.subplot(1,2,1)
plt.title('Orginal')
plt.xlabel('Pul miqdori(so\'m)')
plt.ylabel('Pulning ortishi(so\'m)')
plt.scatter(*X_train[Y_train==0].T,s=50, alpha=0.8,label='sinf-0')
plt.scatter(*X_train[Y_train==1].T,s=50, alpha=0.8,label='sinf-1')
plt.legend()
plt.subplot(1,2,2)
plt.title('Hierarchical')
plt.xlabel('Pul miqdori(so\'m)')
plt.ylabel('Pulning ortishi(so\'m)')
plt.scatter(*X_train[Y_hierarchical==0].T,s=50, alpha=0.8,label='Cluster-0')
plt.scatter(*X_train[Y_hierarchical==1].T,s=50, alpha=0.8,label='Cluster-1')
plt.legend()
plt.show()
Y_hierarchical = cluster.labels_
Y_hierarchical
Y_train
Y_hierarchical=Y_hierarchical+5
Y_hierarchical
Y_hierarchical[Y_hierarchical==5]=1
Y_hierarchical[Y_hierarchical==6]=0
Y_hierarchical
from sklearn.metrics import confusion_matrix, accuracy_score
result = confusion_matrix(Y_train, Y_hierarchical)
print("Confusion Matrix:")
print(result)
result2 = accuracy_score(Y_train,Y_hierarchical)
print("Accuracy:",result2)
Dostları ilə paylaş: |