KMeans案例

15 篇文章 0 订阅
11 篇文章 0 订阅
#coding=utf-8
"""
   2017.3.14     天气数据分析
"""
import os
import xlrd
data = []
xls =xlrd.open_workbook("weather.xls")

sheet = xls.sheets()[0]

result1 = sheet.col_values(2)
print result1
result2 = sheet.col_values(3)

T = dict(zip(result1,result2))
type(T)
print u'List'
X = list(map(lambda x,y: (x,y), T.keys(),T.values()))
print X
print type(X)
print len(X)

"""
KMeans聚类
clf = KMeans(n_clusters=3) 表示类簇数为3,聚成3类数据,clf即赋值为KMeans
y_pred = clf.fit_predict(X) 载入数据集X,并且将聚类的结果赋值给y_pred
"""

from sklearn.cluster import Birch
from sklearn.cluster import KMeans

clf = KMeans(n_clusters=3)
y_pred = clf.fit_predict(X)
print(clf)

print(y_pred)

"""
可视化绘图
Python导入Matplotlib包,专门用于绘图
import matplotlib.pyplot as plt 此处as相当于重命名,plt用于显示图像
"""

import numpy as np
import matplotlib.pyplot as plt

# 获取第一列和第二列数据 使用for循环获取 n[0]表示X第一列
x = [n[0] for n in X]
print x
y = [n[1] for n in X]
print y

# 绘制散点图 参数:x横轴 y纵轴 c=y_pred聚类预测结果 marker类型 o表示圆点 *表示星型 x表示点
# plt.scatter(x, y, c=y_pred, marker='x')


# 坐标
x1 = []
y1 = []

x2 = []
y2 = []

x3 = []
y3 = []

# 分布获取类标为0、1、2的数据 赋值给(x1,y1) (x2,y2) (x3,y3)
i = 0
while i < len(X):
    if y_pred[i] == 0:
        x1.append(X[i][0])
        y1.append(X[i][1])
    elif y_pred[i] == 1:
        x2.append(X[i][0])
        y2.append(X[i][1])
    elif y_pred[i] == 2:
        x3.append(X[i][0])
        y3.append(X[i][1])

    i = i + 1

# 四种颜色 红 绿 蓝 黑
plot1, = plt.plot(x1, y1, 'or', marker="x")
plot2, = plt.plot(x2, y2, 'og', marker="o")
plot3, = plt.plot(x3, y3, 'ob', marker="*")

# 绘制标题
plt.title("Kmeans-HTemp-LTemp")

# 绘制x轴和y轴坐标
plt.xlabel("Htemp")
plt.ylabel("Ltemp")

# 设置右上角图例
plt.legend((plot1, plot2, plot3), ('A', 'B', 'C'), fontsize=10)

plt.show()

Kmeans聚类算法是一种无监督学习算法,用于将相似的对象归到一个类(簇)中。其算法流程如下:首先选择聚类的个数k,然后随机产生k个聚类中心,对每个点确定其聚类中心点,并计算其聚类新中心。重复以上步骤直到满足收敛要求,通常是确定的中心点不再改变。\[1\] Kmeans算法的原理是通过计算数据点之间的欧氏距离来确定它们之间的相似性。欧氏距离是一种常用的距离度量方法,用于衡量两个点之间的距离。\[2\] 关于Kmeans聚类算法的案例,可以参考\[1\]中提到的Kmeans算法流程案例,或者参考\[3\]中的普通K-Means算法代码实现案例。这些案例可以帮助你更好地理解和应用Kmeans聚类算法。 #### 引用[.reference_title] - *1* [Kmeans算法及简单案例](https://blog.csdn.net/m0_47482052/article/details/128548935)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [K-Means算法及相关案例](https://blog.csdn.net/Alian_W/article/details/108217564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值