Spark Mllib测试

一、FPGrowth算法理解

        Spark.mllib 提供并行FP-growth算法,这个算法属于关联规则算法【关联规则:两不相交的非空集合A、B,如果A=>B,就说A=>B是一条关联规则,常提及的{啤酒}-->{尿布}就是一条关联规则】,经常用于挖掘频度物品集。关于算法的介绍网上很多,这里不再赘述。主要搞清楚几个概念:

1)支持度support(A => B) = P(AnB) = |A n B| / |N|,表示数据集D中,事件A和事件B共同出现的概率;

2)置信度confidence(A => B) = P(B|A) = |A n B| / |A|,表示数据集D中,出现事件A的事件中出现事件B的概率;

3)提升度lift(A => B) = P(B|A):P(B) = |A n B| / |A| : |B| / |N|,表示数据集D中,出现A的条件下出现事件B的概率和没有条件A出现B的概率;

由上可以看出,支持度表示这条规则的可能性大小,而置信度表示由事件A得到事件B的可信性大小。

举个列子:10000个消费者购买了商品,尿布1000个,啤酒2000个,同时购买了尿布和啤酒800个。 

1)支持度:在所有项集中出现的可能性,项集同时含有,x与y的概率。尿布和啤酒的支持度为:800/10000=8% 

2)置信度:在X发生的条件下,Y发生的概率。尿布-》啤酒的置信度为:800/1000=80%,啤酒-》尿布的置信度为:800/2000=40% 

3)提升度:在含有x条件下同时含有Y的可能性(x->y的置信度)比没有x这个条件下含有Y的可能性之比:confidence(尿布=> 啤酒)/概率(啤酒)) = 80%/(2000/10000) 。如果提升度=1,那就是没啥关系这两个

通过支持度和置信度可以得出强关联关系,通过提升的,可判别有效的强关联关系。

直接拿例子来说明问题。首先数据集如下:

r z h k p
z y x w v u t s
s x o n r
x z y m t s q e
z
x z y r q t p

