More Related Content Similar to Why dont you_create_new_spark_jl Similar to Why dont you_create_new_spark_jl (20) More from Shintaro Fukushima More from Shintaro Fukushima (13) Why dont you_create_new_spark_jl1. Why don't you create
new Spark.jl?
2015年7月11日
JuliaTokyo#4
@sfchaos
1
2. 自己紹介
■ twitterID: @sfchaos
■ キーワード:
データマイニング,機械学習,オントロジー,
R,Python,Perl,C++,Julia
■ 今日はこの言葉を言うためにやって来ました.
“Why don't you create Spark.jl?”
2
5. Apache Sparkの概要
■ OSSのインメモリ処理の分散並列基盤.
■ RDDという分散コレクションに対して,
map, filter, reduceなどのデータ処理を繰り返して
目的のデータを得る処理モデル.
※ RDD: Resilient Distributed Dataset
■ 2009年,UC Berkeley(当時)のMatei Zaharia氏が
関数プログラミング言語Scalaを用いて開発.
5
17. Scalaのファイルを覗いてみる
${SPARK_HOME}/examples/src/main/scala/org/apache/spark/examples/mllib/
DecisionTreeRunner.scala
17
case class Params(
input: String = null,
testInput: String = "",
dataFormat: String = "libsvm",
algo: Algo = Classification,
maxDepth: Int = 5,
impurity: ImpurityType = Gini,
maxBins: Int = 32,
minInstancesPerNode: Int = 1,
minInfoGain: Double = 0.0,
numTrees: Int = 1,
featureSubsetStrategy: String = "auto",
fracTest: Double = 0.2,
useNodeIdCache: Boolean = false,
checkpointDir: Option[String] = None,
checkpointInterval: Int = 10) extends AbstractParams[Params]
def main(args: Array[String]) {
18. ランダムフォレストを用いたホールドアウト検定
■ 現状は,あえてSparkRを使う理由はない気がする
18
scala> import org.apache.spark.SparkContext
scala> import org.apache.spark.mllib.tree.RandomForest
scala> import org.apache.spark.mllib.util.MLUtils
scala> import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics
scala> import org.apache.spark.mllib.evaluation.MulticlassMetrics
scala> // データのロード
scala> val sc = new SparkContext("local", "RandomForest")
scala> val data = MLUtils.loadLibSVMFile(sc,
"data/mllib/sample_libsvm_data.txt")
scala> // 訓練データとテストデータへの分割
scala> val splits = data.randomSplit(Array(0.7, 0.3))
scala> val (trainingData, testData) = (splits(0), splits(1))
19. ランダムフォレストを用いたホールドアウト検定
■ 現状は,あえてSparkRを使う理由はないかも
19
scala> // ランダムフォレストを用いた予測モデルの構築
scala> val numClasses = 2 // クラス数
scala> val categoricalFeaturesInfo = Map[Int, Int]() // カテゴリ変数の
情報
scala> val numTrees = 500 // 構築する決定木の個数
scala> val featureSubsetStrategy = "auto" // 特徴量選択のアルゴリズム
scala> val impurity = "gini" // 不純度に用いる指標
scala> val maxDepth = 5 // 木の最大の深さ
scala> val maxBins = 32
scala> val model = RandomForest.trainClassifier(trainingData,
numClasses, categoricalFeaturesInfo, numTrees,
featureSubsetStrategy, impurity, maxDepth, maxBins)
20. ランダムフォレストを用いたホールドアウト検定
■ 現状は,あえてSparkRを使う必要はないかも.
20
scala> // テストデータに対する予測モデルの評価
val labelAndPreds = testData.map { point =>
val prediction = model.predict(point.features)
(point.label, prediction)
}
scala> // 混合行列
scala> val metrics = new MulticlassMetrics(labelAndPreds)
scala> metrics.confusionMatrix
res20: org.apache.spark.mllib.linalg.Matrix =
13.0 1.0
0.0 17.0
scala> metrics.precision
res21: Double = 0.9285714
正例と予測
実績が正例
適合率
(precision)
28. データの入出力・変換
■ jsonファイル
■ read.df関数で読み込みHiveSQL上のテーブルに
変換
■ SQLによりデータ抽出可能
28
> people <- read.df(sqlContext, “./examples/src/main/resources/
> people.json", "json")
> head(people)
## age name
##1 NA Michael
##2 30 Andy
##3 19 Justin
> printSchema(people)
# root
# |-- age: integer (nullable = true)
# |-- name: string (nullable = true)
29. データの入出力・変換
■ Hiveとの連携
■ HDFSに蓄積されたデータの抽出
29
> hiveContext <- sparkRHive.init(sc)
> sql(hiveContext, "CREATE TABLE IF NOT EXISTS src (key INT, value
STRING)")
> sql(hiveContext, "LOAD DATA LOCAL INPATH ‘examples/src/main/
resources/kv1.txt' INTO TABLE src")
> # クエリはHiveQLで表現可能
> results <- hiveContext.sql("FROM src SELECT key, value")
> # 返り値はRのデータフレームで返される
> head(results)
## key value
## 1 238 val_238
## 2 86 val_86
## 3 311 val_311
30. DataFrameオブジェクトの作成
■ createDataFrame関数によりRのデータフレームから作成
30
> iris.df <- createDataFrame(sqlContext, iris)
> iris.df
DataFrame[Sepal_Length:double, Sepal_Width:double,
Petal_Length:double, Petal_Width:double, Species:string]
> class(iris.df)
[1] "DataFrame"
attr(,"package")
[1] “SparkR"
> head(iris.df, 3)
Sepal_Length Sepal_Width Petal_Length Petal_Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
31. DataFrameオブジェクトの操作
■ 行の抽出(filter関数)
31
> library(magrittr)
> head(filter(iris.df, iris.df$Sepal_Length >= 5.0), 3)
Sepal_Length Sepal_Width Petal_Length Petal_Width Species
1 5.1 3.5 1.4 0.2 setosa
2 5.0 3.6 1.4 0.2 setosa
3 5.4 3.9 1.7 0.4 setosa
> # パイプ処理も可能
> iris.df %>% filter(iris.df$Sepal_Length >= 5.0) %>% head(3)
> # NSE(Non Standard Evaluation)は使えない
> iris.df %>% filter(Sepal_Length >= 5.0) %>% head(3)
以下にエラー filter(., Sepal_Length >= 5) :
引数 'condition' の評価中にエラーが起きました (関数 'filter' に対するメソッ
ドの選択時): エラー: オブジェクト 'Sepal_Length' がありません
magrittrパッケージを使用
(dplyrを読み込むと名前の衝突が起きて,
面倒なことに・・・)
32. データフレームの操作
■ 列の抽出
32
> head(select(iris.df, iris.df$Species), 3)
Species
1 setosa
2 setosa
3 setosa
> # パイプ処理も可能
> iris.df %>% select(iris.df$Species) %>% head(3)
> # NSE(Non Standard Evaluation)は使えない
> iris.df %>% select(Species) %>% head(3)
以下にエラー select(., Species) :
引数 'col' の評価中にエラーが起きました (関数 'select' に対するメソッドの選
択時): エラー: オブジェクト 'Species' がありません
38. PySparkの起動
■ PySparkの起動
38
$ cd $SPARK_HOME/bin
$ ./pyspark
Python 2.7.9 (default, Feb 10 2015, 03:28:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Using Spark's default log4j profile: org/apache/spark/log4j-
defaults.properties
15/07/11 13:35:56 INFO SparkContext: Running Spark version 1.4.0
(中略)
Welcome to
____ __
/ __/__ ___ _____/ /__
_ / _ / _ `/ __/ '_/
/__ / .__/_,_/_/ /_/_ version 1.4.0
/_/
Using Python version 2.7.9 (default, Feb 10 2015 03:28:08)
SparkContext available as sc, HiveContext available as sqlContext.
>>>
39. MLlibの使用
■ランダムフォレストの実行
39
>>> from pyspark.mllib.tree import RandomForest, RandomForestModel
>>> from pyspark.mllib.util import MLUtils
>>> # データファイルをロードしパースしてLabeledPointのRDDに変換
>>> data = MLUtils.loadLibSVMFile(sc,
... ’../data/mllib/sample_libsvm_data.txt’)
>>> # データを訓練用とテスト用に分割 (テスト用は30%)
>>> (trainingData, testData) = data.randomSplit([0.7, 0.3], seed=123)
>>> # ランダムフォレストを用いた予測モデル構築
>>> model = RandomForest.trainClassifier(
... trainingData, numClasses=2,
... categoricalFeaturesInfo={}, numTrees=3,
... featureSubsetStrategy=‘auto’, impurity='gini',
... maxDepth=4, maxBins=32)
>>> # テストデータに対してモデルを評価し,誤差を算出
>>> predictions = model.predict(testData.map(lambda x: x.features))
>>> labelsAndPredictions = testData.map(
... lambda lp: lp.label).zip(predictions)
>>> testErr = labelsAndPredictions.filter(
... lambda (v, p): v != p).count() / float(testData.count())
>>> print('Test Error = ' + str(testErr))
>>> print('Learned classification forest model:')
>>> print(model.toDebugString())
自然な記述が可能
44. JuliaとSparkの連携
■ とりあえずサンプルコードの実行
44
$ julia
julia> using Spark
julia> # マスターサーバ
julia> master = Spark.Master("127.0.0.1", 3333)
julia> # ワーカーサーバの起動
julia> # Spark.load(master, “default_workers.json")
julia> # マスターサーバーの初期化
julia> Spark.initserver(master)
julia> # RDDの生成と操作
julia> # 整数が並んだデータを読みこむ
julia> rdd = Spark.input(master, "RDDA.txt", "int_reader")
julia> filtered_rdd = Spark.filter(master, rdd, "number_filter")
julia> results = Spark.collect(master, filtered_red)
0-element Array{Any,1}
1
2
…
10
!!!原因究明中
45. JuliaとSparkの連携
■ 他にも Spock.jl や Sparta.jl などがある.
45
Spock.jl
Sparta.jl
https://groups.google.com/forum/m/#!msg/julia-dev/-Ft7jaHLAec/ZfLo9uTAP1cJ
48. Spock.jl
■ インストールとサンプルプログラムの実行
48
$ julia
julia> Pkg.clone(“https://github.com/jey/Spock.jl.git")
julia> quit()
$ cd $HOME/.julia/v0.3/Spock/
$ ln -s /usr/local/share/spark/lib/spark-assembly-1.4.0-
hadoop2.6.0.jar lib/spark.jar
$ make
mkdir -p build
javac -Xlint -d build -cp lib/spark.jar: src//JuliaFunction.java
src//JuliaObject.java
jar cf lib/spock.jar -C build .
julia test/runtests.jl 2> stderr.log
Loaded /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/
Contents/Home/jre/lib/server/libjvm.dylib
49. Spock.jl
■ サンプルプログラムの実行
49
julia> using Spock
julia> using Base.Test
julia> sc = SparkContext()
julia> rdd1 = parallelize(sc, 1:10, 2)
julia> collect(rdd1)
10-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
10
51. Sparta.jl
■ これも多分,RPCでJVMと連携.
51
using JavaCall
JList = @jimport java.util.List
JArrays = @jimport java.util.Arrays
JJuliaRDD = @jimport org.apache.spark.api.julia.JuliaRDD
JJavaRDD = @jimport org.apache.spark.api.java.JavaRDD
JJavaRDD_ = @jimport "org.apache.spark.api.java.JavaRDD$"
JRDD = @jimport org.apache.spark.rdd.RDD
JJavaSparkContext = @jimport
org.apache.spark.api.java.JavaSparkContext
54. Sparta.jl
■ パッケージの読み込み
54
julia> using Sparta
Loaded /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/
Contents/Home/jre/lib/server/libjvm.dylib
signal (11): Segmentation fault: 11
unknown function (ip: 362844852)
julia> using Sparta せまりくるセグフォの恐怖
57. まとめ
■ Spock.jl や Sparta.jl などのパッケージ開発中.
■ セグフォ問題: Julia-Spark連携に立ちふさがる壁
■ そこで,
57
“Why don't you create new Spark.jl?”