Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
spark
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cs525-sp18-g07
spark
Commits
2210e8cc
Commit
2210e8cc
authored
11 years ago
by
Reynold Xin
Browse files
Options
Downloads
Patches
Plain Diff
Use a different validation dataset for Logistic Regression prediction testing.
parent
87a9dd89
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mllib/src/test/scala/spark/mllib/regression/LogisticRegressionSuite.scala
+17
-12
17 additions, 12 deletions
...cala/spark/mllib/regression/LogisticRegressionSuite.scala
with
17 additions
and
12 deletions
mllib/src/test/scala/spark/mllib/regression/LogisticRegressionSuite.scala
+
17
−
12
View file @
2210e8cc
...
@@ -35,10 +35,11 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
...
@@ -35,10 +35,11 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
// Generate input of the form Y = logistic(offset + scale*X)
// Generate input of the form Y = logistic(offset + scale*X)
def
generateLogisticInput
(
def
generateLogisticInput
(
offset
:
Double
,
offset
:
Double
,
scale
:
Double
,
scale
:
Double
,
nPoints
:
Int
)
:
Seq
[(
Double
,
Array
[
Double
])]
=
{
nPoints
:
Int
,
val
rnd
=
new
Random
(
42
)
seed
:
Int
)
:
Seq
[(
Double
,
Array
[
Double
])]
=
{
val
rnd
=
new
Random
(
seed
)
val
x1
=
Array
.
fill
[
Double
](
nPoints
)(
rnd
.
nextGaussian
())
val
x1
=
Array
.
fill
[
Double
](
nPoints
)(
rnd
.
nextGaussian
())
// NOTE: if U is uniform[0, 1] then ln(u) - ln(1-u) is Logistic(0,1)
// NOTE: if U is uniform[0, 1] then ln(u) - ln(1-u) is Logistic(0,1)
...
@@ -60,12 +61,12 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
...
@@ -60,12 +61,12 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
}
}
def
validatePrediction
(
predictions
:
Seq
[
Double
],
input
:
Seq
[(
Double
,
Array
[
Double
])])
{
def
validatePrediction
(
predictions
:
Seq
[
Double
],
input
:
Seq
[(
Double
,
Array
[
Double
])])
{
val
o
ffPredictions
=
predictions
.
zip
(
input
).
filter
{
case
(
prediction
,
(
expected
,
_
))
=>
val
numO
ffPredictions
=
predictions
.
zip
(
input
).
filter
{
case
(
prediction
,
(
expected
,
_
))
=>
// A prediction is off if the prediction is more than 0.5 away from expected value.
// A prediction is off if the prediction is more than 0.5 away from expected value.
math
.
abs
(
prediction
-
expected
)
>
0.5
math
.
abs
(
prediction
-
expected
)
>
0.5
}.
size
}.
size
// At least 80% of the predictions should be on.
// At least 80% of the predictions should be on.
assert
(
o
ffPredictions
<
input
.
length
/
5
)
assert
(
numO
ffPredictions
<
input
.
length
/
5
)
}
}
// Test if we can correctly learn A, B where Y = logistic(A + B*X)
// Test if we can correctly learn A, B where Y = logistic(A + B*X)
...
@@ -74,7 +75,7 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
...
@@ -74,7 +75,7 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
val
A
=
2.0
val
A
=
2.0
val
B
=
-
1.5
val
B
=
-
1.5
val
testData
=
generateLogisticInput
(
A
,
B
,
nPoints
)
val
testData
=
generateLogisticInput
(
A
,
B
,
nPoints
,
42
)
val
testRDD
=
sc
.
parallelize
(
testData
,
2
)
val
testRDD
=
sc
.
parallelize
(
testData
,
2
)
testRDD
.
cache
()
testRDD
.
cache
()
...
@@ -87,11 +88,13 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
...
@@ -87,11 +88,13 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
assert
(
weight0
>=
-
1.60
&&
weight0
<=
-
1.40
,
weight0
+
" not in [-1.6, -1.4]"
)
assert
(
weight0
>=
-
1.60
&&
weight0
<=
-
1.40
,
weight0
+
" not in [-1.6, -1.4]"
)
assert
(
model
.
intercept
>=
1.9
&&
model
.
intercept
<=
2.1
,
model
.
intercept
+
" not in [1.9, 2.1]"
)
assert
(
model
.
intercept
>=
1.9
&&
model
.
intercept
<=
2.1
,
model
.
intercept
+
" not in [1.9, 2.1]"
)
val
validationData
=
generateLogisticInput
(
A
,
B
,
nPoints
,
17
)
val
validationRDD
=
sc
.
parallelize
(
validationData
,
2
)
// Test prediction on RDD.
// Test prediction on RDD.
validatePrediction
(
model
.
predict
(
test
RDD
.
map
(
_
.
_2
)).
collect
(),
test
Data
)
validatePrediction
(
model
.
predict
(
validation
RDD
.
map
(
_
.
_2
)).
collect
(),
validation
Data
)
// Test prediction on Array.
// Test prediction on Array.
validatePrediction
(
test
Data
.
map
(
row
=>
model
.
predict
(
row
.
_2
)),
test
Data
)
validatePrediction
(
validation
Data
.
map
(
row
=>
model
.
predict
(
row
.
_2
)),
validation
Data
)
}
}
test
(
"logistic regression with initial weights"
)
{
test
(
"logistic regression with initial weights"
)
{
...
@@ -99,7 +102,7 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
...
@@ -99,7 +102,7 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
val
A
=
2.0
val
A
=
2.0
val
B
=
-
1.5
val
B
=
-
1.5
val
testData
=
generateLogisticInput
(
A
,
B
,
nPoints
)
val
testData
=
generateLogisticInput
(
A
,
B
,
nPoints
,
42
)
val
initialB
=
-
1.0
val
initialB
=
-
1.0
val
initialWeights
=
Array
(
initialB
)
val
initialWeights
=
Array
(
initialB
)
...
@@ -116,10 +119,12 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
...
@@ -116,10 +119,12 @@ class LogisticRegressionSuite extends FunSuite with BeforeAndAfterAll {
assert
(
weight0
>=
-
1.60
&&
weight0
<=
-
1.40
,
weight0
+
" not in [-1.6, -1.4]"
)
assert
(
weight0
>=
-
1.60
&&
weight0
<=
-
1.40
,
weight0
+
" not in [-1.6, -1.4]"
)
assert
(
model
.
intercept
>=
1.9
&&
model
.
intercept
<=
2.1
,
model
.
intercept
+
" not in [1.9, 2.1]"
)
assert
(
model
.
intercept
>=
1.9
&&
model
.
intercept
<=
2.1
,
model
.
intercept
+
" not in [1.9, 2.1]"
)
val
validationData
=
generateLogisticInput
(
A
,
B
,
nPoints
,
17
)
val
validationRDD
=
sc
.
parallelize
(
validationData
,
2
)
// Test prediction on RDD.
// Test prediction on RDD.
validatePrediction
(
model
.
predict
(
test
RDD
.
map
(
_
.
_2
)).
collect
(),
test
Data
)
validatePrediction
(
model
.
predict
(
validation
RDD
.
map
(
_
.
_2
)).
collect
(),
validation
Data
)
// Test prediction on Array.
// Test prediction on Array.
validatePrediction
(
test
Data
.
map
(
row
=>
model
.
predict
(
row
.
_2
)),
test
Data
)
validatePrediction
(
validation
Data
.
map
(
row
=>
model
.
predict
(
row
.
_2
)),
validation
Data
)
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment