我是靠谱客的博主 甜美百褶裙,这篇文章主要介绍Python之K-means详细案例,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python2.7 # -*- coding: utf-8 -*- # from __future__ import division import requests import json import numpy as np import pandas as pd import sys from sklearn.cluster import KMeans reload(sys) sys.setdefaultencoding('utf-8') # 数据标准化 def normalization(one_list): """[0,1] normaliaztion""" norm_value = (one_list - np.min(one_list)) / (np.max(one_list) - np.min(one_list)) return norm_value # 通过某接口获取的数据,并DataFrame化 def get_dt(URL): api = requests.get(url=URL) try: json_dt = api.json() except Exception as e: print str(e) if json_dt['code'] == 0: load1_info = json_dt['data']['sysload']['load1']['dps'] load1 = [load1_xi[-1] for load1_xi in load1_info] sysio_info = json_dt['data']['sysiops']['sda']['ioutil']['dps'] ioutil = [ioutil_xi[-1] for ioutil_xi in sysio_info] memused_info = json_dt['data']['sysmeminfo']['memused_percentage']['dps'] memused = [memused_xi[-1] for memused_xi in memused_info] cpuused_info = json_dt['data']['syscpuidle']['cpu']['cpuwa']['dps'] cpuused = [cpuused_xi[-1] for cpuused_xi in cpuused_info] # dt = pd.DataFrame([load1, ioutil, memused, cpuused]) dt = pd.DataFrame([normalization(load1), normalization(ioutil), normalization(memused), normalization(cpuused)]) dt = dt.T dt.columns = ['load1', 'ioutil', 'memused', 'cpuused'] else: pass return dt # 调用 if __name__ == '__main__': URL = '这里是一个接口' dt = get_dt(URL) print dt.head(100) clf_kmeans = KMeans(n_clusters=3, random_state=10).fit(dt) dt['tag_col']=clf_kmeans.labels_ df_count_type=dt.groupby('tag_col').apply(np.size) ##各个类别的数目 print df_count_type ##聚类中心 print clf_kmeans.cluster_centers_ # 获取各个类别的数据 type0 = dt[(clf_kmeans.labels_ == 0)] type1 = dt[(clf_kmeans.labels_ == 1)] type2 = dt[(clf_kmeans.labels_ == 2)] print 'type0', type0 print 'type1', type1 print 'type2', type2

最后

以上就是甜美百褶裙最近收集整理的关于Python之K-means详细案例的全部内容,更多相关Python之K-means详细案例内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(92)

评论列表共有 0 条评论

立即
投稿
返回
顶部