Github user Lewuathe commented on a diff in the pull request:
https://github.com/apache/spark/pull/9756#discussion_r45687411
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/util/LinearDataGenerator.scala ---
@@ -131,39 +131,36 @@ object LinearDataGenerator {
eps: Double,
sparsity: Double): Seq[LabeledPoint] = {
require(0.0 <= sparsity && sparsity <= 1.0)
- val rnd = new Random(seed)
- val x = Array.fill[Array[Double]](nPoints)(
- Array.fill[Double](weights.length)(rnd.nextDouble()))
-
- val sparseRnd = new Random(seed)
- x.foreach { v =>
- var i = 0
- val len = v.length
- while (i < len) {
- if (sparseRnd.nextDouble() < sparsity) {
- v(i) = 0.0
- } else {
- v(i) = (v(i) - 0.5) * math.sqrt(12.0 * xVariance(i)) + xMean(i)
- }
- i += 1
- }
- }
-
- val y = x.map { xi =>
- blas.ddot(weights.length, xi, 1, weights, 1) + intercept + eps *
rnd.nextGaussian()
- }
- y.zip(x).map { p =>
- if (sparsity == 0.0) {
+ val rnd = new Random(seed)
+ val rndG = new Random(seed)
+ if (sparsity <= 0.0) {
+ (0 until nPoints).map { _ =>
+ val features = Vectors.dense((0 until weights.length).map { i =>
+ (rnd.nextDouble() - 0.5) * math.sqrt(12.0 * xVariance(i)) +
xMean(i)
+ }.toArray)
+ val label = BLAS.dot(Vectors.dense(weights), features) +
+ intercept + eps * rndG.nextGaussian()
// Return LabeledPoints with DenseVector
- LabeledPoint(p._1, Vectors.dense(p._2))
- } else {
+ LabeledPoint(label, features)
+ }
+ } else {
+ val sparseRnd = new Random(seed)
+ (0 until nPoints).map { _ =>
+ val (values, indices) = (0 until weights.length).filter { _ =>
+ sparseRnd.nextDouble() >= sparsity }.map { i =>
+ ((rnd.nextDouble() - 0.5) * math.sqrt(12.0 * xVariance(i)) +
xMean(i), i)
+ }.unzip
+ val features = Vectors.sparse(weights.length, indices.toArray,
values.toArray)
+ val label = BLAS.dot(Vectors.dense(weights), features) +
+ intercept + eps * rndG.nextGaussian()
--- End diff --
Is there any reason to use separately `rnd` and `rndG` for gaussian
distribution?
I think that if the same test result will be achieved because we use `rnd`
and `rndG` separetely, it is reasonable.
But in this case, the number of use of `rnd` in generation of vector is
already changed. So it might be unnecessary anymore.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]