k-means算法 - 数据挖掘算法(5)
k-means算法,也被称为k-平均或k-均值,是数据挖掘技术中一种广泛使用的聚类算法。 它是将各个聚类子集内的所有数据样本的均值作为该聚类的代表点,算法的主要思想是通过迭代过程把数据集划分为不同的类别,使得评价聚类性能的准则函数达到最优,从而使生成的每个聚类内紧凑,类间独立。
一、计算步骤
设有n个m维向量\((X_{k1},X_{k2},\dots,X_{km}), k=1,2,\dots,n\),
1、在n个样本中随机选k个样本为簇心或类;
2、选定某种距离(如欧氏距离)作为样本间的相似性度量,计算各样本和k个簇心之间的距离,将和簇心距离最小的样本和对应簇心归为一类;
3、根据误差准则,计算类(组内)方差(各簇中心点到其它聚类点的方差)和总方差;
4、计算k个类中样本重心,从而生成新的簇心或类。
重复上面的过程,直至簇心不变。
二、算法举例
简单样本数据如下表:
S | X | Y |
---|---|---|
1 | 0 | 2 |
2 | 0 | 0 |
3 | 1.5 | 0 |
4 | 5 | 0 |
5 | 5 | 2 |
试用k-means算法进行聚类分析(\(k=2\))。
解、
1、选择\(S_1(0,2)\)、\(S_2(0,0)\)为初始的簇中心,即\(M_1=S_1(0,2)\)、\(M_2=S_2(0,0)\);
2、计算各样本和2个簇心之间的欧氏距离;
对\(S_3\):
\(D(S_3,M_2)<D(S_3,M_1)\),故将\(S_3\)分配给第2个簇心\(C_2\);
对\(S_4\):
\(D(S_4,M_2)<D(S_4,M_1)\),故将\(S_4\)分配给第2个簇心\(C_2\);
对\(S_5\):
\(D(S_2,M_2)>D(S_5,M_1)\),故将\(S_5\)分配给第1个簇心\(C_1\)。
得到新簇\(C_1{S_1,S_5}\)、\(C_2{S_2,S_3,S_4}\)。
3、计算类(组内)方差(各簇中心点到其它聚类点的方差)和总方差;
总体平均方差是:\(E=E_1+E2=25+27.25=52.25\)
4、计算2个类中样本重心,从而生成新的簇心或类
以\(M_1=(2.5,2)\)、\(M_2=(2.17,0)\)为新的簇心,重复II、III计算步骤,得到新簇\(C_1{S_1,S_5}\)、\(C_2{S_2,S_3,S_4}\)。
2个类中样本重心仍为,\(M_1=(2.5,2)\)、\(M_2=(2.17,0)\),类(组内)方差为,
总体平均误差是:\(E=E_1+E2=12.5+13.17=25.67\)
第一次迭代后,总体平均误差值由52.25降到25.67,显著减小。由于在两次迭代中,簇中心不变,所以停止迭代过程,算法停止。
三、样例代码
样例中采用鸢尾花数据,
鸢尾花[iris]数据(R语言经典聚类、分类案例数据)
ID | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
---|---|---|---|---|---|
1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
2 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
5 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
6 | 5.4 | 3.9 | 1.7 | 0.4 | setosa |
7 | 4.6 | 3.4 | 1.4 | 0.3 | setosa |
8 | 5.0 | 3.4 | 1.5 | 0.2 | setosa |
9 | 4.4 | 2.9 | 1.4 | 0.2 | setosa |
10 | 4.9 | 3.1 | 1.5 | 0.1 | setosa |
11 | 5.4 | 3.7 | 1.5 | 0.2 | setosa |
12 | 4.8 | 3.4 | 1.6 | 0.2 | setosa |
13 | 4.8 | 3.0 | 1.4 | 0.1 | setosa |
14 | 4.3 | 3.0 | 1.1 | 0.1 | setosa |
15 | 5.8 | 4.0 | 1.2 | 0.2 | setosa |
16 | 5.7 | 4.4 | 1.5 | 0.4 | setosa |
17 | 5.4 | 3.9 | 1.3 | 0.4 | setosa |
18 | 5.1 | 3.5 | 1.4 | 0.3 | setosa |
19 | 5.7 | 3.8 | 1.7 | 0.3 | setosa |
20 | 5.1 | 3.8 | 1.5 | 0.3 | setosa |
21 | 5.4 | 3.4 | 1.7 | 0.2 | setosa |
22 | 5.1 | 3.7 | 1.5 | 0.4 | setosa |
23 | 4.6 | 3.6 | 1.0 | 0.2 | setosa |
24 | 5.1 | 3.3 | 1.7 | 0.5 | setosa |
25 | 4.8 | 3.4 | 1.9 | 0.2 | setosa |
26 | 5.0 | 3.0 | 1.6 | 0.2 | setosa |
27 | 5.0 | 3.4 | 1.6 | 0.4 | setosa |
28 | 5.2 | 3.5 | 1.5 | 0.2 | setosa |
29 | 5.2 | 3.4 | 1.4 | 0.2 | setosa |
30 | 4.7 | 3.2 | 1.6 | 0.2 | setosa |
31 | 4.8 | 3.1 | 1.6 | 0.2 | setosa |
32 | 5.4 | 3.4 | 1.5 | 0.4 | setosa |
33 | 5.2 | 4.1 | 1.5 | 0.1 | setosa |
34 | 5.5 | 4.2 | 1.4 | 0.2 | setosa |
35 | 4.9 | 3.1 | 1.5 | 0.2 | setosa |
36 | 5.0 | 3.2 | 1.2 | 0.2 | setosa |
37 | 5.5 | 3.5 | 1.3 | 0.2 | setosa |
38 | 4.9 | 3.6 | 1.4 | 0.1 | setosa |
39 | 4.4 | 3.0 | 1.3 | 0.2 | setosa |
40 | 5.1 | 3.4 | 1.5 | 0.2 | setosa |
41 | 5.0 | 3.5 | 1.3 | 0.3 | setosa |
42 | 4.5 | 2.3 | 1.3 | 0.3 | setosa |
43 | 4.4 | 3.2 | 1.3 | 0.2 | setosa |
44 | 5.0 | 3.5 | 1.6 | 0.6 | setosa |
45 | 5.1 | 3.8 | 1.9 | 0.4 | setosa |
46 | 4.8 | 3.0 | 1.4 | 0.3 | setosa |
47 | 5.1 | 3.8 | 1.6 | 0.2 | setosa |
48 | 4.6 | 3.2 | 1.4 | 0.2 | setosa |
49 | 5.3 | 3.7 | 1.5 | 0.2 | setosa |
50 | 5.0 | 3.3 | 1.4 | 0.2 | setosa |
51 | 7.0 | 3.2 | 4.7 | 1.4 | versicolor |
52 | 6.4 | 3.2 | 4.5 | 1.5 | versicolor |
53 | 6.9 | 3.1 | 4.9 | 1.5 | versicolor |
54 | 5.5 | 2.3 | 4.0 | 1.3 | versicolor |
55 | 6.5 | 2.8 | 4.6 | 1.5 | versicolor |
56 | 5.7 | 2.8 | 4.5 | 1.3 | versicolor |
57 | 6.3 | 3.3 | 4.7 | 1.6 | versicolor |
58 | 4.9 | 2.4 | 3.3 | 1.0 | versicolor |
59 | 6.6 | 2.9 | 4.6 | 1.3 | versicolor |
60 | 5.2 | 2.7 | 3.9 | 1.4 | versicolor |
61 | 5.0 | 2.0 | 3.5 | 1.0 | versicolor |
62 | 5.9 | 3.0 | 4.2 | 1.5 | versicolor |
63 | 6.0 | 2.2 | 4.0 | 1.0 | versicolor |
64 | 6.1 | 2.9 | 4.7 | 1.4 | versicolor |
65 | 5.6 | 2.9 | 3.6 | 1.3 | versicolor |
66 | 6.7 | 3.1 | 4.4 | 1.4 | versicolor |
67 | 5.6 | 3.0 | 4.5 | 1.5 | versicolor |
68 | 5.8 | 2.7 | 4.1 | 1.0 | versicolor |
69 | 6.2 | 2.2 | 4.5 | 1.5 | versicolor |
70 | 5.6 | 2.5 | 3.9 | 1.1 | versicolor |
71 | 5.9 | 3.2 | 4.8 | 1.8 | versicolor |
72 | 6.1 | 2.8 | 4.0 | 1.3 | versicolor |
73 | 6.3 | 2.5 | 4.9 | 1.5 | versicolor |
74 | 6.1 | 2.8 | 4.7 | 1.2 | versicolor |
75 | 6.4 | 2.9 | 4.3 | 1.3 | versicolor |
76 | 6.6 | 3.0 | 4.4 | 1.4 | versicolor |
77 | 6.8 | 2.8 | 4.8 | 1.4 | versicolor |
78 | 6.7 | 3.0 | 5.0 | 1.7 | versicolor |
79 | 6.0 | 2.9 | 4.5 | 1.5 | versicolor |
80 | 5.7 | 2.6 | 3.5 | 1.0 | versicolor |
81 | 5.5 | 2.4 | 3.8 | 1.1 | versicolor |
82 | 5.5 | 2.4 | 3.7 | 1.0 | versicolor |
83 | 5.8 | 2.7 | 3.9 | 1.2 | versicolor |
84 | 6.0 | 2.7 | 5.1 | 1.6 | versicolor |
85 | 5.4 | 3.0 | 4.5 | 1.5 | versicolor |
86 | 6.0 | 3.4 | 4.5 | 1.6 | versicolor |
87 | 6.7 | 3.1 | 4.7 | 1.5 | versicolor |
88 | 6.3 | 2.3 | 4.4 | 1.3 | versicolor |
89 | 5.6 | 3.0 | 4.1 | 1.3 | versicolor |
90 | 5.5 | 2.5 | 4.0 | 1.3 | versicolor |
91 | 5.5 | 2.6 | 4.4 | 1.2 | versicolor |
92 | 6.1 | 3.0 | 4.6 | 1.4 | versicolor |
93 | 5.8 | 2.6 | 4.0 | 1.2 | versicolor |
94 | 5.0 | 2.3 | 3.3 | 1.0 | versicolor |
95 | 5.6 | 2.7 | 4.2 | 1.3 | versicolor |
96 | 5.7 | 3.0 | 4.2 | 1.2 | versicolor |
97 | 5.7 | 2.9 | 4.2 | 1.3 | versicolor |
98 | 6.2 | 2.9 | 4.3 | 1.3 | versicolor |
99 | 5.1 | 2.5 | 3.0 | 1.1 | versicolor |
100 | 5.7 | 2.8 | 4.1 | 1.3 | versicolor |
101 | 6.3 | 3.3 | 6.0 | 2.5 | virginica |
102 | 5.8 | 2.7 | 5.1 | 1.9 | virginica |
103 | 7.1 | 3.0 | 5.9 | 2.1 | virginica |
104 | 6.3 | 2.9 | 5.6 | 1.8 | virginica |
105 | 6.5 | 3.0 | 5.8 | 2.2 | virginica |
106 | 7.6 | 3.0 | 6.6 | 2.1 | virginica |
107 | 4.9 | 2.5 | 4.5 | 1.7 | virginica |
108 | 7.3 | 2.9 | 6.3 | 1.8 | virginica |
109 | 6.7 | 2.5 | 5.8 | 1.8 | virginica |
110 | 7.2 | 3.6 | 6.1 | 2.5 | virginica |
111 | 6.5 | 3.2 | 5.1 | 2.0 | virginica |
112 | 6.4 | 2.7 | 5.3 | 1.9 | virginica |
113 | 6.8 | 3.0 | 5.5 | 2.1 | virginica |
114 | 5.7 | 2.5 | 5.0 | 2.0 | virginica |
115 | 5.8 | 2.8 | 5.1 | 2.4 | virginica |
116 | 6.4 | 3.2 | 5.3 | 2.3 | virginica |
117 | 6.5 | 3.0 | 5.5 | 1.8 | virginica |
118 | 7.7 | 3.8 | 6.7 | 2.2 | virginica |
119 | 7.7 | 2.6 | 6.9 | 2.3 | virginica |
120 | 6.0 | 2.2 | 5.0 | 1.5 | virginica |
121 | 6.9 | 3.2 | 5.7 | 2.3 | virginica |
122 | 5.6 | 2.8 | 4.9 | 2.0 | virginica |
123 | 7.7 | 2.8 | 6.7 | 2.0 | virginica |
124 | 6.3 | 2.7 | 4.9 | 1.8 | virginica |
125 | 6.7 | 3.3 | 5.7 | 2.1 | virginica |
126 | 7.2 | 3.2 | 6.0 | 1.8 | virginica |
127 | 6.2 | 2.8 | 4.8 | 1.8 | virginica |
128 | 6.1 | 3.0 | 4.9 | 1.8 | virginica |
129 | 6.4 | 2.8 | 5.6 | 2.1 | virginica |
130 | 7.2 | 3.0 | 5.8 | 1.6 | virginica |
131 | 7.4 | 2.8 | 6.1 | 1.9 | virginica |
132 | 7.9 | 3.8 | 6.4 | 2.0 | virginica |
133 | 6.4 | 2.8 | 5.6 | 2.2 | virginica |
134 | 6.3 | 2.8 | 5.1 | 1.5 | virginica |
135 | 6.1 | 2.6 | 5.6 | 1.4 | virginica |
136 | 7.7 | 3.0 | 6.1 | 2.3 | virginica |
137 | 6.3 | 3.4 | 5.6 | 2.4 | virginica |
138 | 6.4 | 3.1 | 5.5 | 1.8 | virginica |
139 | 6.0 | 3.0 | 4.8 | 1.8 | virginica |
140 | 6.9 | 3.1 | 5.4 | 2.1 | virginica |
141 | 6.7 | 3.1 | 5.6 | 2.4 | virginica |
142 | 6.9 | 3.1 | 5.1 | 2.3 | virginica |
143 | 5.8 | 2.7 | 5.1 | 1.9 | virginica |
144 | 6.8 | 3.2 | 5.9 | 2.3 | virginica |
145 | 6.7 | 3.3 | 5.7 | 2.5 | virginica |
146 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
147 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
148 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
149 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
150 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
## 函数 - k-means算法
webTJ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.8,5.6,2.2|6.3,2.8,5.1,1.5|6.1,2.6,5.6,1.4|7.7,3,6.1,2.3|6.3,3.4,5.6,2.4|6.4,3.1,5.5,1.8|6,3,4.8,1.8|6.9,3.1,5.4,2.1|6.7,3.1,5.6,2.4|6.9,3.1,5.1,2.3|5.8,2.7,5.1,1.9|6.8,3.2,5.9,2.3|6.7,3.3,5.7,2.5|6.7,3,5.2,2.3|6.3,2.5,5,1.9|6.5,3,5.2,2|6.2,3.4,5.4,2.3|5.9,3,5.1,1.8";
var oArrs=webTJ.getArrs(oTxt,"|",",");
oArrs=webTJ.Array.getQuantify(oArrs); //样本值数量化
webTJ.Datamining.setKmeans(oArrs,3); //将样本聚为3类
注:代码中鸢尾花数据被转换为格式化字符串,不包括序列号和属性列(最后一列)
四、案例分析
案例一:人口文化程度聚类分析
为了更深入了解我国人口的文化程度状况,现利用1990年全国人口普查数据对全国30个省、直辖市、自治区进行聚类分析。分析选用了三个指标:(1)大学以上文化程度的人口占全部人口的比例(DXBZ);(2)初中文化程度的人口占全部人口的比例(CZBZ);(3)文盲半文盲人口占全部人口的比例(WMBZ)、分别用来反映较高、中等、较低文化程度人口的状况,原始数据如下表:
地区 | 序号 | DXBZ | CZBZ | WMBZ |
---|---|---|---|---|
北京 | 1 | 9.3 | 30.55 | 8.7 |
天津 | 2 | 4.67 | 29.38 | 8.92 |
河北 | 3 | 0.96 | 24.69 | 15.21 |
山西 | 4 | 1.38 | 29.24 | 11.3 |
内蒙 | 5 | 1.48 | 25.47 | 15.39 |
辽宁 | 6 | 2.6 | 32.32 | 8.81 |
吉林 | 7 | 2.15 | 26.31 | 10.49 |
黑龙江 | 8 | 2.14 | 28.46 | 10.87 |
上海 | 9 | 6.53 | 31.59 | 11.04 |
江苏 | 10 | 1.47 | 26.43 | 17.23 |
浙江 | 11 | 1.17 | 23.74 | 17.46 |
安徽 | 12 | 0.88 | 19.97 | 24.43 |
福建 | 13 | 1.23 | 16.87 | 15.63 |
江西 | 14 | 0.99 | 18.84 | 16.22 |
山东 | 15 | 0.98 | 25.18 | 16.87 |
河南 | 16 | 0.85 | 26.55 | 16.15 |
河北 | 17 | 1.57 | 23.16 | 15.79 |
湖南 | 18 | 1.14 | 22.57 | 12.1 |
广东 | 19 | 1.34 | 23.04 | 10.45 |
广西 | 20 | 0.79 | 19.14 | 10.61 |
海南 | 21 | 1.24 | 22.53 | 13.97 |
四川 | 22 | 0.96 | 21.65 | 16.24 |
贵州 | 23 | 0.78 | 14.65 | 24.27 |
云南 | 24 | 0.81 | 13.85 | 25.44 |
西藏 | 25 | 0.57 | 3.85 | 44.43 |
陕西 | 26 | 1.67 | 24.36 | 17.62 |
甘肃 | 27 | 1.1 | 16.85 | 27.93 |
青海 | 28 | 1.49 | 17.76 | 27.7 |
宁夏 | 29 | 1.61 | 20.27 | 22.06 |
新疆 | 30 | 1.85 | 20.66 | 12.75 |
将表格中数据部分转换为格式字符串(列由“,”分割、行由“|”分割),
9.3,30.55,8.7|4.67,29.38,8.92|0.96,24.69,15.21|1.38,29.24,11.3|1.48,25.47,15.39|2.6,32.32,8.81|2.15,26.31,10.49|2.14,28.46,10.87|6.53,31.59,11.04|1.47,26.43,17.23|1.17,23.74,17.46|0.88,19.97,24.43|1.23,16.87,15.63|0.99,18.84,16.22|0.98,25.18,16.87|0.85,26.55,16.15|1.57,23.16,15.79|1.14,22.57,12.1|1.34,23.04,10.45|0.79,19.14,10.61|1.24,22.53,13.97|0.96,21.65,16.24|0.78,14.65,24.27|0.81,13.85,25.44|0.57,3.85,44.43|1.67,24.36,17.62|1.1,16.85,27.93|1.49,17.76,27.7|1.61,20.27,22.06|1.85,20.66,12.75
代码样例
var oTxt="9.3,30.55,8.7|4.67,29.38,8.92|0.96,24.69,15.21|1.38,29.24,11.3|1.48,25.47,15.39|2.6,32.32,8.81|2.15,26.31,10.49|2.14,28.46,10.87|6.53,31.59,11.04|1.47,26.43,17.23|1.17,23.74,17.46|0.88,19.97,24.43|1.23,16.87,15.63|0.99,18.84,16.22|0.98,25.18,16.87|0.85,26.55,16.15|1.57,23.16,15.79|1.14,22.57,12.1|1.34,23.04,10.45|0.79,19.14,10.61|1.24,22.53,13.97|0.96,21.65,16.24|0.78,14.65,24.27|0.81,13.85,25.44|0.57,3.85,44.43|1.67,24.36,17.62|1.1,16.85,27.93|1.49,17.76,27.7|1.61,20.27,22.06|1.85,20.66,12.75";
var oArrs=webTJ.getArrs(oTxt,"|",",");
oArrs=webTJ.Array.getQuantify(oArrs); //样本值数量化
//oArrs=webTJ.Datamining.getYZarrs(oArrs,1); //按均值、标准差将数据标准化
webTJ.Datamining.setKmeans(oArrs,2); //将样本聚为2类
注:代码webTJ.Datamining.setKmeans(oArrs,2)中可以将2改为3、4、5,观察组间误差比的变化
案例二:根据信息基础设施的发展状况,对世界20个国家和地区进行聚类分析
这里选取了发达国家、新兴工业化国家、拉美国家、亚洲发展中国家、转型国家等不同类型的20个国家作Q型聚类分析。描述信息基础设施的变量主要有六个:
I、 Call—每千人拥有电话线数,
II、 movecall—每千房居民蜂窝移动电话数,
III、fee—高峰时期每三分钟国际电话的成本,
IV、 Computer—每千人拥有的计算机数,
V、 mips—每千人中计算机功率《每秒百万指令》,
VI、 net—每千人互联网络户主数。数据摘自《世界竞争力报告—1997》。
ID | country | call | movecall | fee | computer | mips | net |
---|---|---|---|---|---|---|---|
1 | 美国 | 631.6 | 161.9 | 0.36 | 403 | 26073 | 35.34 |
2 | 日本 | 498.4 | 143.2 | 3.57 | 176 | 10223 | 6.26 |
3 | 德国 | 557.6 | 70.6 | 2.18 | 199 | 11571 | 9.48 |
4 | 瑞典 | 684.1 | 281.8 | 1.4 | 286 | 16660 | 29.39 |
5 | 瑞士 | 644 | 93.5 | 1.98 | 234 | 13621 | 22.68 |
6 | 丹麦 | 620.3 | 248.6 | 2.56 | 296 | 17210 | 21.84 |
7 | 新加坡 | 498.4 | 147.5 | 2.5 | 284 | 13578 | 13.49 |
8 | 中国台湾 | 469.4 | 56.1 | 3.68 | 119 | 6911 | 1.72 |
9 | 韩国 | 434.5 | 73 | 3.36 | 99 | 5795 | 1.68 |
10 | 巴西 | 81.9 | 16.3 | 3.02 | 19 | 876 | 0.52 |
11 | 智利 | 138.6 | 8.2 | 1.4 | 31 | 1411 | 1.28 |
12 | 墨西哥 | 92.2 | 9.8 | 2.61 | 31 | 1751 | 0.35 |
13 | 俄罗斯 | 174.9 | 5 | 5.12 | 24 | 1101 | 0.48 |
14 | 波兰 | 169 | 6.5 | 3.68 | 40 | 1796 | 1.45 |
15 | 匈牙利 | 262.2 | 49.4 | 2.66 | 68 | 3067 | 3.09 |
16 | 马来西亚 | 195.5 | 88.4 | 4.19 | 53 | 2734 | 1.25 |
17 | 泰国 | 78.6 | 27.8 | 4.95 | 22 | 1662 | 0.11 |
18 | 印度 | 13.60 | 0.30 | 6.28 | 2.00 | 101.00 | 0.01 |
19 | 法国 | 559.10 | 42.90 | 1.27 | 201.00 | 11702.00 | 4.76 |
20 | 英国 | 521.10 | 122.50 | 0.98 | 248.00 | 14461.00 | 11.91 |
代码样例
var oTxt="631.6,161.9,0.36,403,26073,35.34|498.4,143.2,3.57,176,10223,6.26|557.6,70.6,2.18,199,11571,9.48|684.1,281.8,1.4,286,16660,29.39|644,93.5,1.98,234,13621,22.68|620.3,248.6,2.56,296,17210,21.84|498.4,147.5,2.5,284,13578,13.49|469.4,56.1,3.68,119,6911,1.72|434.5,73,3.36,99,5795,1.68|81.9,16.3,3.02,19,876,0.52|138.6,8.2,1.4,31,1411,1.28|92.2,9.8,2.61,31,1751,0.35|174.9,5,5.12,24,1101,0.48|169,6.5,3.68,40,1796,1.45|262.2,49.4,2.66,68,3067,3.09|195.5,88.4,4.19,53,2734,1.25|78.6,27.8,4.95,22,1662,0.11|13.6,0.3,6.28,2,101,0.01|559.1,42.9,1.27,201,11702,4.76|521.1,122.5,0.98,248,14461,11.91";
var oArrs=webTJ.getArrs(oTxt,"|",",");
oArrs=webTJ.Array.getQuantify(oArrs); //样本值数量化
//oArrs=webTJ.Datamining.getYZarrs(oArrs,1); //按均值、标准差将数据标准化
webTJ.Datamining.setKmeans(oArrs,3); //将样本聚为3类
©哈尔滨商业大学 银河统计工作室
银河统计工作室成员由在校统计、计算机部分师生和企业数据数据分析师组成,维护和开发银河统计网和银河统计博客(技术文档)。专注于数据挖掘技术研究和运用,探索统计学、应用数学和IT技术有机结合,尝试大数据条件下新型统计学教学模式。