我是從業10的Java開發老兵,之前只看其餘人的博客,通過今天的思考決定開博。 目的有兩個:其一把本身親身經歷的項目種種作個覆盤記錄下來,閒暇之時翻翻回憶一下(開始健忘了,(^_^))。其二鍛鍊一下本身的文字功底,不只要有好的想法,若是能用文字表達出來就更加完美。java
個人運行環境是spark-2.3.1-bin-hadoop2.7+hadoop2.7+win10+hadoop_dll2.6.0_64bit,從零開始搭環境耗費幾天時間,由於正常的工做任務要完成。我是從官網下載的spark+hadoop,從csdn上花積分下載的hadoop_dll2.6.0_64bit,安裝配置過程搜索引擎吧。apache
pom.xml核心依賴:這個過程我不太順利,jar下載不下來,致使運行報錯沒有發現類的運行時錯誤,跟網絡有關係,**解決辦法:**發現那個有問題後找到本地庫位置後刪除掉,右鍵->manven->update project,就會從新下載。bash
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.11</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_2.11</artifactId>
<version>2.3.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
複製代碼
示例代碼在spark-2.3.1-bin-hadoop2.7\examples\src\main下,我用的java實現網絡
public class JavaLinearRegressionWithElasticNetExample {
public static void main(String[] args) {
SparkSession spark = SparkSession.builder().appName("JavaLinearRegressionWithElasticNetExample").master("local")
.getOrCreate();
// $example on$
// Load training data.
Dataset<Row> training = spark.read().format("libsvm").load("data/sample_linear_regression_data.txt");
LinearRegression lr = new LinearRegression().setMaxIter(100).setRegParam(0.8).setElasticNetParam(0.8);
// Fit the model.
LinearRegressionModel lrModel = lr.fit(training);
// Print the coefficients and intercept for linear regression.
System.out.println("Coefficients: " + lrModel.coefficients() + " Intercept: " + lrModel.intercept());
// Summarize the model over the training set and print out some metrics.
LinearRegressionTrainingSummary trainingSummary = lrModel.summary();
System.out.println("numIterations: " + trainingSummary.totalIterations());
System.out.println("objectiveHistory: " + Vectors.dense(trainingSummary.objectiveHistory()));
trainingSummary.residuals().show();
System.out.println("RMSE: " + trainingSummary.rootMeanSquaredError());
System.out.println("r2: " + trainingSummary.r2());
// $example off$
lrModel.transform(training).select("features", "label", "prediction").show();
spark.stop();
}
}
複製代碼
代碼釋義請參考《SPARK MLLIB機器學習》這個本,尊重原創請購買正版書籍。敲黑板劃重點 那麼既然是官網的示例有什麼不一樣呢?關鍵在於這行,這行代碼的做用就是輸出預測結果(限於時間關係我用的也是training數據集)lrModel.transform(training).select("features", "label", "prediction").show();對於初學者來講能把預測結果輸出出來是多麼重要啊,提高學習的自信心呢!app
+-------------------+
| residuals|
+-------------------+
| -1020073.172416118|
| 1779184.442462788|
| 1313336.9211495668|
| 4165697.1427883618|
|-1404048.5767556876|
| 4972683.339662604|
| -4435613.32077004|
| 373631.6067474559|
| -5744798.382868968|
+-------------------+
RMSE: 3379675.1928499704
r2: 0.9703976727070023
複製代碼
+--------------------+-------------+--------------------+
| features| label| prediction|
+--------------------+-------------+--------------------+
|(4,[0,1,2,3],[263...| 2528527.75| 3548600.922416118|
|(4,[0,1,2,3],[750...| 4130186.0| 2351001.557537212|
|(4,[0,1,2,3],[297...| 3.64669408E7| 3.515360387885043E7|
|(4,[0,1,2,3],[820...|3.384003015E7|2.9674333007211637E7|
|(4,[0,1,2,3],[661...|6.117732849E7| 6.258137706675569E7|
|(4,[0,1,2,3],[1.2...| 5.63341144E7|5.1361431060337394E7|
|(4,[0,1,2,3],[1.3...|4.407602894E7| 4.851164226077004E7|
|(4,[0,1,2,3],[968...|3.016595874E7|2.9792327133252542E7|
|(4,[0,1,2,3],[695...| 1.7447519E7|2.3192317382868968E7|
+--------------------+-------------+--------------------+
複製代碼
特徵、標籤值、預測值機器學習
2528527.75 1:263455.75 2:275799 3:709413 4:629950
4130186 1:750437 2:201574.5 3:443560 4:409805
36466940.8 1:2974719.5 2:7677371.75 3:7301996.95 4:8867920.5
33840030.15 1:8207536.9 2:9843736.6 3:8679155.35 4:5086838
61177328.49 1:6615422.05 2:15269585.4 3:15642690.7 4:13381847.16
56334114.4 1:12991518.43 2:13798081.9 3:12502771.4 4:12899599.5
44076028.94 1:13081200.65 2:13837499.9 3:12257050.8 4:11567330.24
30165958.74 1:9683931.6 2:9116163.8 3:5350176.6 4:9081991.59
17447519 1:6958805.1 2:7015432.75 3:5833268.4 4:5251674.55
複製代碼
小結:該示例運行參數new LinearRegression().setMaxIter(100).setRegParam(0.8).setElasticNetParam(0.8)可調,有待進一步摸清。數據格式應知足libsvm要求,即標籤值 特徵序號:特徵值,RMSE:預測值與真實值的偏差平方根的均值 r2: 將預測值跟只使用均值的狀況下相比,看能好多少。其區間一般在(0,1)之間。0表示還不如什麼都不預測,直接取均值的狀況,而1表示全部預測跟真實結果完美匹配的狀況, 與均值相比的優秀程度,介於[0~1]。0表示不如均值。1表示完美預測.maven
寫本文加強本身學習的信心,終於輸出了預測結果,因此說學習之路並不順利,惟有持之以恆的堅持下去。ide