K-Means klasterlash:
X_train= data_set.iloc[:,[1,3]].values # 2 ta xususiyatni X ga o'zlashtirdim
Y_train= data_set.iloc[:,-1].values # Sinfni Y ga o'zlashtirdim
from sklearn.cluster import KMeans
Kmean=KMeans(max_iter=300,n_clusters=2)
Kmean.fit(X_train)
center=Kmean.cluster_centers_
center
Y_kmeans=Kmean.labels_
Y_kmeans
plt.figure(figsize=(12,6))
plt.subplot(1,2,1)
plt.title('Orginal')
plt.xlabel('Pul miqdori(so\'m)')
plt.ylabel('Pulning ortish(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('Kmeans')
plt.xlabel('Pul miqdori(so\'m)')
plt.ylabel('Pulning ortish(so\'m)')
plt.scatter(*X_train[Y_kmeans==0].T,s=50, alpha=0.8,label='Cluster-0')
plt.scatter(*X_train[Y_kmeans==1].T,s=50, alpha=0.8,label='Cluster-1')
plt.legend()
for i in center:
plt.scatter(i[0],i[1], s=50, c='k', marker='o')
plt.show()
Y_kmeans=Y_kmeans
Y_kmeans
Y_train
Y_kmeans=Y_kmeans+5
Y_kmeans
Y_kmeans[Y_kmeans==5] = 1
Y_kmeans[Y_kmeans==6] = 0
Y_kmeans
Y_train
from sklearn.metrics import confusion_matrix, accuracy_score
result = confusion_matrix(Y_train, Y_kmeans)
print("Confusion Matrix:")
print(result)
result2 = accuracy_score(Y_train,Y_kmeans)
print("Accuracy:",result2)
Dostları ilə paylaş: |