如何在scikit-learn中获得有意义的kmeans结果

我的数据集看起来像这样:

{‘dns_query_count’: ’11’, ‘http_hostnames_count’: ‘7’, ‘dest_port_count’: ‘3’, ‘ip_count’: ’11’, ‘signature_count’: ‘0’, ‘src_ip’: ‘10.0.64.42’, ‘http_user_agent_count’: ‘2’}

这些数据已经从csv文件转换为字典格式

然后我使用DictVectorizer进行转换

from sklearn.feature_extraction import DictVectorizervec = DictVectorizer()d = vec.fit_transform(data).toarray()

接着我尝试在其上使用Kmeans

from sklearn.cluster import KMeansk = KMeans(n_clusters=2).fit(d)

我的问题是如何获取每行数据所属的聚类信息?

我希望得到类似这样的结果:

{‘dns_query_count’: ’11’, ‘http_hostnames_count’: ‘7’, ‘dest_port_count’: ‘3’, ‘ip_count’: ’11’, ‘signature_count’: ‘0’, ‘src_ip’: ‘10.0.64.42’, ‘http_user_agent_count’: ‘2’, cluster: ‘1’}

能否有人提供一个从原始数据到带有聚类信息的数据的逐步示例?

例如,我使用Weka处理了这个数据集,它显示了我想要的结果 – 我可以点击图表上的数据点并准确读取哪些数据点属于哪个聚类。如何在sklearn中获得类似的结果?


回答:

这将展示如何检索每行的聚类ID和聚类中心。我还测量了每行到每个中心的距离,以便您可以看到行被正确分配到聚类中。

In [1]:import pandas as pdfrom sklearn.cluster import KMeansfrom numpy.random import randomfrom scipy.spatial.distance import euclidean# 我将生成一些随机数据,以便您可以直接复制并查看其工作情况random_data = []for i in range(0,10):    random_data.append({'dns_query_count': random(), 'http_hostnames_count': random(), 'dest_port_count': random(), 'ip_count': random(), 'signature_count': random(), 'src_ip': random(), 'http_user_agent_count': random()})df = pd.DataFrame(random_data)km = KMeans(n_clusters=2).fit(df)df['cluster_id'] = km.labels_# 获取聚类中心并计算每个点到中心的距离# 这将显示所有点都被分配到正确的聚类中def distance_to_centroid(row, centroid):    row = row[['dns_query_count',                'http_hostnames_count',                'dest_port_count',                'ip_count',                'signature_count',                'src_ip',                'http_user_agent_count']]    return euclidean(row, centroid)# 使用km.cluster_centers_获取聚类中心df['distance_to_center0'] = df.apply(lambda r: distance_to_centroid(r,    km.cluster_centers_[0]),1)df['distance_to_center1'] = df.apply(lambda r: distance_to_centroid(r,    km.cluster_centers_[1]),1)df.head()Out [1]:   dest_port_count  dns_query_count  http_hostnames_count  \0         0.516920         0.135925              0.090209   1         0.528907         0.898578              0.752862   2         0.426108         0.604251              0.524905   3         0.373985         0.606492              0.503487   4         0.319943         0.970707              0.707207      http_user_agent_count  ip_count  signature_count    src_ip  cluster_id  \0               0.987878  0.808556         0.860859  0.642014           0   1               0.417033  0.130365         0.067021  0.322509           1   2               0.528679  0.216118         0.041491  0.522445           1   3               0.780292  0.130404         0.048353  0.911599           1   4               0.156117  0.719902         0.484865  0.752840           1      distance_to_center0  distance_to_center1  0             0.846099             1.124509  1             1.175765             0.760310  2             0.970046             0.615725  3             1.054555             0.946233  4             0.640906             1.020849  

http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html#sklearn.cluster.KMeans.fit_predict

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注