一, jar依赖,jsc创建。

package ML.BasicStatistics;

import com.google.common.collect.Lists;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaDoubleRDD;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.DoubleFlatMapFunction;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import org.apache.spark.mllib.linalg.Matrices;
import org.apache.spark.mllib.linalg.Matrix;
import org.apache.spark.mllib.linalg.Vector;
import org.apache.spark.mllib.linalg.Vectors;
import org.apache.spark.mllib.regression.LabeledPoint;
import org.apache.spark.mllib.stat.KernelDensity;
import org.apache.spark.mllib.stat.MultivariateStatisticalSummary;
import org.apache.spark.mllib.stat.Statistics;
import org.apache.spark.mllib.stat.test.ChiSqTestResult;
import org.apache.spark.mllib.stat.test.KolmogorovSmirnovTestResult;
import org.apache.spark.mllib.util.MLUtils;
import org.apache.spark.rdd.RDD;
import scala.Tuple2;
import scala.runtime.Statics;
import static org.apache.spark.mllib.random.RandomRDDs.*; import java.util.*; /**
* TODO
*
* @ClassName: BasicStatistics
* @author: DingH
* @since: 2019/4/3 16:11
*/
public class BasicStatistics {
public static void main(String[] args) {
System.setProperty("hadoop.home.dir","E:\\hadoop-2.6.5");
SparkConf conf = new SparkConf().setAppName("BasicStatistics").setMaster("local");
JavaSparkContext jsc = new JavaSparkContext(conf);

二。Summary statistics

        /**
* @Title: Statistics.colStats一个实例MultivariateStatisticalSummary,其中包含按列的max,min,mean,variance和非零数,以及总计数
* Summary statistics:摘要统计
*/
JavaRDD<Vector> parallelize = jsc.parallelize(Arrays.asList(
Vectors.dense(1, 0, 3),
Vectors.dense(2, 0, 4),
Vectors.dense(3, 0, 5)
));
MultivariateStatisticalSummary summary = Statistics.colStats(parallelize.rdd());
System.out.println(summary.mean());
System.out.println(summary.variance());
System.out.println(summary.numNonzeros());

三。Correlations:相关性

        /**
* @Title: Correlations:相关性
*/
JavaRDD<Tuple2<String, String>> parallelize = jsc.parallelize(Lists.newArrayList(
new Tuple2<String, String>("cat", "11"),
new Tuple2<String, String>("dog", "22"),
new Tuple2<String, String>("cat", "33"),
new Tuple2<String, String>("pig", "44") )); JavaDoubleRDD seriesX = parallelize.mapPartitionsToDouble(new DoubleFlatMapFunction<Iterator<Tuple2<String, String>>>() {
public Iterable<Double> call(Iterator<Tuple2<String, String>> tuple2Iterator) throws Exception {
ArrayList<Double> strings = new ArrayList<Double>();
while (tuple2Iterator.hasNext()){
strings.add(Double.parseDouble(tuple2Iterator.next()._2));
}
return strings;
}
});
JavaDoubleRDD seriesY = parallelize.mapPartitionsToDouble(new DoubleFlatMapFunction<Iterator<Tuple2<String, String>>>() {
public Iterable<Double> call(Iterator<Tuple2<String, String>> tuple2Iterator) throws Exception {
ArrayList<Double> strings = new ArrayList<Double>();
while (tuple2Iterator.hasNext()){
strings.add(Double.parseDouble(tuple2Iterator.next()._2)+1);
}
return strings;
}
});
//compute the correlation using Pearson's method. Enter "spearman" for Spearman's method. If a
//method is not specified, Pearson's method will be used by default.
double correlation = Statistics.corr(seriesX.srdd(), seriesY.srdd(), "pearson"); JavaRDD<Vector> parallelize11 = jsc.parallelize(Arrays.asList(
Vectors.dense(1, 0, 3),
Vectors.dense(2, 0, 4),
Vectors.dense(3, 0, 5)
));// note that each Vector is a row and not a column
Matrix correlation2 = Statistics.corr(parallelize11.rdd(), "spearman");
System.out.println(correlation2);

三,Stratified sampling:分层抽样

        /**
* @Title: Stratified sampling:分层抽样
*/
JavaRDD<Tuple2<String, String>> parallelize = jsc.parallelize(Lists.newArrayList(
new Tuple2<String, String>("cat", "11"),
new Tuple2<String, String>("dog", "22"),
new Tuple2<String, String>("cat", "33"),
new Tuple2<String, String>("pig", "44") ));
JavaPairRDD data = parallelize.mapToPair(new PairFunction<Tuple2<String, String>, String, String>() {
public Tuple2<String, String> call(Tuple2<String, String> stringStringTuple2) throws Exception {
return new Tuple2<String, String>(stringStringTuple2._1, stringStringTuple2._2);
}
}); // an RDD of any key value pairs
Map<String, Double> fractions = new HashMap<String, Double>(); // specify the exact fraction desired from each key
fractions.put("cat",0.5); //对于每个key取值的概率
fractions.put("dog",0.8);
fractions.put("pig",0.8);
// Get an exact sample from each stratum
JavaPairRDD approxSample = data.sampleByKey(false, fractions);
JavaPairRDD exactSample = data.sampleByKeyExact(false, fractions);
approxSample.foreach(new VoidFunction() {
public void call(Object o) throws Exception {
System.out.println(o);
}
});

四。Hypothesis testing  假设检验

        /**
* @Title: Hypothesis testing 假设检验
*/ Vector vec = Vectors.dense(1,2,3,4); // a vector composed of the frequencies of events // compute the goodness of fit. If a second vector to test against is not supplied as a parameter,
// the test runs against a uniform distribution.
ChiSqTestResult goodnessOfFitTestResult = Statistics.chiSqTest(vec);
// summary of the test including the p-value, degrees of freedom, test statistic, the method used,
// and the null hypothesis.
System.out.println(goodnessOfFitTestResult); Matrix mat = Matrices.dense(3,2,new double[]{1,2,3,4,5,6}); // a contingency matrix // conduct Pearson's independence test on the input contingency matrix
ChiSqTestResult independenceTestResult = Statistics.chiSqTest(mat);
// summary of the test including the p-value, degrees of freedom...
System.out.println(independenceTestResult); JavaRDD<LabeledPoint> obs = MLUtils.loadLibSVMFile(jsc.sc(), "/data...").toJavaRDD(); // an RDD of labeled points // The contingency table is constructed from the raw (feature, label) pairs and used to conduct
// the independence test. Returns an array containing the ChiSquaredTestResult for every feature
// against the label.
ChiSqTestResult[] featureTestResults = Statistics.chiSqTest(obs.rdd());
int i = 1;
for (ChiSqTestResult result : featureTestResults) {
System.out.println("Column " + i + ":");
System.out.println(result); // summary of the test
i++;
} JavaDoubleRDD data = jsc.parallelizeDoubles(Arrays.asList(0.2, 1.0,0.3));
KolmogorovSmirnovTestResult testResult = Statistics.kolmogorovSmirnovTest(data,"norm");
// summary of the test including the p-value, test statistic,
// and null hypothesis
// if our p-value indicates significance, we can reject the null hypothesis
System.out.println(testResult);

五。Random data generation

         /**
* @Title: Random data generation :uniform, standard normal, or Poisson.
*/ JavaDoubleRDD u = normalJavaRDD(jsc, 100,2);
// Apply a transform to get a random double RDD following `N(1, 4)`.
JavaRDD<Double> map = u.map(new Function<Double, Double>() {
public Double call(Double aDouble) throws Exception {
return 1.0 + 2.0 * aDouble;
}
});
map.foreach(new VoidFunction<Double>() {
public void call(Double aDouble) throws Exception {
System.out.println(aDouble);
}
});

六。Kernel density estimation

        /**
* @Title: Kernel density estimation
*/
JavaRDD<Double> data = jsc.parallelize(Arrays.asList(1.0, 2.0, 3.0));// an RDD of sample data // Construct the density estimator with the sample data and a standard deviation for the Gaussian
// kernels
KernelDensity kd = new KernelDensity()
.setSample(data)
.setBandwidth(3.0); // Find density estimates for the given values
double[] densities = kd.estimate(new double[] {-1.0, 2.0, 5.0});
for (int i = 0; i < densities.length; i++) {
System.out.println(densities[i]);
}

spark MLlib BasicStatistics 统计学基础的更多相关文章

  1. spark MLLib的基础统计部分学习

    参考学习链接:http://www.itnose.net/detail/6269425.html 机器学习相关算法,建议初学者去看看斯坦福的机器学习课程视频:http://open.163.com/s ...

  2. 【原创 Hadoop&Spark 动手实践 12】Spark MLLib 基础、应用与信用卡欺诈检测系统动手实践

    [原创 Hadoop&Spark 动手实践 12]Spark MLLib 基础.应用与信用卡欺诈检测系统动手实践

  3. Spark MLlib 机器学习

    本章导读 机器学习(machine learning, ML)是一门涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多领域的交叉学科.ML专注于研究计算机模拟或实现人类的学习行为,以获取新知识.新 ...

  4. 《Spark MLlib机器学习实践》内容简介、目录

      http://product.dangdang.com/23829918.html Spark作为新兴的.应用范围最为广泛的大数据处理开源框架引起了广泛的关注,它吸引了大量程序设计和开发人员进行相 ...

  5. Spark MLlib 之 Basic Statistics

    Spark MLlib提供了一些基本的统计学的算法,下面主要说明一下: 1.Summary statistics 对于RDD[Vector]类型,Spark MLlib提供了colStats的统计方法 ...

  6. Spark MLlib - Decision Tree源码分析

    http://spark.apache.org/docs/latest/mllib-decision-tree.html 以决策树作为开始,因为简单,而且也比较容易用到,当前的boosting或ran ...

  7. Spark入门实战系列--8.Spark MLlib(上)--机器学习及SparkMLlib简介

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .机器学习概念 1.1 机器学习的定义 在维基百科上对机器学习提出以下几种定义: l“机器学 ...

  8. Spark入门实战系列--8.Spark MLlib(下)--机器学习库SparkMLlib实战

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .MLlib实例 1.1 聚类实例 1.1.1 算法说明 聚类(Cluster analys ...

  9. Spark MLlib知识点学习整理

    MLlib的设计原理:把数据以RDD的形式表示,然后在分布式数据集上调用各种算法.MLlib就是RDD上一系列可供调用的函数的集合. 操作步骤: 1.用字符串RDD来表示信息. 2.运行MLlib中的 ...

随机推荐

  1. 【数学建模】MATLAB语法

    一.向量.矩阵的表示和使用 format long  %小数很多format short %默认4位小数format rat %显示最近的分数format short e %指数格式的数 尾数多少 e ...

  2. 设置 sql_mode

    MySQL 的 sql_mode 合理设置 sql_mode 是个很容易被忽视的变量,默认值是空值,在这种设置下是可以允许一些非法操作的,比如允许一些非法数据的插入.在生产环境必须将这个值设置为严格模 ...

  3. logstash/conf.d文件编写

    logstash-01.conf input { beats { port => 5044 host => "0.0.0.0" type => "log ...

  4. BZOJ2870 最长道路

    题意:给定树,有点权.求一条路径使得最小点权 * 总点数最大.只需输出这个最大值.5w. 解:树上路径问题,点分治. 考虑合并两个子树的时候,答案的形式是val1 * (d1 + d2),当1是新插入 ...

  5. Harbo1.5.2离线搭建

    环境说明 操作系统版本:Centos7.5 docker版本:docker-ce 17.03.2 harbor版本:v1.5.2 docker-compose:  1.22.0 基础环境搭建 系统优化 ...

  6. Docker:单机编排工具docker-compose [十二]

    一.docker-compose的安装 1.安装 curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.re ...

  7. 我对SAP Business One 项目实施的理解

    一.什么是SAP: 大家都知道ERP是什么,ERP是企业资源计划管理系统.是指建立在信息技术基础上,集信息技术与先进管理思想于一身,以系统化的管理思想,为企业员工及决策层提供决策手段的管理平台.那么问 ...

  8. JGUI源码:Accordion折叠到侧边栏实现(6)

    折叠和非折叠效果如左右图所示 代码如下 //折叠 $.fn.jAccordionfold = function() { return this.each(function() { var obj = ...

  9. [再寄小读者之数学篇](2014-06-20 求极限---Jordan 不等式的应用)

    证明: 当 $\lm<1$ 时, $\dps{\lim_{R\to+\infty} R^\lm\int_0^{\pi/2} e^{-R\sin\tt}\rd \tt=0}$. 证明: 由 $$\ ...

  10. [物理学与PDEs]第2章第2节 粘性流体力学方程组 2.1 引言

    1.  实际的流体与理想流体的主要区别在于: 前者具有粘性 (内摩擦) 和热传导. 2.  内摩擦 (1)  当两层流体有相对运动时, 方有摩擦力; 它是一种内力; 单位面积上所受的内力称为应力; 而 ...