本章节介绍了分类和回归的算法。它还包括讨论特定类别的算法部分,如:线性方法,树和集成。

下面是整个API Doc中的内容框架,这里不会每个都详细介绍,主要会把用到的介绍出来,后续用到的再陆续添加。(下面的链接都是指向官网文档而不是本笔记中的对应内容所在位置,而且有些内容没有出现在本笔记中)

逻辑回归

逻辑回归是预测分类问题的流行算法。它是 广义线性模型的一个特例来预测结果的可能性。 在spark.ml逻辑回归中可以使用二项式Logistic回归来预测二分类问题,也可以通过使用多项Logistic回归来预测多分类问题。 使用family参数在这两种算法之间进行选择,或者不设置它,让Spark自己推断出正确的值。

通过将family参数设置为“多项式”,也可以将多项Logistic回归用于二分类问题。它将产生两个系数的集合和两个intercept。

当在没有intercept的常量非零列的数据集上对LogisticRegressionModel进行拟合时,Spark MLlib为常数非零列输出零系数。此行为与R glmnet相同,但与LIBSVM不同。

二分类逻辑回归

有关二项式逻辑回归实现的更多背景和更多细节,请参阅spark.mllib中逻辑回归的文档。

代码示例:

以下示例显示了如何用elastic net regularization来训练的二项式和多项Logistic的回归模型用于二分类问题。 elasticNetParam对应于α,regParam对应于λ。

public class JavaLogisticRegressionWithElasticNetExample {

public static void main(String[] args) {

SparkSession spark = SparkSession

.builder()

.appName("JavaLogisticRegressionWithElasticNetExample")

.getOrCreate();

// $example on$

// Load training data

Dataset training = spark.read().format("libsvm")

.load("/home/paul/spark/spark-2.1.0-bin-hadoop2.7/data/mllib/sample_libsvm_data.txt");

LogisticRegression lr = new LogisticRegression()

.setMaxIter(10)

.setRegParam(0.3)

.setElasticNetParam(0.8);

// Fit the model

LogisticRegressionModel lrModel = lr.fit(training);

// Print the coefficients and intercept for logistic regression

System.out.println("\n---------- Binomial logistic regression's Coefficients: "

+ lrModel.coefficients() + "\nBinomial Intercept: " + lrModel.intercept());

// We can also use the multinomial family for binary classification

LogisticRegression mlr = new LogisticRegression()

.setMaxIter(10)

.setRegParam(0.3)

.setElasticNetParam(0.8)

.setFamily("multinomial");

// Fit the model

LogisticRegressionModel mlrModel = mlr.fit(training);

// Print the coefficients and intercepts for logistic regression with multinomial family

System.out.println("\n+++++++++ Multinomial coefficients: " + mlrModel.coefficientMatrix()

+ "\nMultinomial intercepts: " + mlrModel.interceptVector());

// $example off$

spark.stop();

}

}

上面代码运行结果,二项式的系数是按照稀疏矩阵格式打印的,多项式的是按照矩阵的格式打印

spark.ml实现的逻辑回归算法也支持提取出训练集上训练后模型的摘要(这有助于分析模型在训练集上的性能)。 需要注意的是预测结果和权值在BinaryLogisticRegressionSummary中被存储为DataFrame类型并且被标注为@transient,所以只能在driver上可用。

LogisticRegressionTrainingSummary是提供给LogisticRegressionModel的摘要。目前只有二分类模型有这个功能,而且必须被显式的强转成类型BinaryLogisticRegressionTrainingSummary。对于多分类模型的摘要的支持将在后续版本中实现。

Java版代码:

public class JavaLogisticRegressionSummaryExample {

public static void main(String[] args) {

SparkSession spark = SparkSession

.builder()

.appName("JavaLogisticRegressionSummaryExample")

.getOrCreate();

// Load training data

Dataset training = spark.read().format("libsvm")

.load("/home/paul/spark/spark-2.1.0-bin-hadoop2.7/data/mllib/sample_libsvm_data.txt");

LogisticRegression lr = new LogisticRegression()

.setMaxIter(10)

.setRegParam(0.3)

.setElasticNetParam(0.8);

// Fit the model

LogisticRegressionModel lrModel = lr.fit(training);

// $example on$

// Extract the summary from the returned LogisticRegressionModel instance trained in the earlier

// example

LogisticRegressionTrainingSummary trainingSummary = lrModel.summary();

// Obtain the loss per iteration.

double[] objectiveHistory = trainingSummary.objectiveHistory();

for (double lossPerIteration : objectiveHistory) {

System.out.println(lossPerIteration);

}

// Obtain the metrics useful to judge performance on test data.

// We cast the summary to a BinaryLogisticRegressionSummary since the problem is a binary

// classification problem.

BinaryLogisticRegressionSummary binarySummary =

(BinaryLogisticRegressionSummary) trainingSummary;

// Obtain the receiver-operating characteristic as a dataframe and areaUnderROC.

Dataset roc = binarySummary.roc();

roc.show();

roc.select("FPR").show();

System.out.println(binarySummary.areaUnderROC());

// Get the threshold corresponding to the maximum F-Measure and rerun LogisticRegression with

// this selected threshold.

Dataset fMeasure = binarySummary.fMeasureByThreshold();

double maxFMeasure = fMeasure.select(functions.max("F-Measure")).head().getDouble(0);

double bestThreshold = fMeasure.where(fMeasure.col("F-Measure").equalTo(maxFMeasure))

.select("threshold").head().getDouble(0);

lrModel.setThreshold(bestThreshold);

// $example off$

spark.stop();

}

}

运行结果为:

0.6833149135741672

0.6662875751473734

0.6217068546034618

0.6127265245887887

0.6060347986802873

0.6031750687571562

0.5969621534836274

0.5940743031983118

0.5906089243339022

0.5894724576491042

0.5882187775729587

17/05/02 22:46:21 WARN Executor: 1 block locks were not released by TID = 25:

[rdd_39_0]

+---+--------------------+

|FPR| TPR|

+---+--------------------+

|0.0| 0.0|

|0.0|0.017543859649122806|

|0.0| 0.03508771929824561|

|0.0| 0.05263157894736842|

|0.0| 0.07017543859649122|

|0.0| 0.08771929824561403|

|0.0| 0.10526315789473684|

|0.0| 0.12280701754385964|

|0.0| 0.14035087719298245|

|0.0| 0.15789473684210525|

|0.0| 0.17543859649122806|

|0.0| 0.19298245614035087|

|0.0| 0.21052631578947367|

|0.0| 0.22807017543859648|

|0.0| 0.24561403508771928|

|0.0| 0.2631578947368421|

|0.0| 0.2807017543859649|

|0.0| 0.2982456140350877|

|0.0| 0.3157894736842105|

|0.0| 0.3333333333333333|

+---+--------------------+

only showing top 20 rows

17/05/02 22:46:22 WARN Executor: 1 block locks were not released by TID = 27:

[rdd_39_0]

+---+

|FPR|

+---+

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

|0.0|

+---+

only showing top 20 rows

1.0

