利用mahout自带的fpgrowth算法挖掘频繁模式

建立测试文件,将测试文件上传至hdfs上。这里我的测试文件是自己随便写的几行数字

1,5,2,3
5,7,3,4
5,2,3
1,5,2,7,3,4
1,2,4
5,2,4
1,2,3
1,5,2,6,3
1,5,6,3

hadoop fs -put fp.txt /

hadoop jar /opt/mahout-distribution-0.9/mahout-examples-0.9-job.jar org.apache.mahout.fpm.pfpgrowth.FPGrowthDriver -i /fp.txt -o /out -s 1 -method mapreduce

运行后查看结果是乱码。

原因:mahout运行之后得到的结果是序列化的,必须将其转化为文本文件下载到本地才可进行查看

mahout seqdumper -i /out/frequentpatterns/part-r-00000 -o /home/mahout_test/fpresult1.txt

查看后发现结果仍为乱码

猜想:mahout处理的文件必须都为序列化文件,原因可能是我的输入文件是文本格式不是序列化。

用命令mahout seqdirectory -i /test/ -o /test1/ -c UTF-8将其转化为序列化文件,但是报错:

Error: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.apache.hadoop.mapreduce.lib.input.CombineFileRecordReader.initNextRecordReader(CombineFileRecordReader.java:164)
    at org.apache.hadoop.mapreduce.lib.input.CombineFileRecordReader.<init>(CombineFileRecordReader.java:126)
    at org.apache.mahout.text.MultipleTextFileInputFormat.createRecordReader(MultipleTextFileInputFormat.java:43)
    at org.apache.hadoop.mapred.MapTask$NewTrackingRecordReader.<init>(MapTask.java:492)
    at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:735)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:340)
    at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:168)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:415)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1548)
    at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:163)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.apache.hadoop.mapreduce.lib.input.CombineFileRecordReader.initNextRecordReader(CombineFileRecordReader.java:155)
    ... 10 more
Caused by: java.lang.IncompatibleClassChangeError: Found interface org.apache.hadoop.mapreduce.TaskAttemptContext, but class was expected
    at org.apache.mahout.text.WholeFileRecordReader.<init>(WholeFileRecordReader.java:59)
    ... 15 more


未能解决。

于是尝试利用mahout中自带的例子进行测试

使用测试样本在mahout的源码中,路径为F:\mahout\mahout-distribution-0.9-src\mahout-distribution-0.9\core\src\test\resources\retail.dat

将其上传至hdfs中,再运行如下命令

hadoop jar /opt/mahout-distribution-0.9/mahout-examples-0.9-job.jar org.apache.mahout.fpm.pfpgrowth.FPGrowthDriver -i /test/retail.dat -o /out2 -s 5 -method mapreduce

结果存至hdfs上的out2中。

mahout seqdumper -i /out2/frequentpatterns/part-r-00000 -o /home/mahout_test/fpresult5.txt

将其下载到本地后,

直接vi fpresult5.txt,即可看到结果,其中,部分结果如下:

Key: 10 : Value: ([10 ],6)
Key: 10 39 : Value: ([10 39 ],5)
Key: 1034 : Value: ([1034 ],5)
Key: 1146 : Value: ([1146 ],5)
Key: 13518 : Value: ([13518 ],15)
Key: 14098 : Value: ([14098 ],6)
Key: 14099 : Value: ([14099 ],6)
Key: 14386 : Value: ([14386 ],14)
Key: 15094 : Value: ([15094 ],11)
Key: 15685 : Value: ([15685 ],7)
Key: 15686 : Value: ([15686 ],7)
Key: 170 : Value: ([170 ],12)
Key: 2046 : Value: ([2046 ],19)
Key: 225 : Value: ([225 ],7)
Key: 225 2238 : Value: ([225 2238 ],6)
Key: 237 : Value: ([237 ],5)
Key: 286 : Value: ([286 ],14)
Key: 31 : Value: ([31 ],8)
Key: 32 : Value: ([32 ],273)
Key: 32 1046 : Value: ([32 1046 ],10)

key代表频繁模式,value代表这个频繁模式出现的次数

如果只想知道结果的条数

mahout seqdumper -i /out2/frequentpatterns/part-r-00000 -o /home/mahout_test/fpresult5.txt -c

fpresult5.txt结果如下:

Input Path: /out2/frequentpatterns/part-r-00000
Key class: class org.apache.hadoop.io.Text Value Class: class org.apache.mahout.fpm.pfpgrowth.convertors.string.TopKStringPatterns
Count: 147

有147条结果






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mahout 是一个基于 Hadoop 的机器学习库,提供了许多机器学习算法的实现,其中包括 FPGrowth 算法。下面是一个使用 Mahout 实现 FPGrowth 算法的示例: 1. 准备数据 首先,我们需要准备数据。假设我们有一个包含购物篮的数据集,每个购物篮中包含多个物品。数据集的格式如下: ``` A,B,C A,C,D B,C,E A,B,C,E A,C,E ``` 2. 安装 Mahout Mahout 是一个基于 Hadoop 的库,因此需要先安装 HadoopMahout 的安装过程比较简单,可以按照官方文档进行安装。 3. 数据预处理 在运行 FPGrowth 算法之前,需要将数据转换为 Mahout 中的 SequenceFile 格式。可以使用 Mahout 中的 seqdirectory 命令将数据转换为 SequenceFile 格式: ``` bin/mahout seqdirectory -i /path/to/data -o /path/to/output ``` 其中,/path/to/data 是原始数据的路径,/path/to/output 是输出路径。 4. 运行 FPGrowth 算法 使用 Mahout 中的 fpg 命令运行 FPGrowth 算法: ``` bin/mahout fpg -i /path/to/output -o /path/to/output/fpg -method mapreduce -regex '[\t,]' -s 2 -k 2 ``` 其中,/path/to/output 是数据预处理步骤中的输出路径,/path/to/output/fpgFPGrowth 算法的输出路径,-method 参数指定使用 MapReduce 运行算法,-regex 参数指定数据的分隔符,-s 参数指定支持度为 2,-k 参数指定最小支持度为 2。 5. 查看结果 FPGrowth 算法的输出结果包含频繁项集和关联规则。可以使用 Mahout 中的 fpgdumper 命令查看结果: ``` bin/mahout fpgdumper -i /path/to/output/fpg/frequentpatterns/ -o /path/to/output/fpg/frequentpatterns.txt -p true -d "," ``` 其中,/path/to/output/fpg/frequentpatterns/ 是 FPGrowth 算法的输出路径,/path/to/output/fpg/frequentpatterns.txt 是输出文件的路径,-p 参数指定输出频繁项集,-d 参数指定项之间的分隔符。 输出结果如下: ``` {A} 3 {A,C} 3 {B} 2 {B,C} 2 {C} 4 {C,E} 3 {E} 2 ``` 其中,每一行表示一个频繁项集及其支持度。 以上就是使用 Mahout 实现 FPGrowth 算法的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值