二、代码实现。在IDEA中建立Maven工程,然后本地模式调试代码如下:

 
  1. import org.apache.spark.SparkConf;

  2. import org.apache.spark.api.java.JavaRDD;

  3. import org.apache.spark.api.java.JavaSparkContext;

  4. import org.apache.spark.api.java.function.Function;

  5. import org.apache.spark.mllib.fpm.AssociationRules;

  6. import org.apache.spark.mllib.fpm.FPGrowth;

  7. import org.apache.spark.mllib.fpm.FPGrowthModel;

  8.  
  9. import java.util.Arrays;

  10. import java.util.List;

  11.  
  12. public class FPDemo {

  13. public static void main(String[] args){

  14. String data_path; //数据集路径

  15. double minSupport = 0.2;//最小支持度

  16. int numPartition = 10; //数据分区

  17. double minConfidence = 0.8;//最小置信度

  18. if(args.length < 1){

  19. System.out.println("<input data_path>");

  20. System.exit(-1);

  21. }

  22. data_path = args[0];

  23. if(args.length >= 2)

  24. minSupport = Double.parseDouble(args[1]);

  25. if(args.length >= 3)

  26. numPartition = Integer.parseInt(args[2]);

  27. if(args.length >= 4)

  28. minConfidence = Double.parseDouble(args[3]);

  29.  
  30. SparkConf conf = new SparkConf().setAppName("FPDemo").setMaster("local");

  31. JavaSparkContext sc = new JavaSparkContext(conf);

  32.  
  33. //加载数据,并将数据通过空格分割

  34. JavaRDD<List<String>> transactions = sc.textFile(data_path)

  35. .map(new Function<String, List<String>>() {

  36. public List<String> call(String s) throws Exception {

  37. String[] parts = s.split(" ");

  38. return Arrays.asList(parts);

  39. }

  40. });

  41.  
  42. //创建FPGrowth的算法实例,同时设置好训练时的最小支持度和数据分区

  43. FPGrowth fpGrowth = new FPGrowth().setMinSupport(minSupport).setNumPartitions(numPartition);

  44. FPGrowthModel<String> model = fpGrowth.run(transactions);//执行算法

  45.  
  46. //查看所有频繁諅,并列出它出现的次数

  47. for(FPGrowth.FreqItemset<String> itemset : model.freqItemsets().toJavaRDD().collect())

  48. System.out.println("[" + itemset.javaItems() + "]," + itemset.freq());

  49.  
  50. //通过置信度筛选出强规则

  51. //antecedent表示前项

  52. //consequent表示后项

  53. //confidence表示规则的置信度

  54. for(AssociationRules.Rule<String> rule : model.generateAssociationRules(minConfidence).toJavaRDD().collect())

  55. System.out.println(rule.javaAntecedent() + "=>" + rule.javaConsequent() + ", " + rule.confidence());

  56. }

  57. }

       直接在Maven工程中运用上面的代码会有问题,因此这里需要添加依赖项解决项目中的问题,依赖项的添加如下:

 
  1. <dependencies>

  2. <dependency>

  3. <groupId>org.apache.spark</groupId>

  4. <artifactId>spark-core_2.10</artifactId>

  5. <version>2.1.0</version>

  6. </dependency>

  7. <dependency>

  8. <groupId>org.apache.spark</groupId>

  9. <artifactId>spark-mllib_2.10</artifactId>

  10. <version>2.1.0</version>

  11. </dependency>

  12. </dependencies>

        本地模式运行的结果如下:

 
  1. [t, s, y]=>[x], 1.0

  2. [t, s, y]=>[z], 1.0

  3. [y, x, z]=>[t], 1.0

  4. [y]=>[x], 1.0

  5. [y]=>[z], 1.0

  6. [y]=>[t], 1.0

  7. [p]=>[r], 1.0

  8. [p]=>[z], 1.0

  9. [q, t, z]=>[y], 1.0

  10. [q, t, z]=>[x], 1.0

  11. [q, y]=>[x], 1.0

  12. [q, y]=>[z], 1.0

  13. [q, y]=>[t], 1.0

  14. [t, s, x]=>[y], 1.0

  15. [t, s, x]=>[z], 1.0

  16. [q, t, y, z]=>[x], 1.0

  17. [q, t, x, z]=>[y], 1.0

  18. [q, x]=>[y], 1.0

  19. [q, x]=>[t], 1.0

  20. [q, x]=>[z], 1.0

  21. [t, x, z]=>[y], 1.0

  22. [x, z]=>[y], 1.0

  23. [x, z]=>[t], 1.0

  24. [p, z]=>[r], 1.0

  25. [t]=>[y], 1.0

  26. [t]=>[x], 1.0

  27. [t]=>[z], 1.0

  28. [y, z]=>[x], 1.0

  29. [y, z]=>[t], 1.0

  30. [p, r]=>[z], 1.0

  31. [t, s]=>[y], 1.0

  32. [t, s]=>[x], 1.0

  33. [t, s]=>[z], 1.0

  34. [q, z]=>[y], 1.0

  35. [q, z]=>[t], 1.0

  36. [q, z]=>[x], 1.0

  37. [q, y, z]=>[x], 1.0

  38. [q, y, z]=>[t], 1.0

  39. [y, x]=>[z], 1.0

  40. [y, x]=>[t], 1.0

  41. [q, x, z]=>[y], 1.0

  42. [q, x, z]=>[t], 1.0

  43. [t, y, z]=>[x], 1.0

  44. [q, y, x]=>[z], 1.0

  45. [q, y, x]=>[t], 1.0

  46. [q, t, y, x]=>[z], 1.0

  47. [t, s, x, z]=>[y], 1.0

  48. [s, y, x]=>[z], 1.0

  49. [s, y, x]=>[t], 1.0

  50. [s, x, z]=>[y], 1.0

  51. [s, x, z]=>[t], 1.0

  52. [q, y, x, z]=>[t], 1.0

  53. [s, y]=>[x], 1.0

  54. [s, y]=>[z], 1.0

  55. [s, y]=>[t], 1.0

  56. [q, t, y]=>[x], 1.0

  57. [q, t, y]=>[z], 1.0

  58. [t, y]=>[x], 1.0

  59. [t, y]=>[z], 1.0

  60. [t, z]=>[y], 1.0

  61. [t, z]=>[x], 1.0

  62. [t, s, y, x]=>[z], 1.0

  63. [t, y, x]=>[z], 1.0

  64. [q, t]=>[y], 1.0

  65. [q, t]=>[x], 1.0

  66. [q, t]=>[z], 1.0

  67. [q]=>[y], 1.0

  68. [q]=>[t], 1.0

  69. [q]=>[x], 1.0

  70. [q]=>[z], 1.0

  71. [t, s, z]=>[y], 1.0

  72. [t, s, z]=>[x], 1.0

  73. [t, x]=>[y], 1.0

  74. [t, x]=>[z], 1.0

  75. [s, z]=>[y], 1.0

  76. [s, z]=>[x], 1.0

  77. [s, z]=>[t], 1.0

  78. [s, y, x, z]=>[t], 1.0

  79. [s]=>[x], 1.0

  80. [t, s, y, z]=>[x], 1.0

  81. [s, y, z]=>[x], 1.0

  82. [s, y, z]=>[t], 1.0

  83. [q, t, x]=>[y], 1.0

  84. [q, t, x]=>[z], 1.0

  85. [r, z]=>[p], 1.0

