Weka应用总结

0. 引入

本文是我学习《数据挖掘与机器学习–WEKA应用技术与实践》的笔记。该书电子版的链接是:http://download.csdn.net/detail/fhb292262794/8759397

全书简单说明了机器学习的分类、聚类、关联的算法原理及实践,在高级应用中说明了贝叶斯网络、神经网络的原理及简单实例应用,还给出了Java Api调用WEKA接口的方法。总体而言该书比较详实,有理论有实践,内容比较全面,关于数据挖掘、机器学习的内容都有所涉及,但不够深入,原理也是点到为止了,如果读者想深入研究,每个点都需要去单独买书花时间学习研究。
本书的实例比较有帮助,联系该书作者拿到了书中的所有源码,整理后记录如下。

1.分类(手把手教你做)

1.1 使用C4.5分类器

分类

该实例使用WEKA的C4.5分类器,即决策树分类器处理天气数据集进行分类。

open Explorer–open file–select data/weather.nominal.arff–select Classify

构建决策树的C4.5算法在Weka中是作为一个分类器来实现,名称是J48。J48的全名是:weka.classifiers.trees.J48
继续上面操作:

Choose–select Trees– J48( -C 0.25 -M 2)– Use training set–play–start–Classifier Output

注意,J48分类器的默认参数很少需要修改。
结果如下:

决策树过程:
outlook = sunny
|   humidity = high: no (3.0)
|   humidity = normal: yes (2.0)
outlook = overcast: yes (4.0)
outlook = rainy
|   windy = TRUE: no (2.0)
|   windy = FALSE: yes (3.0)

Number of Leaves  :     5

Size of the tree :  8


混淆矩阵结果:
=== Confusion Matrix ===

 a b   <-- classified as
 9 0 | a = yes
 0 5 | b = no

只有主对角线上的数值很大,而非主对角线上的数值都为0时,表明预测完全正确,这不用惊讶,将训练数据用于测试的条件下经常发生的事。

测试

使用十折交叉验证,需要运行10次训练和评估算法,以构建和评估10次分类器,将数据集分成10等份,其中九份是训练数据,一份是测试数据,如此依次进行10次训练和评估。
现在加载iris数据集,使用J48分类器学习,先用Use training set 测试选项,然后使用Cross-validation 10 折测试选项,分别训练并评估J48分类器,结果如下:

Use training set    Correctly Classified Instances         147               98      %
Cross-validation 10     Correctly Classified Instances         144               96      %

1.2 使用分类器预测未知数据

还是使用J48分类器对天气数据训练,现在构建一个测试数据集,用文本编辑器输入如下内容:

@relation weather.symbolic

@attribute outlook {
   
   sunny, overcast, rainy}
@attribute temperature {
   
   hot, mild, cool}
@attribute humidity {
   
   high, normal}
@attribute windy {
   
   TRUE, FALSE}
@attribute play {
   
   yes, no}

@data
sunny,hot,high,FALSE,no
rainy,cool,high,FALSE,yes

保存为weather.nominal.test.arff 文件。
操作步骤:

Classify–Supplied test set – weather.nominal.test.arff–Close–Test option的More option–Choose–PlainText–OK–Start.

结果是:

Correctly Classified Instances           2              100      %

预测没错。

1.3 使用决策规则

操作:

select Explorer– Open file– weather.nominal.arff – Classifier –Choose –rules –JRip –Start.

生成的规则有三条:

JRIP rules:
===========

(humidity = high) and (outlook = sunny) => play=no (3.0/0.0)
(outlook = rainy) and (windy = TRUE) => play=no (2.0/0.0)
 => play=yes (9.0/0.0)

Number of Rules : 3

每条规则用“=>”分开规则前件和规则后件,小括号的两个数字,第一个数字表示规则覆盖的实例数量,第二个数字表示错分的实例数量。

注意,第三条规则前件为空,表示这条规则覆盖除去前两条规则覆盖的训练实例外的所有实例。

1.4 使用线性回归

本示例使用线性回归训练CPU数据集,并评估分类器性能。
操作:

启动探索者界面,在预处理面板中加载cpu.arff,由于类别属性是连续型数值,因此该直方图不是彩色的。选择Classifier的Choose,在functions条目下选择LinearRegression分类器,Start。

选择另一种分类器-M5P,该分类器在trees下。M5P是决策树方案和线性回归方案的结合体,前半部分使用剪枝的决策树,后半部分使用线性回归。

1.5 使用元学习器

操作:

