用 K 均值优化实际示例
下面的示例演示了一个实际的用例,其中 k 均值集群和形心函数应用于数据集。K 均值函数将数据点分隔成共享相似性的集群。随着 K 均值算法在可配置的迭代次数上的应用,集群变得更加紧凑和差异化。
K 均值在各种各样的用例中跨多个领域使用;集群用例的一些示例包括客户细分、欺诈检测、预测帐户损耗、针对客户的激励、网络犯罪识别和交付路线优化。K 均值聚类算法正越来越多地被用于企业试图推断模式和优化服务产品的场景。
Qlik Sense K 均值与形心函数
Qlik Sense 提供两个 K 均值函数,根据相似性将数据点分组到集群中。请参阅 KMeans2D - 图表函数 和 KMeansND - 图表函数。KMeans2D 函数接受二维数据,通过散点图很好地显示结果。KMeansND 函数接受两个以上的维度。由于在标准图表上概念化二维结果很容易,下面的演示在使用两个维度的散点图上应用 K 均值。K 均值聚类可以通过表达式着色来可视化;或按本例中所述的维度。
Qlik Sense 形心函数确定集群中所有数据点的算术平均位置,并标识一个中心点或该集群的形心。对于每个图表行(或记录),纸芯函数显示分配了该数据点的集群的坐标。请参阅 KMeansCentroid2D - 图表函数 和 KMeansCentroidND - 图表函数。
用例和示例概述
下面的示例将通过模拟的真实世界场景逐步展开。美国纽约州的一家纺织公司必须通过降低交货成本来减少开支。一种方法是将仓库搬迁到离分销商更近的地方。该公司在纽约州拥有 118 个分销商。下面的演示模拟了运营经理如何使用 K 均值函数将分销商划分为五个集群地理位置,然后使用形心函数确定这些集群中心的五个最佳仓库位置。目标是发现可用于确定五个中心仓库位置的地图坐标。
数据集
该数据集基于纽约州随机生成的名称和地址,并具有准确的经纬度坐标。数据集包含以下十列: id、first_name、last_name、telephone、address、city、state、zip、latitude、longitude。数据集在下面作为一个文件提供,您可以在本地下载,然后上传到 Qlik Sense 或对数据加载编辑器内联。创建的应用程序可以命名为 Distributors KMeans and Centroid,应用程序中的第一个工作表命名为 Distribution cluster analysis。
选择以下链接来下载样本数据文件:DistributorData.csv
Distributor 数据集:Qlik Sense 中数据加载编辑器的内联加载
标题:DistributorData
记录的总数:118
应用 KMeans2D 函数
在本例中,使用 DistributorData 数据集演示散点图的配置,应用 KMeans2D 函数,并按维度为图表着色。
注意 Qlik Sense K 均值函数支持使用名为深度差 (DeD) 的方法支持自动聚合。如果用户为集群数设置 0,则会为该数据集确定最优集群数。但是,在本例中,为 num_clusters 参数创建了一个变量(有关语法,请参阅 KMeans2D - 图表函数 )。因此,所需的集群数 (k=5) 由一个变量指定。
- 散点图被拖到工作表上并命名为 Distributors (by dimension)。
- 创建了变量以指定集群数。变量被命名为 vDistClusters。对于变量 Definition,输入 5。
- 图表的数据配置:
- 在维度下,选择气泡的 id 字段。输入标签的 Cluster id。
- 在度量下,Avg([latitude]) 是 X 轴的表达式。
- 在度量下,Avg([longitude]) 是 Y 轴的表达式。
- 外观配置:
- 在颜色和图例下,为颜色选择了自定义。
- 为图表着色选择了按维度。
- 输入了以下表达式: =pick(aggr(KMeans2D(vDistClusters,only(latitude),only(longitude)),id)+1, 'Cluster 1', 'Cluster 2', 'Cluster 3', 'Cluster 4', 'Cluster 5')
- 选择了固定颜色的复选框。
添加表格:分销商
有一个方便的表格可以快速访问相关数据,这会很有帮助。散点图通过表格显示 ID,其中添加了相应分商名称供参考。
- 一个名为 Distributors 的表被拖到工作表上,并添加了以下列(维度):id、first\u name 和 last\u name。
添加条形图:# observations per cluster
对于仓库配送场景,了解每个仓库将为多少分销商提供服务是很有帮助的。因此,将创建一个条形图,用于测量分配给每个集群的分销商数量。
- 条形图被拖动到工作表上。图表被命名为:# observations per cluster。
- 条形图的数据配置:
- 添加一个标注为群集的维度(可以在应用表达式后添加标签)。输入了以下表达式: =pick(aggr(KMeans2D(vDistClusters,only(latitude),only(longitude)),id)+1, 'Cluster 1', 'Cluster 2', 'Cluster 3', 'Cluster 4', 'Cluster 5')
- 添加了标记为 # of observations 的度量。输入了以下表达式: =count(aggr(KMeans2D(vDistClusters,only(latitude),only(longitude)),id))
- 外观配置:
- 在颜色和图例下,为颜色选择了自定义。
-
为图表着色选择了按维度。
- 输入了以下表达式: =pick(aggr(KMeans2D(vDistClusters,only(latitude),only(longitude)),id)+1, 'Cluster 1', 'Cluster 2', 'Cluster 3', 'Cluster 4', 'Cluster 5')
- 选择了固定颜色的复选框。
- 显示图例已关闭。
- 在演示下方,值标签切换为自动。
- 在 X 轴下方:选择了集群、只有标签。
应用 Centroid2D 函数
为 Centroid2D 函数添加了第二个表,该表将标识潜在仓库位置的坐标。此表显示了五个已标识的分销商组的中心位置(形心值)。
- 将一个表拖到工作表上并命名为群集形心,并添加以下列:
- 添加了标记为 Clusters 的维度。输入了以下表达式:=pick(aggr(KMeans2D(vDistClusters,only(latitude),only(longitude)),id)+1,'Warehouse 1','Warehouse 2','Warehouse 3','Warehouse 4','Warehouse 5')
- 添加了标记为 latitude (D1) 的度量。输入了以下表达式:=only(aggr(KMeansCentroid2D(vDistClusters,0,only(latitude),only(longitude)),id))
注意参数 coordinate_no 对应于第一个 dimension(0)。在该情况下,将根据 x 轴绘制维度 latitude。如果我们使用的是 CentroidND 函数,并且最多有六个维度,那么这些参数项可以是六个值中的任意一个:0、1、2、3、4 或 5。 - 添加了标记为 longitude (D2) 的度量。输入了以下表达式:=only(aggr(KMeansCentroid2D(vDistClusters,1,only(latitude),only(longitude)),id))
此表达式中的参数 coordinate_no 对应于第二个维度(1)。根据 y 轴绘制了维度 longitude。
形心映射
下一步是映射形心。它由应用程序开发人员决定,如果他们喜欢把可视化放置在单独的工作表上,就可以如此。
- 名为 Centroid mapping 的映射被拖动到工作表上。
- 在层部分中。选择了添加层,然后选择了点层。
-
选择了字段 id 并且添加了 Dist ids 标签。
-
在位置部分中,选中纬度和经度字段的复选框。
-
对于纬度,选择了纬度字段。
-
对于经度,选择了经度字段。
-
在大小和形状区域中,对于形状选择气泡,并在滑块上将大小减小到“首选项”。
-
在颜色部分中,选择单色,对于颜色选择蓝色,对于轮廓选择灰色(这些选择也是首选项)。
- 在层部分中,通过选择添加层,然后选择点层,添加第二个点层。
-
输入了以下表达式:=aggr(KMeans2D(vDistClusters,only(latitude),only(longitude)),id)
-
添加了标签群集。
-
在位置部分中,选中纬度和经度字段的复选框。
-
对于在本例中沿 x 轴绘制的纬度,添加以下表达式:=aggr(KMeansCentroid2D(vDistClusters,0,only(latitude),only(longitude)),id)
-
对于在本例中沿 y 轴绘制的经度,添加以下表达式:=aggr(KMeansCentroid2D(vDistClusters,1,only(latitude),only(longitude)),id)
-
在大小和形状区域中,对于形状选择三角形,并在滑块上将大小减小到“首选项”。
-
在颜色和图例下,为颜色选择了自定义。
-
为图表着色选择了按维度。输入了以下表达式: =pick(aggr(KMeans2D(vDistClusters,only(latitude),only(longitude)),id)+1,'Cluster 1','Cluster 2','Cluster 3','Cluster 4','Cluster 5')
-
添加了标记为 Clusters 的维度。
- 在地图设置中,对于投影选择了自适应。对于度量单位选择了公制。
结论
在这个真实场景中使用 K 均值函数,根据相似性将分销商分成相似的组或群集;在这种情况下,彼此接近。将形心函数应用于这些群集,识别出 5 个映射坐标。这些坐标提供了建造或定位仓库的初始中心位置。形心函数应用于地图图表,这样应用程序用户可以直观地看到形心相对于周围群集数据点的位置。由此产生的坐标表示潜在的仓库位置,这可以最大限度地降低纽约州分销商的交货成本。
Distributor 数据集:Qlik Sense 中数据加载编辑器的内联加载
DistributorData:
Load * Inline [
id,first_name,last_name,telephone,address,city,state,zip,latitude,longitude
1,Kaiya,Snow,(716) 201-1212,6231 Tonawanda Creek Rd #APT 308,Lockport,NY,14094,43.08926,-78.69313
2,Dean,Roy,(716) 201-1588,6884 E High St,Lockport,NY,14094,43.16245,-78.65036
3,Eden,Paul,(716) 202-4596,4647 Southwestern Blvd #APT 350,Hamburg,NY,14075,42.76003,-78.83194
4,Bryanna,Higgins,(716) 203-7041,418 Park Ave,Dunkirk,NY,14048,42.48279,-79.33088
5,Elisabeth,Lee,(716) 203-7043,36 E Courtney St,Dunkirk,NY,14048,42.48299,-79.31928
6,Skylar,Robinson,(716) 203-7166,26 Greco Ln,Dunkirk,NY,14048,42.4612095,-79.3317925
7,Cody,Bailey,(716) 203-7201,114 Lincoln Ave,Dunkirk,NY,14048,42.4801269,-79.322232
8,Dario,Sims,(408) 927-1606,N Castle Dr,Armonk,NY,10504,41.11979,-73.714864
9,Deacon,Hood,(410) 244-6221,4856 44th St,Woodside,NY,11377,40.748372,-73.905445
10,Zackery,Levy,(410) 363-8874,61 Executive Blvd,Farmingdale,NY,11735,40.7197457,-73.430239
11,Rey,Hawkins,(412) 344-8687,4585 Shimerville Rd,Clarence,NY,14031,42.972075,-78.6592452
12,Phillip,Howard,(413) 269-4049,464 Main St #101,Port Washington,NY,11050,40.8273756,-73.7009971
13,Shirley,Tyler,(434) 985-8943,114 Glann Rd,Apalachin,NY,13732,42.0482515,-76.1229725
14,Aniyah,Jarvis,(440) 244-1808,87 N Middletown Rd,Pearl River,NY,10965,41.0629,-74.0159
15,Alayna,Woodard,(478) 335-3704,70 W Red Oak Ln,West Harrison,NY,10604,41.0162722,-73.7234926
16,Jermaine,Lambert,(508) 561-9836,24 Kellogg Rd,New Hartford,NY,13413,43.0555739,-75.2793197
17,Harper,Gibbs,(239) 466-0238,Po Box 33,Cottekill,NY,12419,41.853392,-74.106082
18,Osvaldo,Graham,(252) 246-0816,6878 Sand Hill Rd,East Syracuse,NY,13057,43.073215,-76.081448
19,Roberto,Wade,(270) 469-1211,3936 Holley Rd,Moravia,NY,13118,42.713044,-76.481227
20,Kate,Mcguire,(270) 788-3080,6451 State 64 Rte #3,Naples,NY,14512,42.707366,-77.380489
21,Dale,Andersen,(281) 480-5690,205 W Service Rd,Champlain,NY,12919,44.9645392,-73.4470831
22,Lorelai,Burch,(302) 644-2133,1 Brewster St,Glen Cove,NY,11542,40.865177,-73.633019
23,Amiyah,Flowers,(303) 223-0055,46600 Us Interstate 81 Rte,Alexandria Bay,NY,13607,44.309626,-75.988365
24,Mckinley,Clements,(303) 918-3230,200 Summit Lake Dr,Valhalla,NY,10595,41.101145,-73.778298
25,Marc,Gibson,(607) 203-1233,25 Robinson St,Binghamton,NY,13901,42.107416,-75.901614
26,Kali,Norman,(607) 203-1400,1 Ely Park Blvd #APT 15,Binghamton,NY,13905,42.125866,-75.925026
27,Laci,Cain,(607) 203-1437,16 Zimmer Road,Kirkwood,NY,13795,42.066516,-75.792627
28,Mohammad,Perez,(607) 203-1652,71 Endicott Ave #APT 12,Johnson City,NY,13790,42.111894,-75.952187
29,Izabelle,Pham,(607) 204-0392,434 State 369 Rte,Port Crane,NY,13833,42.185838,-75.823074
30,Kiley,Mays,(607) 204-0870,244 Ballyhack Rd #14,Port Crane,NY,13833,42.175612,-75.814917
31,Peter,Trevino,(607) 205-1374,125 Melbourne St.,Vestal,NY,13850,42.080254,-76.051124
32,Ani,Francis,(607) 208-4067,48 Caswell St,Afton,NY,13730,42.232065,-75.525674
33,Jared,Sheppard,(716) 386-3002,4709 430th Rte,Bemus Point,NY,14712,42.162175,-79.39176
34,Dulce,Atkinson,(914) 576-2266,501 Pelham Rd,New Rochelle,NY,10805,40.895449,-73.782602
35,Jayla,Beasley,(716) 526-1054,5010 474th Rte,Ashville,NY,14710,42.096859,-79.375561
36,Dane,Donovan,(718) 545-3732,5014 31st Ave,Woodside,NY,11377,40.756967,-73.909506
37,Brendon,Clay,(585) 322-7780,133 Cummings Ave,Gainesville,NY,14066,42.664309,-78.085651
38,Asia,Nunez,(718) 426-1472,2407 Gilmore ,East Elmhurst,NY,11369,40.766662,-73.869185
39,Dawson,Odonnell,(718) 342-2179,5019 H Ave,Brooklyn,NY,11234,40.633245,-73.927591
40,Kyle,Collins,(315) 733-7078,502 Rockhaven Rd,Utica,NY,13502,43.129184,-75.226726
41,Eliza,Hardin,(315) 331-8072,502 Sladen Place,West Point,NY,10996,41.3993,-73.973003
42,Kasen,Klein,(518) 298-4581,2407 Lake Shore Rd,Chazy,NY,12921,44.925561,-73.387373
43,Reuben,Bradford,(518) 298-4581,33 Lake Flats Dr,Champlain,NY,12919,44.928092,-73.387884
44,Henry,Grimes,(518) 523-3990,2407 Main St,Lake Placid,NY,12946,44.291487,-73.98474
45,Kyan,Livingston,(518) 585-7364,241 Alexandria Ave,Ticonderoga,NY,12883,43.836553,-73.43155
46,Kaitlyn,Short,(516) 678-3189,241 Chance Dr,Oceanside,NY,11572,40.638534,-73.63079
47,Damaris,Jacobs,(914) 664-5331,241 Claremont Ave,Mount Vernon,NY,10552,40.919852,-73.827848
48,Alivia,Schroeder,(315) 469-4473,241 Lafayette Rd,Syracuse,NY,13205,42.996446,-76.12957
49,Bridget,Strong,(315) 298-4355,241 Maltby Rd,Pulaski,NY,13142,43.584966,-76.136317
50,Francis,Lee,(585) 201-7021,166 Ross St,Batavia,NY,14020,43.0031502,-78.17487
51,Makaila,Phelps,(585) 201-7422,58 S Main St,Batavia,NY,14020,42.99941,-78.1939285
52,Jazlynn,Stephens,(585) 203-1087,1 Sinclair Dr,Pittsford,NY,14534,43.084157,-77.545452
53,Ryann,Randolph,(585) 203-1519,331 Eaglehead Rd,East Rochester,NY,14445,43.10785,-77.475552
54,Rosa,Baker,(585) 204-4011,42 Ossian St,Dansville,NY,14437,42.560761,-77.70088
55,Marcel,Barry,(585) 204-4013,42 Jefferson St,Dansville,NY,14437,42.557735,-77.702983
56,Dennis,Schmitt,(585) 204-4061,750 Dansville Mount Morris Rd,Dansville,NY,14437,42.584458,-77.741648
57,Cassandra,Kim,(585) 204-4138,3 Perine Ave APT1,Dansville,NY,14437,42.562865,-77.69661
58,Kolton,Jacobson,(585) 206-5047,4925 Upper Holly Rd,Holley,NY,14470,43.175957,-78.074465
59,Nathanael,Donovan,(718) 393-3501,9604 57th Ave,Corona,NY,11373,40.736077,-73.864858
60,Robert,Frazier,(718) 271-3067,300 56th Ave,Corona,NY,11373,40.735304,-73.873997
61,Jessie,Mora,(315) 405-8991,9607 Forsyth Loop,Watertown,NY,13603,44.036466,-75.833437
62,Martha,Rollins,(347) 242-2642,22 Main St,Corona,NY,11373,40.757727,-73.829331
63,Emely,Townsend,(718) 699-0751,60 Sanford Ave,Corona,NY,11373,40.755466,-73.831029
64,Kylie,Cooley,(347) 561-7149,9608 95th Ave,Ozone Park,NY,11416,40.687564,-73.845715
65,Wendy,Cameron,(585) 571-4185,9608 Union St,Scottsville,NY,14546,43.013327,-77.7907839
66,Kayley,Peterson,(718) 654-5027,961 E 230th St,Bronx,NY,10466,40.889275,-73.850555
67,Camden,Ochoa,(718) 760-8699,59 Vark St,Yonkers,NY,10701,40.929322,-73.89957
68,Priscilla,Castillo,(910) 326-7233,9359 Elm St,Chadwicks,NY,13319,43.024902,-75.26886
69,Dana,Schultz,(913) 322-4580,99 Washington Ave,Hastings on Hudson,NY,10706,40.99265,-73.879748
70,Blaze,Medina,(914) 207-0015,60 Elliott Ave,Yonkers,NY,10705,40.921498,-73.896682
71,Finnegan,Tucker,(914) 207-0015,90 Hillside Drive,Yonkers,NY,10705,40.922514,-73.892911
72,Pranav,Palmer,(914) 214-8376,5 Bruce Ave,Harrison,NY,10528,40.970916,-73.711493
73,Kolten,Wong,(914) 218-8268,70 Barker St,Mount Kisco,NY,10549,41.211993,-73.723202
74,Jasiah,Vazquez,(914) 231-5199,30 Broadway,Dobbs Ferry,NY,10522,41.004629,-73.879825
75,Lamar,Pierce,(914) 232-0380,68 Ridge Rd,Katonah,NY,10536,41.256662,-73.707964
76,Carla,Coffey,(914) 232-0469,197 Beaver Dam Rd,Katonah,NY,10536,41.247934,-73.664363
77,Brooklynn,Harmon,(716) 595-3227,8084 Glasgow Rd,Cassadega,NY,14718,42.353861,-79.329558
78,Raquel,Hodges,(585) 398-8125,809 County Road ,Victor,NY,14564,43.011745,-77.398806
79,Jerimiah,Gardner,(585) 787-9127,809 Houston Rd,Webster,NY,14580,43.224204,-77.491353
80,Clarence,Hammond,(720) 746-1619,809 Pierpont Ave,Piermont,NY,10968,41.0491181,-73.918622
81,Rhys,Gill,(518) 427-7887,81 Columbia St,Albany,NY,12210,42.652824,-73.752096
82,Edith,Parrish,(845) 452-7621,81 Glenwood Ave,Poughkeepsie,NY,12603,41.691058,-73.910829
83,Kobe,Mcintosh,(845) 371-1101,81 Heitman Dr,Spring Valley,NY,10977,41.103227,-74.054396
84,Ayden,Waters,(516) 796-2722,81 Kingfisher Rd,Levittown,NY,11756,40.738939,-73.52826
85,Francis,Rogers,(631) 427-7728,81 Knollwood Ave,Huntington,NY,11743,40.864905,-73.426107
86,Jaden,Landry,(716) 496-4038,12839 39th Rte,Chaffee,NY,14030,43.527396,-73.462786
87,Giancarlo,Campos,(518) 885-5717,1284 Saratoga Rd,Ballston Spa,NY,12020,42.968594,-73.862847
88,Eduardo,Contreras,(716) 285-8987,1285 Saunders Sett Rd,Niagara Falls,NY,14305,43.122963,-79.029274
89,Gabriela,Davidson,(716) 267-3195,1286 Mee Rd,Falconer,NY,14733,42.147339,-79.137976
90,Evangeline,Case,(518) 272-9435,1287 2nd Ave,Watervliet,NY,12189,42.723132,-73.703818
91,Tyrone,Ellison,(518) 843-4691,1287 Midline Rd,Amsterdam,NY,12010,42.9730876,-74.1700608
92,Bryce,Bass,(518) 943-9549,1288 Leeds Athens Rd,Athens,NY,12015,42.259381,-73.876897
93,Londyn,Butler,(518) 922-7095,129 Argersinger Rd,Fultonville,NY,12072,42.910969,-74.441917
94,Graham,Becker,(607) 655-1318,129 Baker Rd,Windsor,NY,13865,42.107271,-75.66408
95,Rolando,Fitzgerald,(315) 465-4166,17164 County 90 Rte,Mannsville,NY,13661,43.713443,-76.06232
96,Grant,Hoover,(518) 692-8363,1718 County 113 Rte,Schaghticote,NY,12154,42.900648,-73.585036
97,Mark,Goodwin,(631) 584-6761,172 Cambon Ave,Saint James,NY,11780,40.871152,-73.146032
98,Deacon,Cantu,(845) 221-7940,172 Carpenter Rd,Hopewell Junction,NY,12533,41.57388,-73.77609
99,Tristian,Walsh,(516) 997-4750,172 E Cabot Ln,Westbury,NY,11590,40.7480397,-73.54819
100,Abram,Alexander,(631) 588-3817,172 Lorenzo Cir,Ronkonkoma,NY,11779,40.837123,-73.09367
101,Lesly,Bush,(516) 489-3791,172 Nassau Blvd,Garden City,NY,11530,40.71147,-73.660753
102,Pamela,Espinoza,(716) 201-1520,172 Niagara St ,Lockport,NY,14094,43.169871,-78.70093
103,Bryanna,Newton,(914) 328-4332,172 Warren Ave,White Plains,NY,10603,41.047207,-73.79572
104,Marcelo,Schmitt,(315) 393-4432,319 Mansion Ave,Ogdensburg,NY,13669,44.690246,-75.49992
105,Layton,Valenzuela,(631) 676-2113,319 Singingwood Dr,Holbrook,NY,11741,40.801391,-73.058993
106,Roderick,Rocha,(518) 671-6037,319 Warren St,Hudson,NY,12534,42.252527,-73.790629
107,Camryn,Terrell,(315) 635-1680,3192 Olive Dr,Baldinsville,NY,13027,43.136843,-76.260303
108,Summer,Callahan,(585) 394-4195,3192 Smith Road,Canandaigua,NY,14424,42.875457,-77.228039
109,Pierre,Novak,(716) 665-2524,3194 Falconer Kimball Stand Rd,Falconer,NY,14733,42.138439,-79.211091
110,Kennedi,Fry,(315) 543-2301,32 College Rd,Selden,NY,11784,40.861624,-73.04757
111,Wyatt,Pruitt,(716) 681-4042,277 Ransom Rd,Lancaster ,NY,14086,42.87702,-78.591302
112,Lilly,Jensen,(631) 841-0859,2772 Schliegel Blvd,Amityville,NY,11701,40.708021,-73.413015
113,Tristin,Hardin,(631) 920-0927,278 Fulton Street,West Babylon,NY,11704,40.733578,-73.357321
114,Tanya,Stafford,(716) 484-0771,278 Sampson St,Jamestown,NY,14701,42.0797,-79.247805
115,Paris,Cordova,(607) 589-4857,278 Washburn Rd,Spencer,NY,14883,42.225046,-76.510257
116,Alfonso,Morse,(718) 359-5582,200 Colden St,Flushing,NY,11355,40.750403,-73.822752
117,Maurice,Hooper,(315) 595-6694,4435 Italy Hill Rd,Branchport,NY,14418,42.597957,-77.199267
118,Iris,Wolf,(607) 539-7288,444 Harford Rd,Brooktondale,NY,14817,42.392164,-76.30756
];