前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >机器学习-K均值算法(K-Means)案例

机器学习-K均值算法(K-Means)案例

作者头像
XXXX-user
发布2019-10-14 17:40:33
1.2K0
发布2019-10-14 17:40:33
举报
文章被收录于专栏:不仅仅是python不仅仅是python

背景介绍

这是一种无监督算法,可以解决聚类问题。它的过程遵循一种简单的方法,可以通过一定数量的聚类(假设k个聚类)对给定的数据集进行分类。集群中的数据点对同级组是同质的,并且是异构的。

还记得从墨水印迹中找出形状吗? k表示此活动有点类似。 您查看形状并展开以解释存在多少个不同的群集/种群!

K-均值如何形成聚类:

  1. K均值为每个群集选取k个点,称为质心。
  2. 每个数据点形成具有最接近质心的群集,即k个群集。
  3. 根据现有集群成员查找每个集群的质心。在这里,我们有了新的质心。
  4. 当我们有了新的质心时,请重复步骤2和3。找到每个数据点与新质心的最近距离,并与新的k簇相关联。重复此过程,直到会聚发生为止,即质心不变。

如何确定K的值:

在K均值中,我们有聚类,每个聚类都有自己的质心。 质心和群集中数据点之间的差平方和构成该群集的平方值之和。 同样,当所有聚类的平方和相加时,它成为聚类解的平方和之内的总和。

我们知道,随着簇数的增加,该值会不断减少,但是如果绘制结果,您可能会看到平方距离的总和急剧减小,直到达到某个k值,然后才逐渐减小。 在这里,我们可以找到最佳的群集数量。

下面来看使用Python实现的案例:

代码语言:javascript
复制
'''
The following code is for the K-Means
Created by - ANALYTICS VIDHYA
'''

# importing required libraries
import pandas as pd
from sklearn.cluster import KMeans

# read the train and test dataset
train_data = pd.read_csv('train-data.csv')
test_data = pd.read_csv('test-data.csv')

# shape of the dataset
print('Shape of training data :',train_data.shape)
print('Shape of testing data :',test_data.shape)

# Now, we need to divide the training data into differernt clusters
# and predict in which cluster a particular data point belongs.  

'''
Create the object of the K-Means model
You can also add other parameters and test your code here
Some parameters are : n_clusters and max_iter
Documentation of sklearn KMeans: 

https://scikit-learn.org/stable/
modules/generated/sklearn.cluster.KMeans.html
 '''

model = KMeans()  

# fit the model with the training data
model.fit(train_data)

# Number of Clusters
print('\nDefault number of Clusters : ',model.n_clusters)

# predict the clusters on the train dataset
predict_train = model.predict(train_data)
print('\nCLusters on train data',predict_train) 

# predict the target on the test dataset
predict_test = model.predict(test_data)
print('Clusters on test data',predict_test) 

# Now, we will train a model with n_cluster = 3
model_n3 = KMeans(n_clusters=3)

# fit the model with the training data
model_n3.fit(train_data)

# Number of Clusters
print('\nNumber of Clusters : ',model_n3.n_clusters)

# predict the clusters on the train dataset
predict_train_3 = model_n3.predict(train_data)
print('\nCLusters on train data',predict_train_3) 

# predict the target on the test dataset
predict_test_3 = model_n3.predict(test_data)
print('Clusters on test data',predict_test_3)

运行结果:

代码语言:javascript
复制
Shape of training data : (100, 5)
Shape of testing data : (100, 5)

Default number of Clusters :  8

CLusters on train data [6 7 0 7 6 5 5 7 7 3 1 1 3 0 7 1 0 4 5 6 4 3 3 0 4 0 1 1 0 3 4 3 3 0 0 1 2
 1 4 3 0 2 1 1 0 3 3 0 7 1 3 0 5 1 0 1 5 4 6 4 3 6 5 0 3 0 4 33 1 5 1 6 5
 7 7 6 3 5 3 5 3 1 5 2 5 0 3 2 3 4 7 1 0 1 5 3 6 1 6]
Clusters on test data [3 6 2 0 5 6 0 3 5 2 3 4 5 5 5 3 3 5 5 70 0 5 5 3 5 0 6 5 0 1 6 3 5 6 0 1
 7 3 0 0 6 2 0 5 3 5 7 3 3 4 6 3 1 6 3 1 3 3 2 3 3 5 1 7 5 1 53 3 5 2 0 1
 5 0 3 0 3 6 3 5 4 0 2 6 3 5 6 0 6 4 3 5 0 6 6 6 1 0]

Number of Clusters :  3

CLusters on train data [2 0 1 0 2 1 2 0 0 2 0 0 2 1 0 0 1 2 2 2 2 2 2 1 2 1 0 0 1 2 2 2 2 1 1 0 2
 0 2 2 1 2 0 0 1 2 2 1 0 0 2 1 2 0 1 0 2 2 2 2 2 2 2 1 2 1 2 22 0 1 0 2 2
 0 0 0 2 0 2 2 2 0 2 2 2 1 2 2 2 2 0 0 1 0 2 2 2 0 2]
Clusters on test data [2 2 2 1 2 2 1 2 2 2 2 2 2 1 1 2 2 2 2 01 1 2 2 2 2 1 2 2 1 0 2 2 2 2 1 0
 0 2 1 1 2 2 1 2 2 2 0 2 2 2 2 2 0 2 2 0 2 2 2 2 2 2 0 0 2 0 22 2 0 2 1 0
 2 1 2 1 2 0 2 2 2 1 2 2 2 2 2 1 2 2 2 2 1 2 2 2 0 1]

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 yale记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景介绍
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档