如何解决重复运行该程序的不同 k 均值结果
此计划的目的是:
- 读取数据集:行是客户,列是客户购买的产品
- 应用主成分分析来减少特征数量
- 应用k-means确定每个客户所属的集群
- 在结构与原始数据集相同但值不同的新数据集上执行步骤 1、2
- 将步骤 3 中确定的 k-means 模型应用于新数据集
问题是重复运行会根据客户属于哪个集群给出不同的结果。一定有我无法找到的错误。提前致谢。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
def get_kmeans_score(data,center):
'''
returns the kmeans score regarding SSE for points to centers
INPUT:
data - the dataset you want to fit kmeans to
center - the number of centers you want (the k value)
OUTPUT:
score - the SSE score for the kmeans model fit to the data
'''
#instantiate kmeans
kmeans = KMeans(n_clusters=center)
# Then fit the model to your data using the fit method
model = kmeans.fit(data)
# Obtain a score related to the model fit
score = np.abs(model.score(data))
return score
data = {
'apples': [3,2,9,1],'oranges': [0,7.6,7,6],'figs':[1.4,11,10.999,3.99,10,2],'pears': [5,6,2.45,1,7],'berries': [1.3,4,5,21],'tomatoes': [5,15,3,17,5],'onions': [11,10]
}
purchases = pd.DataFrame(data,index=['June','Robert','Lily','David','Bob','Karen'])
print('ORIGINAL DATA')
print(purchases)
Y1 = pd.DataFrame(np.round(purchases,0),columns = purchases.keys())
scaler = StandardScaler()
Y = scaler.fit_transform(Y1)
pca = PCA(n_components=3)
W = pca.fit_transform(Y)
# apply k-means
scores = []
centers = list(range(1,5))
for center in centers:
scores.append(get_kmeans_score(W,center))
X = zip(centers,scores)
print('k-means results on original data as a function of # centers')
for i in X:
print(i)
# from the above results,assume the elbow is 4 clusters
print('_________________________________________')
n_c = 4
#kmeans = KMeans(n_clusters=4,random_state=int)
kmeans = KMeans(n_clusters=4)
model = kmeans.fit(W)
score = np.abs(model.score(W))
print('k-means score on ',n_c,' clusters for the original dataset = ',score)
# model is the k-means model that will also be applied to the new dataset
#
NEW_data = {
'apples': [9,20,12,'oranges': [10,18,'figs':[34,3.999,12],16,11],'berries': [13,4],'tomatoes': [7,14,27,'onions': [1,10]
}
purchases_N = pd.DataFrame(NEW_data)
purchases_N = pd.DataFrame(NEW_data,'Karen'])
print('NEW DATA')
print(purchases_N)
YY1 = pd.DataFrame(np.round(purchases_N,columns = purchases_N.keys())
YY = scaler.fit_transform(YY1)
W1 = pca.transform(YY)
scoreNew = np.abs(model.score(W1))
print('k-means score on ',' clusters for the new dataset = ',scoreNew)
print(scoreNew)
# k-means score the new dataset using the model determined on original ds
# predictions for the 2 datasets using the k-means model based on orig data
predict_purchases_dataset = model.predict(W)
predict_purchases_NewDataset = model.predict(W1)
print('original data upon PCA using n_components=3')
print(W)
print('k-means predictions --- original data')
print(predict_purchases_dataset)
print('_________________________________________')
print('new data upon PCA using n_components=3')
print(W1)
print('k-means predictions --- new data')
print(predict_purchases_NewDataset)
# the output matches the prediction on orig dataset:
# there are 2 customers in cluster 2,2 customers in cluster 1,1 in cluster 3 and 1 in 0
L = len(purchases.index)
x = [i for i in range (10)]
orig = []
NEW = []
for i in range(10):
orig.append((predict_purchases_dataset== i).sum()/L)
NEW.append((predict_purchases_NewDataset== i).sum()/L)
print('proportion of k-means clusters for original data')
print(orig)
print('proportion of k-means clusters for new data')
print(NEW)
#df_summary = pd.DataFrame({'cluster' : x,'propotion_orig' : orig,'proportion_NEW': NEW})
#df_summary.plot(x='cluster',y= ['propotion_orig','proportion_NEW' ],kind='bar')
model.cluster_centers_
#
IPCA = pca.inverse_transform(model.cluster_centers_)
APPROX = scaler.inverse_transform(IPCA)
approx_df =pd.DataFrame(APPROX,columns=purchases.columns)
print('k-means centers coordinates in original features space')
print('k-means centers coordinates in original features space')
print(approx_df)
第一次运行
k-means predictions --- original data
[3 1 0 2 1 0]
k-means predictions --- new data
[1 2 0 1 1 0]
第二次运行
k-means predictions --- original data
[1 2 0 3 2 0]
k-means predictions --- new data
[2 3 0 2 2 0]
解决方法
两次运行实际上都为您提供了相同的结果。 KMeans
生成的标签没有任何意义,是任意值,可让您了解数据已分配到哪个集群。如果您查看示例中构建的集群,您会发现它们是相同的,只是在重新训练模型后调用方式不同(“0”仍为“0”,“1”变为“2”、“2”变成了“3”,“3”变成了“1”)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。