Explorer–iris.arff– DecisionStump– select 10 fold test

得到结论分类正确率是66.66%

接下来,选择AdaboostM1分类器,这是一个使用提升算法的集成学习器,单击改分类器进行配置,为了和DecisionStump分类器比较,设置AdabootM1的基分类器为DecisionStump,设置numIterations的参数默认为10,也就是训练会迭代提升DecisionStump分类器10次,在150个iris数据中,只有7个错分的实例,分类正确率高达95.333%。

总结:DecisionStump算法本来就十分原始,并且只经过很少次数的提升迭代,性能提高很大,令人满意。

1.6 最近邻分类器

本节使用IBk分类器,这是一种k-NN分类器,即可以在交叉验证的基础上选择合适的k值,也可以加权距离权重。

Explorer–open file–glass.arff–Classify–Choose IBk(weka.classifiers.lazy.IBk) –设置KNN=1–Start.

结果:

Correctly Classified Instances         151               70.5607 %

修改KNN为5,结果是:

Correctly Classified Instances         145               67.757  %

2.聚类(手把手教你做)

2.1使用SimpleKmeans算法

k均值是一种常用的聚类分析算法,该算法接受输入值k,然后将n个数据对象分为k个簇,使得获得的簇满足如下条件:同一簇中的对象相似度较高,而不同簇中的对象相似度较小。

Explorer–open file– weather.arff–Cluster–SimpleKMeans(默认参数:2个簇及欧式距离)–Start。

结果:

kMeans
======

Number of iterations: 3
Within cluster sum of squared errors: 16.237456311387238

Initial starting points (random):

Cluster 0: rainy,75,80,FALSE,yes
Cluster 1: overcast,64,65,TRUE,yes

Missing values globally replaced with mean/mode

Final cluster centroids:
                           Cluster#
Attribute      Full Data          0          1
                  (14.0)      (9.0)      (5.0)
==============================================
outlook            sunny      sunny   overcast
temperature      73.5714    75.8889       69.4
humidity         81.6429    84.1111       77.2
windy              FALSE      FALSE       TRUE
play                 yes        yes        yes




Time taken to build model (full training data) : 0 seconds

=== Model and evaluation on training set ===

Clustered Instances

0       9 ( 64%)
1       5 ( 36%)

结果分析:聚类结果以表格形式显示–行对应属性名,列对应簇中心。

2.2使用EM算法

把算法改为EM,同样设置聚类数目为2,结果:

EM
==

Number of clusters: 2
Number of iterations performed: 7


              Cluster
Attribute           0       1
               (0.35)  (0.65)
==============================
outlook
  sunny         3.8732  3.1268
  overcast      1.7746  4.2254
  rainy         2.1889  4.8111
  [total]       7.8368 12.1632
temperature
  mean         76.9173 71.8054
  std. dev.     5.8302  5.8566

humidity
  mean         90.1132 77.1719
  std. dev.     3.8066  9.1962

windy
  TRUE            3.14    4.86
  FALSE         3.6967  6.3033
  [total]       6.8368 11.1632
play
  yes           2.1227  8.8773
  no            4.7141  2.2859
  [total]       6.8368 11.1632


Time taken to build model (full training data) : 0.01 seconds

=== Model and evaluation on training set ===

Clustered Instances

0       4 ( 29%)
1      10 ( 71%)

结果不同于SimpleKMeans算法的输出,EM结果在表头每个簇的下方并没有显示实例数量,只是在表头括号内显示其先验概率。
在输出的最后,显示了模型的对数似然值,这里是相对于训练数据,输出还显示了分配给每个簇的实例数量,这是将经过学习后的模型作为分类器应用到数据后所得的结果。

2.3使用DBSCAN和OPTICS算法

DBSCAN

DBSCAN使用欧式距离度量,以确定哪些实例属于同一个簇。但是,不同于K-均值算法,DBSCAN可以自动确定簇的数量,发现任意形状的簇并引入离群值的概念。根据设定的点间距离值和簇大小的最小值(Weka界面为minPoints参数),有可能存在某些不属于任何簇的实例,这些实例称为离群值。

OPTICS

OPTICS算法是DBSCAN算法在层次聚类方面的扩展。OPTICS规定了实例的顺序,这些实例进行二维可视化,揭示了簇的层次结构。

Explorer–open file–iris.arff–Cluster–Choose–DBSCAN

3.关联(手把手教你做)

3.1Apriori关联规则挖掘

Apriori算法需要完全的标称型数据,如果有数值型属性,必须先进行离散化。
操作:

加载weather.norninal.arff–Associate–Apriori–Start.

结果:

Best rules found:

 1. outlook=overcast 4 ==> play=yes 4    <conf:(1)> lift:(1.56) lev:(0.1) [1] conv:(1.43)
 2. temperature=cool 4 ==> humidity=normal 4    <conf:(1)> lift:(2) lev:(0.14) [2] conv:(2)
 3. humidity=normal windy=FALSE 4 ==> play=yes 4    <conf:(1)> lift:(1.56) lev:(0.1) [1] conv:(1.43)
 4. outlook=sunny play=no 3 ==> humidity=high 3    <conf:(1)> lift:(2) lev:(0.11) [1] conv:(1.5)
 5. outlook=sunny humidity=high 3 ==> play=no 3    <conf:(1)> lift:(2.8) lev:(0.14) [1] conv:(1.93)
 6. outlook=rainy play=yes 3 ==> windy=FALSE 3    <conf:(1)> lift:(1.75) lev:(0.09) [1] conv:(1.29)
 7. outlook=rainy windy=FALSE 3 ==> play=yes 3    <conf:(1)> lift:(1.56) lev:(0.08) [1] conv:(1.07)
 8. temperature=cool play=yes 3 ==> humidity=normal 3    <conf:(1)> lift:(2) lev:(0.11) [1] conv:(1.5)
 9. outlook=sunny temperature=hot 2 ==> humidity=high 2    <conf:(1)> lift:(2) lev:(0.07) [1] conv:(1)
10. temperature=hot play=no 2 ==> outlook=sunny 2    <conf:(1)> lift:(2.8) lev:(0.09) [1] conv:(1.29)

规则采用“前件 num.1 ==>结论 num.2”的形式表示,前件后面的数字表示有多少个实例满足前件,结论后的数字表示有多少个实例满足整个规则,这就是规则的”支持度“。

规则排序有替代度量,除置信度(confidence)外,Apriori算法还支持lift(提升度)、leverage(杠杠率)、以及conviction(确信度),这些可以通过使用metricType进行选择。

3.2市场购物篮分析

Weka自带一个超市购物篮分析数据集,文件名是supermarket.arff,其中数据是从新西兰的一个真实超市收集得到。

Explorer–open file–supermarket.arff–Associate–Apriori–Start.

结果:


Best rules found:

 1. biscuits=t frozen foods=t fruit=t total=high 788 ==> bread and cake=t 723    <conf:(0.92)> lift:(1.27) lev:(0.03) [155] conv:(3.35)
 2. baking needs=t biscuits=t fruit=t total=high 760 ==> bread and cake=t 696    <conf:(0.92)> lift:(1.27) lev:(0.03) [149] conv:(3.28)
 3. baking needs=t frozen foods=t fruit=t total=high 770 ==> bread and cake=t 705    <conf:(0.92)> lift:(1.27) lev:(0.03) [150] conv:(3.27)
 4. biscuits=t fruit=t vegetables=t total=high 815 ==> bread and cake=t 746    <conf:(0.92)> lift:(1.27) lev:(0.03) [159] conv:(3.26)
 5. party snack foods=t fruit=t total=high 854 ==> bread and cake=t 779    <conf:(0.91)> lift:(1.27) lev:(0.04) [164] conv:(3.15)
 6. biscuits=t frozen foods=t vegetables=t total=high 797 ==> bread and cake=t 725    <conf:(0.91)> lift:(1.26) lev:(0.03) [151] conv:(3.06)
 7. baking needs=t biscuits=t vegetables=t total=high 772 ==> bread and cake=t 701    <conf:(0.91)> lift:(1.26) lev:(0.03) [145] conv:(3.01)
 8. biscuits=t fruit=t total=high 954 ==> bread and cake=t 866    <conf:(0.91)> lift:(1.26) lev:(0.04) [179] conv:(3)
 9. frozen foods=t fruit=t vegetables=t total=high 834 ==> bread and cake=t 757    <conf:(0.91)> lift:(1.26) lev:(0.03) [156] conv:(3)
10. frozen foods=t fruit=t total=high 969 ==> bread and cake=t 877    <conf:(0.91)> lift:(1.26) lev:(0.04) [179] conv:(2.92)

分析一下,看看:

第一条规则:饼干+冷冻食品+水果+高总额 ==> 面包和蛋糕
第二条规则:烘烤所需+饼干+水果+高总额 ==> 面包和蛋糕
第三条规则:烘烤所需+冷冻食品+水果+高总额 ==> 面包和蛋糕
第四条规则:饼干+水果+蔬菜+高总额 ==> 面包和蛋糕
第五条规则:聚会零食+水果+高总额 ==> 面包和蛋糕
第六条规则:饼干+冷冻食品+蔬菜+高总额 ==> 面包和蛋糕
第七条规则:烘烤所需+饼干+蔬菜+高总额 ==> 面包和蛋糕
第八条规则:饼干+水果+高总额 ==> 面包和蛋糕
第九条规则:冷冻食品+水果+蔬菜+高总额 ==> 面包和蛋糕
第十条规则:冷冻食品+水果+高总额 ==> 面包和蛋糕

十条关联规则中,多项商品多次出现,而且总金额都很高。这给出了一些显而易见的信息,第一,购买饼干、冷冻食品等速食的顾客,会顺便采购些水果、蔬菜,以补充身体所需的维生素;第二,购买饼干、冷冻食品以及水果、蔬菜的顾客,会顺便购买面包和蛋糕;第三,购买上述食品的顾客,一次的采购量会很大,总金额较高;第四,总金额较高的交易,一般都会购买面包和蛋糕,等等。
对于超市经理来说,这些信息非常重要,可以根据挖掘到的知识重新安排货架,重新布局超市,提供快速付款通道以及安排送货等附加服务,以期提升市场竞争力。
请大家尝试使用Apriori算法的不同参数,看看挖掘效果,能否得到一些意外而又在情理之中的结论。

4.选择属性(手把手教你做)

4.1自动属性选择-过滤器方法

在数据集中手工选择游泳的属性非常繁琐且乏味,自动属性选择方法通常更快更好。
属性选择方法可以分为过滤器和包装方法。前者应用低计算开销的启发式方法来衡量属性子集的质量,后者通过构建和评估实际的分类模型,衡量属性子集的质量,计算开销较大,但往往性能超好。

下面使用InfoGainAttributeEval评估器和Ranker搜索方法对属性排名。

Explorer–labor.arff–Select attributes–Attribute Evaluator子面板–Choose–InfoGainAttributeEval–Ranker–Start.

结果:

=== Attribute Selection on all input data ===

Search Method:
    Attribute ranking.

Attribute Evaluator (supervised, Class (nominal): 17 class):
    Information Gain Ranking Filter

Ranked attributes:
 0.2948   2 wage-increase-first-year
 0.1893   3 wage-increase-second-year
 0.1624  11 statutory-holidays
 0.1341  14 contribution-to-dental-plan
 0.1164  16 contribution-to-health-plan
 0.1091  12 vacation
 0.0855  13 longterm-disability-assistance
 0.0717   9 shift-differential
 0.0548   7 pension
 0.0484   5 cost-of-living-adjustment
 0.0333  15 bereavement-assistance
 0.0307   4 wage-increase-third-year
 0.024   10 education-allowance
 0.0195   8 standby-pay
 0        6 working-hours
 0        1 duration

Selected attributes: 2,3,11,14,16,12,13,9,7,5,15,4,10,8,6,1 : 16

从运行结果可以看到,属性选择输出中已经按照各个属性在预测目标方面的重要性进行排序,用户可以按照排名后的重要程度选取一定的属性子集。

与上述评估器的信息增益方法不同,CfsSubsetEval目标是确定一个属性子集,它与目标属性相关性强,同时相互之间又没有强相关性。通常默认下,它用BestFirst搜索方法。

4.2自动属性选择-包装方法

如果要使用包装器的方法,而不用如图CfsSubsetEval那样的过滤器方法,那么,首先要选择WrapperSubsetEval评估器,然后选择学习算法,设置评估属性子集所使用的交叉验证的折数等选项。

Explorer–labor.arff–Select attributes–Attribute Evaluator子面板–Choose–WrapperSubsetEval–基学习器classifier:J48-BestFirst–Start.

结果:

Selected attributes: 1,2,4,6,11,12 : 6
                     duration
                     wage-increase-first-year
                     wage-increase-third-year
                     working-hours
                     statutory-holidays
                     vacation

相比较CfsSubsetEval和WrapperSubsetEval两种算法选出来的结果,分析:
第一,经过属性选择后,分类准确度得到提高。
第二,对于本例,相对CfsSubsetEval和WrapperSubsetEval,WrapperSubsetEval选出的属性,属性子集更小但效果更好。

猜你喜欢

转载自blog.csdn.net/shibing624/article/details/64918634