开发者社区> 问答> 正文

如何使Kmeans与一个热点编码数据?

我有分类数据,我应用了一个热的编码能够使一个Kmeans。但是在绘制之后,我在图上只有3个点,我无法理解。请帮助我。

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder

#load my data
myData = pd.read_excel('panelForOneHot.xlsx')
myData = myData.dropna()
myData.reset_index(drop = True, inplace = True)
myData
myData = myData.drop(['Unnamed: 0'], axis =1)

#integer encode
label_encoder = LabelEncoder()
myData['PRENOM'] = pd.to_numeric(myData['PRENOM'], errors='coerce')
myData['CODE_POSTAL'] = pd.to_numeric(myData['CODE_POSTAL'], errors='coerce')
myData['TELEPHONE'] = pd.to_numeric(myData['TELEPHONE'], errors='coerce')

integer_encoded= myData.apply(label_encoder.fit_transform)
integer_encoded

#binary encode
onehot_encoder = OneHotEncoder(sparse=False)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
print(onehot_encoded)

from sklearn.cluster import KMeans

kmeans = KMeans(n_clusters=4, random_state = 5)
kmeans.fit(onehot_encoded)

plt.scatter(onehot_encoded[:, 0], onehot_encoded[:, 1], c=y_kmeans, s=50, cmap='viridis')

centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5);

[![image of plotting][1]][1]


  [1]: https://i.stack.imgur.com/dOUWl.png

问题来源StackOverflow 地址:/questions/59467132/how-to-make-kmeans-with-one-hot-encoding-data

展开
收起
kun坤 2019-12-25 09:32:50 300 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载