def ansEMTrainModel(model,cls,Xtest,ytest): """ @note: 聚类模型分析 """ #K-Means算法聚类中心点…
- User
def ansEMTrainModel(model,cls,X_test,y_test): """ @note: 聚类模型分析 """ #K-Means算法聚类中心点 centers = cls.cluster_centers_ #预测类别 labels = cls.labels_ ##ARI(调整兰德指数) ari = adjusted_rand_score(y_test,labels) #均一性与完整性的加权平均 vms = v_measure_score(y_test,labels) #AMI(调整互信息) ami = adjusted_mutual_info_score(y_test,labels) #轮廓系数 ss = silhouette_score(X_test, labels,metric='euclidean') #中心对应的类别 centerClass = cls.predict(centers) variables = np.concatenate((centers,centerClass.reshape(-1,1)),axis=1).tolist() columns = model.getInputVars()+['class'] df = pd.DataFrame(np.concatenate((X_test,cls.predict(X_test).reshape(-1,1)),axis=1),columns = columns) #data = [df[(df[columns[-1]] == val)][columns].values.tolist() for val in labels] return df,{"ari":ari,"vms":vms,"ami":ami,"ss":ss,"variables":[{columns[i]:each[i] for i in range(len(columns))} for each in variables]}
- Assistant
The function
ansEMTrainModeltakes in a clustering model,model, aKMeansclustering object,cls, as well as the test data,X_testandy_test.The function first calculates the cluster centers using
cls.cluster_centers_, and the predicted labels for the test data usingcls.labels_.Next, it calculates several clustering performance metrics including the Adjusted Rand Index (ARI), V-measure score (VMS), Adjusted Mutual Information (AMI), and Silhouette score (SS) using the
adjusted_rand_score,v_measure_score,adjusted_mutual_info_score, andsilhouette_scorefunctions respectively.Then, it predicts the corresponding class for each cluster center using
cls.predict(centers), and concatenates the cluster centers with their predicted classes.Finally, it creates a DataFrame using
pd.DataFramewith the test data and their predicted labels, and returns it along with the calculated metrics and the cluster centers with their predicted classes as a list of dictionaries.The function returns the DataFrame, the calculated metrics, and the cluster centers with their predicted classes.