ruby-spark 1.1.0.1-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +37 -0
- data/Gemfile +47 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +252 -0
- data/Rakefile +35 -0
- data/TODO.md +6 -0
- data/benchmark/aggregate.rb +33 -0
- data/benchmark/bisect.rb +88 -0
- data/benchmark/comparison/prepare.sh +18 -0
- data/benchmark/comparison/python.py +156 -0
- data/benchmark/comparison/r.r +69 -0
- data/benchmark/comparison/ruby.rb +167 -0
- data/benchmark/comparison/run-all.sh +160 -0
- data/benchmark/comparison/scala.scala +181 -0
- data/benchmark/custom_marshal.rb +94 -0
- data/benchmark/digest.rb +150 -0
- data/benchmark/enumerator.rb +88 -0
- data/benchmark/serializer.rb +82 -0
- data/benchmark/sort.rb +43 -0
- data/benchmark/sort2.rb +164 -0
- data/benchmark/take.rb +28 -0
- data/bin/ruby-spark +8 -0
- data/example/pi.rb +28 -0
- data/example/website_search.rb +83 -0
- data/ext/ruby_c/extconf.rb +3 -0
- data/ext/ruby_c/murmur.c +158 -0
- data/ext/ruby_c/murmur.h +9 -0
- data/ext/ruby_c/ruby-spark.c +18 -0
- data/ext/ruby_java/Digest.java +36 -0
- data/ext/ruby_java/Murmur2.java +98 -0
- data/ext/ruby_java/RubySparkExtService.java +28 -0
- data/ext/ruby_java/extconf.rb +3 -0
- data/ext/spark/build.sbt +73 -0
- data/ext/spark/project/plugins.sbt +9 -0
- data/ext/spark/sbt/sbt +34 -0
- data/ext/spark/src/main/scala/Exec.scala +91 -0
- data/ext/spark/src/main/scala/MLLibAPI.scala +4 -0
- data/ext/spark/src/main/scala/Marshal.scala +52 -0
- data/ext/spark/src/main/scala/MarshalDump.scala +113 -0
- data/ext/spark/src/main/scala/MarshalLoad.scala +220 -0
- data/ext/spark/src/main/scala/RubyAccumulatorParam.scala +69 -0
- data/ext/spark/src/main/scala/RubyBroadcast.scala +13 -0
- data/ext/spark/src/main/scala/RubyConstant.scala +13 -0
- data/ext/spark/src/main/scala/RubyMLLibAPI.scala +55 -0
- data/ext/spark/src/main/scala/RubyMLLibUtilAPI.scala +21 -0
- data/ext/spark/src/main/scala/RubyPage.scala +34 -0
- data/ext/spark/src/main/scala/RubyRDD.scala +392 -0
- data/ext/spark/src/main/scala/RubySerializer.scala +14 -0
- data/ext/spark/src/main/scala/RubyTab.scala +11 -0
- data/ext/spark/src/main/scala/RubyUtils.scala +15 -0
- data/ext/spark/src/main/scala/RubyWorker.scala +257 -0
- data/ext/spark/src/test/scala/MarshalSpec.scala +84 -0
- data/lib/ruby-spark.rb +1 -0
- data/lib/spark.rb +198 -0
- data/lib/spark/accumulator.rb +260 -0
- data/lib/spark/broadcast.rb +98 -0
- data/lib/spark/build.rb +43 -0
- data/lib/spark/cli.rb +169 -0
- data/lib/spark/command.rb +86 -0
- data/lib/spark/command/base.rb +158 -0
- data/lib/spark/command/basic.rb +345 -0
- data/lib/spark/command/pair.rb +124 -0
- data/lib/spark/command/sort.rb +51 -0
- data/lib/spark/command/statistic.rb +144 -0
- data/lib/spark/command_builder.rb +141 -0
- data/lib/spark/command_validator.rb +34 -0
- data/lib/spark/config.rb +238 -0
- data/lib/spark/constant.rb +14 -0
- data/lib/spark/context.rb +322 -0
- data/lib/spark/error.rb +50 -0
- data/lib/spark/ext/hash.rb +41 -0
- data/lib/spark/ext/integer.rb +25 -0
- data/lib/spark/ext/io.rb +67 -0
- data/lib/spark/ext/ip_socket.rb +29 -0
- data/lib/spark/ext/module.rb +58 -0
- data/lib/spark/ext/object.rb +24 -0
- data/lib/spark/ext/string.rb +24 -0
- data/lib/spark/helper.rb +10 -0
- data/lib/spark/helper/logger.rb +40 -0
- data/lib/spark/helper/parser.rb +85 -0
- data/lib/spark/helper/serialize.rb +71 -0
- data/lib/spark/helper/statistic.rb +93 -0
- data/lib/spark/helper/system.rb +42 -0
- data/lib/spark/java_bridge.rb +19 -0
- data/lib/spark/java_bridge/base.rb +203 -0
- data/lib/spark/java_bridge/jruby.rb +23 -0
- data/lib/spark/java_bridge/rjb.rb +41 -0
- data/lib/spark/logger.rb +76 -0
- data/lib/spark/mllib.rb +100 -0
- data/lib/spark/mllib/classification/common.rb +31 -0
- data/lib/spark/mllib/classification/logistic_regression.rb +223 -0
- data/lib/spark/mllib/classification/naive_bayes.rb +97 -0
- data/lib/spark/mllib/classification/svm.rb +135 -0
- data/lib/spark/mllib/clustering/gaussian_mixture.rb +82 -0
- data/lib/spark/mllib/clustering/kmeans.rb +118 -0
- data/lib/spark/mllib/matrix.rb +120 -0
- data/lib/spark/mllib/regression/common.rb +73 -0
- data/lib/spark/mllib/regression/labeled_point.rb +41 -0
- data/lib/spark/mllib/regression/lasso.rb +100 -0
- data/lib/spark/mllib/regression/linear.rb +124 -0
- data/lib/spark/mllib/regression/ridge.rb +97 -0
- data/lib/spark/mllib/ruby_matrix/matrix_adapter.rb +53 -0
- data/lib/spark/mllib/ruby_matrix/vector_adapter.rb +57 -0
- data/lib/spark/mllib/stat/distribution.rb +12 -0
- data/lib/spark/mllib/vector.rb +185 -0
- data/lib/spark/rdd.rb +1377 -0
- data/lib/spark/sampler.rb +92 -0
- data/lib/spark/serializer.rb +79 -0
- data/lib/spark/serializer/auto_batched.rb +59 -0
- data/lib/spark/serializer/base.rb +63 -0
- data/lib/spark/serializer/batched.rb +84 -0
- data/lib/spark/serializer/cartesian.rb +13 -0
- data/lib/spark/serializer/compressed.rb +27 -0
- data/lib/spark/serializer/marshal.rb +17 -0
- data/lib/spark/serializer/message_pack.rb +23 -0
- data/lib/spark/serializer/oj.rb +23 -0
- data/lib/spark/serializer/pair.rb +41 -0
- data/lib/spark/serializer/text.rb +25 -0
- data/lib/spark/sort.rb +189 -0
- data/lib/spark/stat_counter.rb +125 -0
- data/lib/spark/storage_level.rb +39 -0
- data/lib/spark/version.rb +3 -0
- data/lib/spark/worker/master.rb +144 -0
- data/lib/spark/worker/spark_files.rb +15 -0
- data/lib/spark/worker/worker.rb +200 -0
- data/ruby-spark.gemspec +47 -0
- data/spec/generator.rb +37 -0
- data/spec/inputs/lorem_300.txt +316 -0
- data/spec/inputs/numbers/1.txt +50 -0
- data/spec/inputs/numbers/10.txt +50 -0
- data/spec/inputs/numbers/11.txt +50 -0
- data/spec/inputs/numbers/12.txt +50 -0
- data/spec/inputs/numbers/13.txt +50 -0
- data/spec/inputs/numbers/14.txt +50 -0
- data/spec/inputs/numbers/15.txt +50 -0
- data/spec/inputs/numbers/16.txt +50 -0
- data/spec/inputs/numbers/17.txt +50 -0
- data/spec/inputs/numbers/18.txt +50 -0
- data/spec/inputs/numbers/19.txt +50 -0
- data/spec/inputs/numbers/2.txt +50 -0
- data/spec/inputs/numbers/20.txt +50 -0
- data/spec/inputs/numbers/3.txt +50 -0
- data/spec/inputs/numbers/4.txt +50 -0
- data/spec/inputs/numbers/5.txt +50 -0
- data/spec/inputs/numbers/6.txt +50 -0
- data/spec/inputs/numbers/7.txt +50 -0
- data/spec/inputs/numbers/8.txt +50 -0
- data/spec/inputs/numbers/9.txt +50 -0
- data/spec/inputs/numbers_0_100.txt +101 -0
- data/spec/inputs/numbers_1_100.txt +100 -0
- data/spec/lib/collect_spec.rb +42 -0
- data/spec/lib/command_spec.rb +68 -0
- data/spec/lib/config_spec.rb +64 -0
- data/spec/lib/context_spec.rb +165 -0
- data/spec/lib/ext_spec.rb +72 -0
- data/spec/lib/external_apps_spec.rb +45 -0
- data/spec/lib/filter_spec.rb +80 -0
- data/spec/lib/flat_map_spec.rb +100 -0
- data/spec/lib/group_spec.rb +109 -0
- data/spec/lib/helper_spec.rb +19 -0
- data/spec/lib/key_spec.rb +41 -0
- data/spec/lib/manipulation_spec.rb +122 -0
- data/spec/lib/map_partitions_spec.rb +87 -0
- data/spec/lib/map_spec.rb +91 -0
- data/spec/lib/mllib/classification_spec.rb +54 -0
- data/spec/lib/mllib/clustering_spec.rb +35 -0
- data/spec/lib/mllib/matrix_spec.rb +32 -0
- data/spec/lib/mllib/regression_spec.rb +116 -0
- data/spec/lib/mllib/vector_spec.rb +77 -0
- data/spec/lib/reduce_by_key_spec.rb +118 -0
- data/spec/lib/reduce_spec.rb +131 -0
- data/spec/lib/sample_spec.rb +46 -0
- data/spec/lib/serializer_spec.rb +88 -0
- data/spec/lib/sort_spec.rb +58 -0
- data/spec/lib/statistic_spec.rb +170 -0
- data/spec/lib/whole_text_files_spec.rb +33 -0
- data/spec/spec_helper.rb +38 -0
- metadata +389 -0
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
def func3(x)
|
4
|
+
x.map(&:to_i).reduce(:+)
|
5
|
+
end
|
6
|
+
|
7
|
+
def func4_with_index(data, index)
|
8
|
+
[{
|
9
|
+
index => data.map(&:to_i).reduce(:*)
|
10
|
+
}]
|
11
|
+
end
|
12
|
+
|
13
|
+
RSpec.shared_examples 'a map partitions' do |workers|
|
14
|
+
context "with #{workers || 'default'} worker" do
|
15
|
+
it 'without index' do
|
16
|
+
rdd2 = rdd(workers).map_partitions(func1)
|
17
|
+
result = func1.call(numbers)
|
18
|
+
|
19
|
+
expect(func1.call(rdd2.collect)).to eql(result)
|
20
|
+
|
21
|
+
rdd3 = rdd(workers)
|
22
|
+
rdd3 = rdd3.map_partitions(func1)
|
23
|
+
rdd3 = rdd3.map_partitions(func2)
|
24
|
+
rdd3 = rdd3.map_partitions(method(:func3))
|
25
|
+
result = func3(func2.call(func1.call(numbers)))
|
26
|
+
|
27
|
+
# Not same number of workers
|
28
|
+
expect(rdd3.collect.size).to be >= 1
|
29
|
+
|
30
|
+
rdd4 = rdd(workers)
|
31
|
+
rdd4 = rdd4.map_partitions(func1)
|
32
|
+
rdd4 = rdd4.map_partitions(func2)
|
33
|
+
rdd4 = rdd4.map_partitions(method(:func3))
|
34
|
+
|
35
|
+
expect(rdd4.collect).to eql(rdd3.collect)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'with index' do
|
39
|
+
rdd2 = rdd(workers).map_partitions_with_index(method(:func4_with_index))
|
40
|
+
result = rdd2.collect
|
41
|
+
|
42
|
+
expect(result).to be_a(Array)
|
43
|
+
|
44
|
+
result.each do |x|
|
45
|
+
expect(x).to be_a(Hash)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Multiply by 0
|
49
|
+
# Some values are 0 because of batched serialization
|
50
|
+
expect(result.map(&:values).flatten.compact.uniq.first).to eql(0)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
RSpec::describe 'Spark::RDD.map_partitions(_with_index)' do
|
56
|
+
let(:func1) { lambda{|x| x.map(&:to_i)} }
|
57
|
+
let(:func2) {
|
58
|
+
lambda{|x|
|
59
|
+
x.map{|y| y*2}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
context 'throught parallelize' do
|
64
|
+
let(:numbers) { 0..1000 }
|
65
|
+
|
66
|
+
def rdd(workers)
|
67
|
+
$sc.parallelize(numbers, workers)
|
68
|
+
end
|
69
|
+
|
70
|
+
it_behaves_like 'a map partitions', nil
|
71
|
+
it_behaves_like 'a map partitions', 1
|
72
|
+
it_behaves_like 'a map partitions', rand(2..10)
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'throught text_file' do
|
76
|
+
let(:file) { File.join('spec', 'inputs', 'numbers_0_100.txt') }
|
77
|
+
let(:numbers) { File.readlines(file).map(&:strip) }
|
78
|
+
|
79
|
+
def rdd(workers)
|
80
|
+
$sc.text_file(file, workers)
|
81
|
+
end
|
82
|
+
|
83
|
+
it_behaves_like 'a map partitions', nil
|
84
|
+
it_behaves_like 'a map partitions', 1
|
85
|
+
it_behaves_like 'a map partitions', rand(2..10)
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec::shared_examples "a mapping" do |workers|
|
4
|
+
it "with #{workers || 'default'} worker" do
|
5
|
+
rdd2 = rdd(workers).map(func1)
|
6
|
+
result = numbers.map(&func1)
|
7
|
+
|
8
|
+
expect(rdd2.collect).to eql(result)
|
9
|
+
|
10
|
+
rdd3 = rdd(workers)
|
11
|
+
rdd3 = rdd3.map(func1)
|
12
|
+
rdd3 = rdd3.map(func2)
|
13
|
+
rdd3 = rdd3.map(func3)
|
14
|
+
result = numbers.map(&func1).map(&func2).map(&func3)
|
15
|
+
|
16
|
+
expect(rdd3.collect).to eql(result)
|
17
|
+
|
18
|
+
rdd4 = rdd(workers)
|
19
|
+
rdd4 = rdd4.map(func3)
|
20
|
+
rdd4 = rdd4.map(func2)
|
21
|
+
rdd4 = rdd4.map(func1)
|
22
|
+
|
23
|
+
expect(rdd4.collect).to eql(rdd3.collect)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
RSpec::shared_examples "a mapping values" do |workers|
|
28
|
+
it "with #{workers || 'default'} worker" do
|
29
|
+
rdd2 = rdd(workers).map_values(func1)
|
30
|
+
result = hash.map{|key, value| [key, func1.call(value)]}
|
31
|
+
|
32
|
+
expect(rdd2.collect).to eql(result)
|
33
|
+
|
34
|
+
rdd3 = rdd(workers)
|
35
|
+
rdd3 = rdd3.map_values(func1)
|
36
|
+
rdd3 = rdd3.map_values(func2)
|
37
|
+
rdd3 = rdd3.map_values(func3)
|
38
|
+
result = hash.map{|key, value| [key, func1.call(value)]}
|
39
|
+
.map{|key, value| [key, func2.call(value)]}
|
40
|
+
.map{|key, value| [key, func3.call(value)]}
|
41
|
+
|
42
|
+
expect(rdd3.collect).to eql(result)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
RSpec::describe "Spark::RDD" do
|
47
|
+
let(:func1) { lambda{|x| x*2} }
|
48
|
+
let(:func2) { lambda{|x| x*3} }
|
49
|
+
let(:func3) { lambda{|x| x*4} }
|
50
|
+
|
51
|
+
context "throught parallelize" do
|
52
|
+
context ".map" do
|
53
|
+
let(:numbers) { Generator.numbers }
|
54
|
+
|
55
|
+
def rdd(workers)
|
56
|
+
$sc.parallelize(numbers, workers)
|
57
|
+
end
|
58
|
+
|
59
|
+
it_behaves_like "a mapping", nil
|
60
|
+
it_behaves_like "a mapping", 1
|
61
|
+
it_behaves_like "a mapping", rand(2..10)
|
62
|
+
end
|
63
|
+
|
64
|
+
context ".map_values" do
|
65
|
+
let!(:hash) { Generator.hash }
|
66
|
+
|
67
|
+
def rdd(workers)
|
68
|
+
$sc.parallelize(hash, workers)
|
69
|
+
end
|
70
|
+
|
71
|
+
it_behaves_like "a mapping values", nil
|
72
|
+
it_behaves_like "a mapping values", 1
|
73
|
+
it_behaves_like "a mapping values", rand(2..10)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "throught text_file" do
|
78
|
+
context ".map" do
|
79
|
+
let(:file) { File.join("spec", "inputs", "numbers_0_100.txt") }
|
80
|
+
let(:numbers) { File.readlines(file).map(&:strip) }
|
81
|
+
|
82
|
+
def rdd(workers)
|
83
|
+
$sc.text_file(file, workers)
|
84
|
+
end
|
85
|
+
|
86
|
+
it_behaves_like "a mapping", nil
|
87
|
+
it_behaves_like "a mapping", 1
|
88
|
+
it_behaves_like "a mapping", rand(2..10)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'Spark::Mllib classification' do
|
4
|
+
|
5
|
+
let(:data1) do
|
6
|
+
[
|
7
|
+
LabeledPoint.new(0.0, [1, 0, 0]),
|
8
|
+
LabeledPoint.new(1.0, [0, 1, 1]),
|
9
|
+
LabeledPoint.new(0.0, [2, 0, 0]),
|
10
|
+
LabeledPoint.new(1.0, [0, 2, 1])
|
11
|
+
]
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:values1) do
|
15
|
+
data1.map do |lp|
|
16
|
+
lp.features.values
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:rdd1) { $sc.parallelize(data1) }
|
21
|
+
|
22
|
+
context 'logistic regression' do
|
23
|
+
it 'test' do
|
24
|
+
lrm = LogisticRegressionWithSGD.train(rdd1)
|
25
|
+
|
26
|
+
expect(lrm.predict(values1[0])).to be <= 0
|
27
|
+
expect(lrm.predict(values1[1])).to be > 0
|
28
|
+
expect(lrm.predict(values1[2])).to be <= 0
|
29
|
+
expect(lrm.predict(values1[3])).to be > 0
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'svm' do
|
34
|
+
it 'test' do
|
35
|
+
lrm = SVMWithSGD.train(rdd1)
|
36
|
+
|
37
|
+
expect(lrm.predict(values1[0])).to be <= 0
|
38
|
+
expect(lrm.predict(values1[1])).to be > 0
|
39
|
+
expect(lrm.predict(values1[2])).to be <= 0
|
40
|
+
expect(lrm.predict(values1[3])).to be > 0
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'naive bayes' do
|
45
|
+
it 'test' do
|
46
|
+
lrm = NaiveBayes.train(rdd1)
|
47
|
+
|
48
|
+
expect(lrm.predict(values1[0])).to be <= 0
|
49
|
+
expect(lrm.predict(values1[1])).to be > 0
|
50
|
+
expect(lrm.predict(values1[2])).to be <= 0
|
51
|
+
expect(lrm.predict(values1[3])).to be > 0
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'Spark::Mllib clustering' do
|
4
|
+
context 'kmeans' do
|
5
|
+
it 'test' do
|
6
|
+
data = [
|
7
|
+
DenseVector.new([0, 1.1]),
|
8
|
+
DenseVector.new([0, 1.2]),
|
9
|
+
DenseVector.new([1.1, 0]),
|
10
|
+
DenseVector.new([1.2, 0])
|
11
|
+
]
|
12
|
+
model = KMeans.train($sc.parallelize(data), 2, initialization_mode: 'k-means||')
|
13
|
+
|
14
|
+
expect(model.predict(data[0])).to eq(model.predict(data[1]))
|
15
|
+
expect(model.predict(data[2])).to eq(model.predict(data[3]))
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'deterministic' do
|
19
|
+
data = Array.new(10) do |i|
|
20
|
+
i *= 10
|
21
|
+
DenseVector.new([i, i])
|
22
|
+
end
|
23
|
+
|
24
|
+
clusters1 = KMeans.train($sc.parallelize(data), 3, initialization_mode: 'k-means||', seed: 42)
|
25
|
+
clusters2 = KMeans.train($sc.parallelize(data), 3, initialization_mode: 'k-means||', seed: 42)
|
26
|
+
|
27
|
+
centers1 = clusters1.centers.to_a
|
28
|
+
centers2 = clusters2.centers.to_a
|
29
|
+
|
30
|
+
centers1.zip(centers2).each do |c1, c2|
|
31
|
+
expect(c1).to eq(c2)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'Spark::Mllib::Matrix' do
|
4
|
+
context 'dense' do
|
5
|
+
it 'construct' do
|
6
|
+
values = [[1.0, 0.0, 4.0], [0.0, 3.0, 5.0], [2.0, 0.0, 6.0]]
|
7
|
+
matrix = DenseMatrix.new(3, 3, [[1.0, 0.0, 4.0], [0.0, 3.0, 5.0], [2.0, 0.0, 6.0]])
|
8
|
+
|
9
|
+
expect(matrix.shape).to eq([3, 3])
|
10
|
+
expect(matrix.values).to eq([[1.0, 0.0, 4.0], [0.0, 3.0, 5.0], [2.0, 0.0, 6.0]])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'sparse' do
|
15
|
+
it 'construct' do
|
16
|
+
values = [1.0, 2.0, 4.0, 5.0]
|
17
|
+
column_pointers = [0, 2, 2, 4, 4]
|
18
|
+
row_indices = [1, 2, 1, 2]
|
19
|
+
|
20
|
+
matrix = SparseMatrix.new(3, 4, column_pointers, row_indices, values)
|
21
|
+
|
22
|
+
expect(matrix.shape).to eq([3, 4])
|
23
|
+
expect(matrix.to_a).to eq(
|
24
|
+
[
|
25
|
+
[0.0, 0.0, 0.0, 0.0],
|
26
|
+
[1.0, 0.0, 4.0, 0.0],
|
27
|
+
[2.0, 0.0, 5.0, 0.0]
|
28
|
+
]
|
29
|
+
)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# Mllib functions are tested on Spark
|
4
|
+
# This just test if ruby call proper methods
|
5
|
+
|
6
|
+
RSpec.describe 'Spark::Mllib regression' do
|
7
|
+
|
8
|
+
let(:data1) do
|
9
|
+
[
|
10
|
+
LabeledPoint.new(-1.0, [0, -1]),
|
11
|
+
LabeledPoint.new(1.0, [0, 1]),
|
12
|
+
LabeledPoint.new(-1.0, [0, -2]),
|
13
|
+
LabeledPoint.new(1.0, [0, 2])
|
14
|
+
]
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:values1) do
|
18
|
+
data1.map do |lp|
|
19
|
+
lp.features.values
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:rdd1) { $sc.parallelize(data1) }
|
24
|
+
|
25
|
+
context 'labeled point' do
|
26
|
+
let(:lp) { LabeledPoint.new(1, [1,2,3]) }
|
27
|
+
|
28
|
+
it 'from array' do
|
29
|
+
expect(lp.label).to eql(1.0)
|
30
|
+
expect(lp.features).to be_a(DenseVector)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'serialize' do
|
34
|
+
lp2 = Marshal.load(Marshal.dump(lp))
|
35
|
+
|
36
|
+
expect(lp2.label).to eql(lp.label)
|
37
|
+
expect(lp2.features.values).to eql(lp.features.values)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'linear regression' do
|
42
|
+
context 'test' do
|
43
|
+
let(:lrm) { LinearRegressionWithSGD.train(rdd1) }
|
44
|
+
|
45
|
+
it 'test' do
|
46
|
+
expect(lrm.predict(values1[0])).to be <= 0
|
47
|
+
expect(lrm.predict(values1[1])).to be > 0
|
48
|
+
expect(lrm.predict(values1[2])).to be <= 0
|
49
|
+
expect(lrm.predict(values1[3])).to be > 0
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'test via rdd' do
|
53
|
+
rdd = $sc.parallelize(values1, 1)
|
54
|
+
rdd = rdd.map(lambda{|value| model.predict(value)})
|
55
|
+
rdd = rdd.bind(model: lrm)
|
56
|
+
|
57
|
+
result = rdd.collect
|
58
|
+
|
59
|
+
expect(result[0]).to be <= 0
|
60
|
+
expect(result[1]).to be > 0
|
61
|
+
expect(result[2]).to be <= 0
|
62
|
+
expect(result[3]).to be > 0
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Y = 3 + 10*X1 + 10*X2
|
67
|
+
it 'linear regression' do
|
68
|
+
data = Spark.jb.call(RubyMLLibUtilAPI, 'generateLinearInput', 3.0, ['10.0', '10.0'], 100, 42, 0.1)
|
69
|
+
rdd = $sc.parallelize(data)
|
70
|
+
|
71
|
+
lrm = LinearRegressionWithSGD.train(rdd, iterations: 1000, intercept: true, step: 1.0)
|
72
|
+
|
73
|
+
expect(lrm.intercept).to be_between(2.5, 3.5)
|
74
|
+
expect(lrm.weights.size).to eq(2)
|
75
|
+
expect(lrm.weights[0]).to be_between(9.0, 11.0)
|
76
|
+
expect(lrm.weights[1]).to be_between(9.0, 11.0)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'lasso' do
|
81
|
+
it 'test' do
|
82
|
+
lrm = LassoWithSGD.train(rdd1)
|
83
|
+
|
84
|
+
expect(lrm.predict(values1[0])).to be <= 0
|
85
|
+
expect(lrm.predict(values1[1])).to be > 0
|
86
|
+
expect(lrm.predict(values1[2])).to be <= 0
|
87
|
+
expect(lrm.predict(values1[3])).to be > 0
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'local random SGD with initial weights' do
|
91
|
+
data = Spark.jb.call(RubyMLLibUtilAPI, 'generateLinearInput', 2.0, ['-1.5', '0.01'], 1000, 42, 0.1)
|
92
|
+
data.map! do |lp|
|
93
|
+
LabeledPoint.new(lp.label, [1.0] + lp.features.values)
|
94
|
+
end
|
95
|
+
|
96
|
+
rdd = $sc.parallelize(data);
|
97
|
+
|
98
|
+
lrm = LassoWithSGD.train(rdd, step: 1.0, reg_param: 0.01, iterations: 40, initial_weights: [-1.0, -1.0, -1.0])
|
99
|
+
|
100
|
+
expect(lrm.weights[0]).to be_between(1.9, 2.1)
|
101
|
+
expect(lrm.weights[1]).to be_between(-1.60, -1.40)
|
102
|
+
expect(lrm.weights[2]).to be_between(-1.0e-3, 1.0e-3)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'ridge' do
|
107
|
+
it 'test' do
|
108
|
+
lrm = RidgeRegressionWithSGD.train(rdd1)
|
109
|
+
|
110
|
+
expect(lrm.predict(values1[0])).to be <= 0
|
111
|
+
expect(lrm.predict(values1[1])).to be > 0
|
112
|
+
expect(lrm.predict(values1[2])).to be <= 0
|
113
|
+
expect(lrm.predict(values1[3])).to be > 0
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'Spark::Mllib::Vector' do
|
4
|
+
|
5
|
+
context 'parsing' do
|
6
|
+
it 'dense vector' do
|
7
|
+
dv = DenseVector.new([1.0, 2.0, 3.0, 4.0, 5.0])
|
8
|
+
dv2 = DenseVector.parse(dv.to_s)
|
9
|
+
dv3 = Vectors.parse(dv.to_s)
|
10
|
+
|
11
|
+
expect(dv.to_s).to eq("[1.0,2.0,3.0,4.0,5.0]")
|
12
|
+
expect(dv2.values).to eq(dv.values)
|
13
|
+
expect(dv3.values).to eq(dv.values)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'sparse vector' do
|
17
|
+
sv = SparseVector.new(5, {1 => 3, 4 => 5})
|
18
|
+
sv2 = SparseVector.parse(sv.to_s)
|
19
|
+
sv3 = Vectors.parse(sv.to_s)
|
20
|
+
|
21
|
+
expect(sv.to_s).to eq("(5,[1,4],[3,5])")
|
22
|
+
expect(sv2.size).to eq(sv.size)
|
23
|
+
expect(sv2.indices).to eq(sv.indices)
|
24
|
+
expect(sv2.values).to eq(sv.values)
|
25
|
+
expect(sv3.size).to eq(sv.size)
|
26
|
+
expect(sv3.indices).to eq(sv.indices)
|
27
|
+
expect(sv3.values).to eq(sv.values)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'dot' do
|
32
|
+
sv = SparseVector.new(4, {1 => 1, 3 => 2})
|
33
|
+
dv = DenseVector.new([1.0, 2.0, 3.0, 4.0])
|
34
|
+
lst = DenseVector.new([1, 2, 3, 4])
|
35
|
+
|
36
|
+
expect(sv.dot(dv)).to eq(10.0)
|
37
|
+
expect(dv.dot(dv)).to eq(30.0)
|
38
|
+
expect(lst.dot(dv)).to eq(30.0)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'squared distance' do
|
42
|
+
sv = SparseVector.new(4, {1 => 1, 3 => 2})
|
43
|
+
dv = DenseVector.new([1.0, 2.0, 3.0, 4.0])
|
44
|
+
lst = DenseVector.new([4, 3, 2, 1])
|
45
|
+
|
46
|
+
expect(sv.squared_distance(dv)).to eq(15)
|
47
|
+
expect(sv.squared_distance(lst)).to eq(25)
|
48
|
+
expect(dv.squared_distance(lst)).to eq(20)
|
49
|
+
expect(dv.squared_distance(sv)).to eq(15)
|
50
|
+
expect(lst.squared_distance(sv)).to eq(25)
|
51
|
+
expect(lst.squared_distance(dv)).to eq(20)
|
52
|
+
expect(sv.squared_distance(sv)).to eq(0)
|
53
|
+
expect(dv.squared_distance(dv)).to eq(0)
|
54
|
+
expect(lst.squared_distance(lst)).to eq(0)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'sparse vector indexing' do
|
58
|
+
sv1 = SparseVector.new(4, {1 => 1, 3 => 2})
|
59
|
+
sv2 = SparseVector.new(4, [1, 3], [1, 2])
|
60
|
+
|
61
|
+
expect(sv1[0]).to eq(0)
|
62
|
+
expect(sv1[3]).to eq(2)
|
63
|
+
expect(sv1[1]).to eq(1)
|
64
|
+
expect(sv1[2]).to eq(0)
|
65
|
+
expect(sv1[-1]).to eq(2)
|
66
|
+
expect(sv1[-2]).to eq(0)
|
67
|
+
expect(sv1[-4]).to eq(0)
|
68
|
+
|
69
|
+
expect(sv2[0]).to eq(0)
|
70
|
+
expect(sv2[3]).to eq(2)
|
71
|
+
expect(sv2[1]).to eq(1)
|
72
|
+
expect(sv2[2]).to eq(0)
|
73
|
+
expect(sv2[-1]).to eq(2)
|
74
|
+
expect(sv2[-2]).to eq(0)
|
75
|
+
expect(sv2[-4]).to eq(0)
|
76
|
+
end
|
77
|
+
end
|