我有分类数据,我应用了一个热的编码能够使一个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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。