Spark MLlib學習——分類和回歸

本章節(jié)介紹了分類和回歸的算法夏跷。它還包括討論特定類別的算法部分哼转,如:線性方法,樹和集成槽华。
下面是整個API Doc中的內容框架壹蔓,這里不會每個都詳細介紹,主要會把用到的介紹出來猫态,后續(xù)用到的再陸續(xù)添加雁刷。(下面的鏈接都是指向官網文檔而不是本筆記中的對應內容所在位置织阳,而且有些內容沒有出現在本筆記中)

Classification 分類

邏輯回歸

邏輯回歸是預測分類問題的流行算法青瀑。它是 廣義線性模型的一個特例來預測結果的可能性衣撬。 在spark.ml邏輯回歸中可以使用二項式Logistic回歸來預測二分類問題酿联,也可以通過使用多項Logistic回歸來預測多分類問題终息。 使用family參數在這兩種算法之間進行選擇,或者不設置它贞让,讓Spark自己推斷出正確的值周崭。

通過將family參數設置為“多項式”,也可以將多項Logistic回歸用于二分類問題喳张。它將產生兩個系數的集合和兩個intercept续镇。

當在沒有intercept的常量非零列的數據集上對LogisticRegressionModel進行擬合時,Spark MLlib為常數非零列輸出零系數销部。此行為與R glmnet相同摸航,但與LIBSVM不同制跟。

二分類邏輯回歸

有關二項式邏輯回歸實現的更多背景和更多細節(jié),請參閱spark.mllib中邏輯回歸的文檔酱虎。
代碼示例:
以下示例顯示了如何用elastic net regularization來訓練的二項式和多項Logistic的回歸模型用于二分類問題雨膨。 elasticNetParam對應于αregParam對應于λ读串。(這兩個參數的定義參見Linear methods)
Java版代碼

public class JavaLogisticRegressionWithElasticNetExample {
  public static void main(String[] args) {
    SparkSession spark = SparkSession
      .builder()
      .appName("JavaLogisticRegressionWithElasticNetExample")
      .getOrCreate();

    // $example on$
    // Load training data
    Dataset<Row> 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
。對于多分類模型的摘要的支持將在后續(xù)版本中實現琼娘。
Java版代碼:

public class JavaLogisticRegressionSummaryExample {
  public static void main(String[] args) {
    SparkSession spark = SparkSession
      .builder()
      .appName("JavaLogisticRegressionSummaryExample")
      .getOrCreate();

    // Load training data
    Dataset<Row> 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<Row> 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<Row> 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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末峭弟,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子脱拼,更是在濱河造成了極大的恐慌瞒瘸,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件熄浓,死亡現場離奇詭異情臭,居然都是意外死亡,警方通過查閱死者的電腦和手機赌蔑,發(fā)現死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門俯在,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人娃惯,你說我怎么就攤上這事跷乐。” “怎么了趾浅?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵愕提,是天一觀的道長。 經常有香客問我皿哨,道長浅侨,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任证膨,我火速辦了婚禮如输,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己不见,他們只是感情好澳化,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著脖祈,像睡著了一般肆捕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上盖高,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天慎陵,我揣著相機與錄音,去河邊找鬼喻奥。 笑死席纽,一個胖子當著我的面吹牛,可吹牛的內容都是我干的撞蚕。 我是一名探鬼主播润梯,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼甥厦!你這毒婦竟也來了纺铭?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤刀疙,失蹤者是張志新(化名)和其女友劉穎舶赔,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體谦秧,經...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡竟纳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了疚鲤。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锥累。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖集歇,靈堂內的尸體忽然破棺而出桶略,到底是詐尸還是另有隱情,我是刑警寧澤诲宇,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布际歼,位于F島的核電站,受9級特大地震影響焕窝,放射性物質發(fā)生泄漏蹬挺。R本人自食惡果不足惜维贺,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一它掂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦虐秋、人聲如沸榕茧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽用押。三九已至,卻和暖如春靶剑,著一層夾襖步出監(jiān)牢的瞬間蜻拨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工桩引, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留缎讼,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓坑匠,卻偏偏與公主長得像血崭,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子厘灼,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內容