mllib逻辑回归 spark_Spark MLlib学习——分类和回归相关推荐

  1. ML之分类预测之LARS:利用回归工具将二分类转为回归问题并采用LARS算法构建分类器

    ML之分类预测之LARS:利用回归工具将二分类转为回归问题并采用LARS算法构建分类器 目录 输出结果 设计思路 代码实现 输出结果 ['V10', 'V48', 'V44', 'V11', 'V35 ...

  2. mllib逻辑回归 spark_Spark mllib 逻辑回归

    逻辑回归 逻辑回归其实是一个分类算法而不是回归算法.通常是利用已知的自变量来预测一个离散型因变量的值(像二进制值0/1,是/否,真/假).简单来说,它就是通过拟合一个逻辑函数(logit fuctio ...

  3. ml回归_ML中的分类和回归是什么?

    ml回归 机器学习教程 (MACHINE LEARNING TUTORIAL) ML is extracting data from knowledge. ML正在从知识中提取数据. Machine ...

  4. r 多元有序logistic回归_R实现多分类logistic回归

    多分类logistic回归 在临床研究中,接触最多的是二分类数据,如淋巴癌是否转移,是否死亡,这些因变量最后都可以转换成二分类0与1的问题.然后建立二元logistic回归方程,可以得到影响因素的OR ...

  5. mllib逻辑回归 spark_Spark Mllib中逻辑回归

    1相关参数设置 <1> setMaxIter():设置最大迭代次数 <2> setRegParam(): 设置正则项的参数,控制损失函数与惩罚项的比例,防止整个训练过程过拟合, ...

  6. mllib调参 spark_Spark MLlib协同过滤算法

    算法说明 协同过滤(Collaborative Filtering,简称CF,WIKI上的定义是:简单来说是利用某个兴趣相投.拥有共同经验之群体的喜好来推荐感兴趣的资讯给使用者,个人透过合作的机制给予 ...

  7. 数据分析--分类与回归模型(一)

    一.分类回归方法 主要的分类.回归算法,网上和书上的资料进行梳理整理. 二.各类分类方法 代码参照<人工智能:python实现>一书,对部分代码进行了修改. 1.logistic 回归 l ...

  8. 一文掌握多分类logistic回归

    Logistic回归分析(logit回归)一般可分为3类,分别是二元Logistic回归分析.多分类Logistic回归分析和有序Logistic回归分析.logistic回归分析类型如下所示. Lo ...

  9. 分类与回归(如何把分类问题转化为回归问题解决)

    如何把分类问题转化为回归问题解决 一.分类与回归的区 我们都知道,有监督学习算法都在做一样事情,那就是预测.但是显然,针对不同的事件,我们有不同的预测的目标.其中,预测目标的形式(变量)有离散和连续两 ...

最新文章

  1. LTE TDD的特殊子帧
  2. tensorflow环境下的识别食物_在win10环境下进行tensorflow物体识别(ObjectDetection)训练...
  3. [JavaScript] DOM操作技术
  4. 根据另外一个表来更新,增加字段
  5. qq说说时间轴php实现,qq空间时间轴 PHP实现时间轴函数代码
  6. php菱形,PHP 打印菱形
  7. 自定义 View 之雅虎新闻视差动画
  8. 一分钟整明白web前端和Java后端的就业前景
  9. 帮优质粉丝脱单|【英国女】No.33|22岁,硕士,喜欢旅行爱好做饭,消费者心理学...
  10. JS Uncaught TypeError: Cannot read property ‘indexOf‘ of null问题解决
  11. Ceph 存储集群5-数据归置
  12. java 怎么让打印信息换行?
  13. pr导出视频在哪里?
  14. win7共享xp打印机_快速有效的局域网共享方法,十年老技术现身说法亲测可用
  15. 我的第一篇 实习报告
  16. 传输层之UDP头部解析、UDP特点TCP头部解析、TCP特点
  17. 护理方面关于人工智能的构想_谷歌秘密的atap实验室正在构想智能设备的未来...
  18. 一元夺宝的2种押注策略分析
  19. Marry me(嫁给我)
  20. Linux下查看隐藏文件命令

热门文章

  1. android 解决APP退出后以及后台运行时,再次点击图标的运行问题
  2. 初学物联网:智能手表制作方案
  3. it计算机工资,什么是it技术(it工资一般多少)
  4. 华为机试真题 Java 实现【Excel单元格数值统计】【2022.11 Q4】
  5. c++【IO挂】【输入输出挂】
  6. C#语言实例源码系列-实现ComboBox变色
  7. python计算两个日期相差的天数的代码
  8. win11关闭蓝牙耳机音质差的handsfree模式
  9. linux下strcmp函数,Linux登录和strcmp
  10. 爱奇艺谢丹铭:用AI让创作者提升效率,让消费者简单快乐