Github user Lewuathe commented on a diff in the pull request:
https://github.com/apache/spark/pull/9756#discussion_r45686856
--- 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 =>
--- End diff --
`sparsity` specifis a ratio of non-zero values in sparse vector. So
correctly we can write
```
sparseRnd.nextDouble() <= sparsity
```
---
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]