-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
163 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
spark-starter/src/main/java/com/wugui/sparkstarter/ml/MatrixExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.wugui.sparkstarter.ml | ||
|
||
import breeze.linalg.DenseMatrix | ||
import org.apache.spark.mllib.linalg.Matrices | ||
import org.apache.spark.mllib.stat.Statistics | ||
|
||
object MatrixExample { | ||
|
||
def main(args: Array[String]): Unit = { | ||
// 矩阵打竖排 | ||
val dm = Matrices.dense(3,2,Array(1,2,3,4,5,6)) | ||
println(dm) | ||
|
||
// 矩阵打横排 | ||
val d1 = DenseMatrix(Array(1,2),Array(3,4),Array(5,6)) | ||
println(d1) | ||
|
||
// 矩阵转置 | ||
println(d1.t) | ||
|
||
// 皮尔森卡方检验 | ||
val pValue = Statistics.chiSqTest(Matrices.dense(2,2,Array(127,19,147,10))) | ||
println(pValue) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
spark-starter/src/main/java/com/wugui/sparkstarter/ml/StatisticsExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.wugui.sparkstarter.ml | ||
|
||
import org.apache.spark.mllib.linalg.Vectors | ||
import org.apache.spark.mllib.stat.Statistics | ||
import org.apache.spark.{SparkConf, SparkContext} | ||
|
||
object StatisticsExample { | ||
def main(args: Array[String]): Unit = { | ||
val conf = new SparkConf().setMaster("local[2]").setAppName("StatisticsApp") | ||
val sc = new SparkContext(conf) | ||
val classpath = this.getClass.getResource("/").getPath | ||
|
||
val txt = sc.textFile(classpath + "北京降雨量.txt") | ||
// 将数据转成向量vector的RDD | ||
val data = txt.flatMap(line => line.split(",")).map { s => | ||
Vectors.dense(s.toDouble) | ||
} | ||
|
||
val summary = Statistics.colStats(data) | ||
// 每一列的平均值 | ||
println(summary.mean) | ||
// 方差 | ||
println(summary.variance) | ||
// 非0数量 | ||
println(summary.numNonzeros) | ||
// 最大值 | ||
println(summary.max) | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
spark-starter/src/main/java/com/wugui/sparkstarter/ml/VectorsExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.wugui.sparkstarter.ml | ||
import breeze.linalg.DenseVector | ||
import org.apache.spark.mllib.linalg.Vectors | ||
|
||
object VectorsExample { | ||
def main(args: Array[String]): Unit = { | ||
val dv = Vectors.dense(1,2,3) | ||
|
||
val denseVector1 = DenseVector(1,2,3) | ||
val denseVector2 = DenseVector(1,2,3) | ||
|
||
val sub =denseVector1 + denseVector2 | ||
println(sub) | ||
|
||
println() | ||
|
||
// 向量乘法 | ||
println(denseVector1 * denseVector2) | ||
println(denseVector1 * denseVector2.t) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.4806,0.4839,0.318,0.4107,0.4835,0.4445,0.3704,0.3389,0.3711,0.2669,0.7317,0.4309,0.7009,0.5725,0.8132,0.5067,0.5415,0.7479,0.6973,0.4422,0.6733,0.6839,0.6653,0.721,0.4888,0.4899,0.5444,0.3932,0.3807,0.7184,0.6648,0.779,0.684,0.3928,0.4747,0.6982,0.3742,0.5112,0.597,0.9132,0.3867,0.5934,0.5279,0.2618,0.8177,0.7756,0.3669,0.5998,0.5271,1.406,0.6919,0.4868,1.1157,0.9332,0.9614,0.6577,0.5573,0.4816,0.9109,0.921 |