我正在尝试使用K-means方法进行聚类,但我想测量聚类的性能.我不是专家,但我渴望了解有关群集的更多信息.
这是我的代码:
import pandas as pd
from sklearn import datasets
#loading the dataset
iris = datasets.load_iris()
df = pd.DataFrame(iris.data)
#K-Means
from sklearn import cluster
k_means = cluster.KMeans(n_clusters=3)
k_means.fit(df) #K-means training
y_pred = k_means.predict(df)
#We store the K-means results in a dataframe
pred = pd.DataFrame(y_pred)
pred.columns = ['Species']
#we merge this dataframe with df
prediction = pd.concat([df,pred], axis = 1)
#We store the clusters
clus0 = prediction.loc[prediction.Species == 0]
clus1 = prediction.loc[prediction.Species == 1]
clus2 = prediction.loc[prediction.Species == 2]
k_list = …Run Code Online (Sandbox Code Playgroud) python cluster-analysis machine-learning scikit-learn sklearn-pandas