三、Spark集群部署。代码修改正如:

 
  1. import org.apache.spark.SparkConf;

  2. import org.apache.spark.api.java.JavaRDD;

  3. import org.apache.spark.api.java.JavaSparkContext;

  4. import org.apache.spark.api.java.function.Function;

  5. import org.apache.spark.mllib.fpm.AssociationRules;

  6. import org.apache.spark.mllib.fpm.FPGrowth;

  7. import org.apache.spark.mllib.fpm.FPGrowthModel;

  8.  
  9. import java.util.Arrays;

  10. import java.util.List;

  11.  
  12. public class FPDemo {

  13. public static void main(String[] args){

  14. String data_path; //数据集路径

  15. double minSupport = 0.2;//最小支持度

  16. int numPartition = 10; //数据分区

  17. double minConfidence = 0.8;//最小置信度

  18. if(args.length < 1){

  19. System.out.println("<input data_path>");

  20. System.exit(-1);

  21. }

  22. data_path = args[0];

  23. if(args.length >= 2)

  24. minSupport = Double.parseDouble(args[1]);

  25. if(args.length >= 3)

  26. numPartition = Integer.parseInt(args[2]);

  27. if(args.length >= 4)

  28. minConfidence = Double.parseDouble(args[3]);

  29.  
  30. SparkConf conf = new SparkConf().setAppName("FPDemo");////修改的地方

  31. JavaSparkContext sc = new JavaSparkContext(conf);

  32.  
  33. //加载数据,并将数据通过空格分割

  34. JavaRDD<List<String>> transactions = sc.textFile(data_path)

  35. .map(new Function<String, List<String>>() {

  36. public List<String> call(String s) throws Exception {

  37. String[] parts = s.split(" ");

  38. return Arrays.asList(parts);

  39. }

  40. });

  41.  
  42. //创建FPGrowth的算法实例,同时设置好训练时的最小支持度和数据分区

  43. FPGrowth fpGrowth = new FPGrowth().setMinSupport(minSupport).setNumPartitions(numPartition);

  44. FPGrowthModel<String> model = fpGrowth.run(transactions);//执行算法

  45.  
  46. //查看所有频繁諅,并列出它出现的次数

  47. for(FPGrowth.FreqItemset<String> itemset : model.freqItemsets().toJavaRDD().collect())

  48. System.out.println("[" + itemset.javaItems() + "]," + itemset.freq());

  49.  
  50. //通过置信度筛选出强规则

  51. //antecedent表示前项

  52. //consequent表示后项

  53. //confidence表示规则的置信度

  54. for(AssociationRules.Rule<String> rule : model.generateAssociationRules(minConfidence).toJavaRDD().collect())

  55. System.out.println(rule.javaAntecedent() + "=>" + rule.javaConsequent() + ", " + rule.confidence());

  56. }

  57. }

       然后在IDEA中打包成JAR包

       然后在工具栏

生成Jar包,然后上传到集群中执行命令

        得到结果

猜你喜欢

转载自blog.csdn.net/caizhengwu/article/details/81541843