Skip to content

Commit

Permalink
passed test compile
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxr committed Mar 26, 2014
1 parent 1859701 commit 75c83a4
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ trait Vector extends Serializable {
* Converts the instance to a breeze vector.
*/
private[mllib] def toBreeze: BV[Double]

/**
* Gets the value of the ith element.
* @param i index
*/
private[mllib] def apply(i: Int): Double = toBreeze(i)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.mllib.linalg.Vectors;
import org.apache.spark.mllib.regression.LabeledPoint;
import org.junit.After;
import org.junit.Assert;
Expand All @@ -45,12 +46,12 @@ public void tearDown() {
}

private static final List<LabeledPoint> POINTS = Arrays.asList(
new LabeledPoint(0, new double[] {1.0, 0.0, 0.0}),
new LabeledPoint(0, new double[] {2.0, 0.0, 0.0}),
new LabeledPoint(1, new double[] {0.0, 1.0, 0.0}),
new LabeledPoint(1, new double[] {0.0, 2.0, 0.0}),
new LabeledPoint(2, new double[] {0.0, 0.0, 1.0}),
new LabeledPoint(2, new double[] {0.0, 0.0, 2.0})
new LabeledPoint(0, Vectors.dense(1.0, 0.0, 0.0)),
new LabeledPoint(0, Vectors.dense(2.0, 0.0, 0.0)),
new LabeledPoint(1, Vectors.dense(0.0, 1.0, 0.0)),
new LabeledPoint(1, Vectors.dense(0.0, 2.0, 0.0)),
new LabeledPoint(2, Vectors.dense(0.0, 0.0, 1.0)),
new LabeledPoint(2, Vectors.dense(0.0, 0.0, 2.0))
);

private int validatePrediction(List<LabeledPoint> points, NaiveBayesModel model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

import java.io.Serializable;

import com.google.common.collect.Lists;

import scala.Tuple2;

import com.google.common.collect.Lists;

import org.junit.Test;
import static org.junit.Assert.*;

Expand All @@ -36,7 +36,7 @@ public void denseArrayConstruction() {

@Test
public void sparseArrayConstruction() {
Vector v = Vectors.sparse(3, Lists.newArrayList(
Vector v = Vectors.sparse(3, Lists.<Tuple2<Integer, Double>>newArrayList(
new Tuple2<Integer, Double>(0, 2.0),
new Tuple2<Integer, Double>(2, 3.0)));
assertArrayEquals(new double[]{2.0, 0.0, 3.0}, v.toArray(), 0.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class LogisticRegressionSuite extends FunSuite with LocalSparkContext with Shoul
val testData = LogisticRegressionSuite.generateLogisticInput(A, B, nPoints, 42)

val initialB = -1.0
val initialWeights = Array(initialB)
val initialWeights = Vectors.dense(initialB)

val testRDD = sc.parallelize(testData, 2)
testRDD.cache()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package org.apache.spark.mllib.classification

import scala.util.Random

import org.scalatest.BeforeAndAfterAll
import org.scalatest.FunSuite

import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.util.LocalSparkContext
import org.apache.spark.mllib.linalg.Vectors

object NaiveBayesSuite {

Expand Down Expand Up @@ -54,7 +54,7 @@ object NaiveBayesSuite {
if (rnd.nextDouble() < _theta(y)(j)) 1 else 0
}

LabeledPoint(y, xi)
LabeledPoint(y, Vectors.dense(xi))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ package org.apache.spark.mllib.classification
import scala.util.Random
import scala.collection.JavaConversions._

import org.scalatest.BeforeAndAfterAll
import org.scalatest.FunSuite

import org.jblas.DoubleMatrix

import org.apache.spark.SparkException
import org.apache.spark.mllib.regression._
import org.apache.spark.mllib.util.LocalSparkContext
import org.apache.spark.mllib.linalg.Vectors

object SVMSuite {

Expand All @@ -54,7 +54,7 @@ object SVMSuite {
intercept + 0.01 * rnd.nextGaussian()
if (yD < 0) 0.0 else 1.0
}
y.zip(x).map(p => LabeledPoint(p._1, p._2))
y.zip(x).map(p => LabeledPoint(p._1, Vectors.dense(p._2)))
}

}
Expand Down Expand Up @@ -110,7 +110,7 @@ class SVMSuite extends FunSuite with LocalSparkContext {

val initialB = -1.0
val initialC = -1.0
val initialWeights = Array(initialB,initialC)
val initialWeights = Vectors.dense(initialB, initialC)

val testRDD = sc.parallelize(testData, 2)
testRDD.cache()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ package org.apache.spark.mllib.optimization
import scala.util.Random
import scala.collection.JavaConversions._

import org.scalatest.BeforeAndAfterAll
import org.scalatest.FunSuite
import org.scalatest.matchers.ShouldMatchers

import org.apache.spark.SparkContext
import org.apache.spark.mllib.regression._
import org.apache.spark.mllib.util.LocalSparkContext
import org.apache.spark.mllib.linalg.Vectors

object GradientDescentSuite {

Expand Down Expand Up @@ -58,7 +57,7 @@ object GradientDescentSuite {
if (yVal > 0) 1 else 0
}

val testData = (0 until nPoints).map(i => LabeledPoint(y(i), Array(x1(i))))
val testData = (0 until nPoints).map(i => LabeledPoint(y(i), Vectors.dense(Array(x1(i)))))
testData
}
}
Expand All @@ -83,11 +82,11 @@ class GradientDescentSuite extends FunSuite with LocalSparkContext with ShouldMa
// Add a extra variable consisting of all 1.0's for the intercept.
val testData = GradientDescentSuite.generateGDInput(A, B, nPoints, 42)
val data = testData.map { case LabeledPoint(label, features) =>
label -> Array(1.0, features: _*)
label -> Vectors.dense(1.0, features.toArray: _*)
}

val dataRDD = sc.parallelize(data, 2).cache()
val initialWeightsWithIntercept = Array(1.0, initialWeights: _*)
val initialWeightsWithIntercept = Vectors.dense(0.0, initialWeights: _*)

val (_, loss) = GradientDescent.runMiniBatchSGD(
dataRDD,
Expand All @@ -113,13 +112,13 @@ class GradientDescentSuite extends FunSuite with LocalSparkContext with ShouldMa
// Add a extra variable consisting of all 1.0's for the intercept.
val testData = GradientDescentSuite.generateGDInput(2.0, -1.5, 10000, 42)
val data = testData.map { case LabeledPoint(label, features) =>
label -> Array(1.0, features: _*)
label -> Vectors.dense(1.0, features.toArray: _*)
}

val dataRDD = sc.parallelize(data, 2).cache()

// Prepare non-zero weights
val initialWeightsWithIntercept = Array(1.0, 0.5)
val initialWeightsWithIntercept = Vectors.dense(1.0, 0.5)

val regParam0 = 0
val (newWeights0, loss0) = GradientDescent.runMiniBatchSGD(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@

package org.apache.spark.mllib.regression


import org.scalatest.BeforeAndAfterAll
import org.scalatest.FunSuite

import org.apache.spark.SparkContext
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.util.{LinearDataGenerator, LocalSparkContext}

class LassoSuite extends FunSuite with LocalSparkContext {
Expand Down Expand Up @@ -51,7 +49,6 @@ class LassoSuite extends FunSuite with LocalSparkContext {
ls.optimizer.setStepSize(1.0).setRegParam(0.01).setNumIterations(20)

val model = ls.run(testRDD)

val weight0 = model.weights(0)
val weight1 = model.weights(1)
assert(model.intercept >= 1.9 && model.intercept <= 2.1, model.intercept + " not in [1.9, 2.1]")
Expand Down Expand Up @@ -79,7 +76,7 @@ class LassoSuite extends FunSuite with LocalSparkContext {

val initialB = -1.0
val initialC = -1.0
val initialWeights = Array(initialB,initialC)
val initialWeights = Vectors.dense(Array(initialB, initialC))

val testRDD = sc.parallelize(testData, 2)
testRDD.cache()
Expand All @@ -88,7 +85,6 @@ class LassoSuite extends FunSuite with LocalSparkContext {
ls.optimizer.setStepSize(1.0).setRegParam(0.01).setNumIterations(20)

val model = ls.run(testRDD, initialWeights)

val weight0 = model.weights(0)
val weight1 = model.weights(1)
assert(model.intercept >= 1.9 && model.intercept <= 2.1, model.intercept + " not in [1.9, 2.1]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ class LinearRegressionSuite extends FunSuite with LocalSparkContext {
linReg.optimizer.setNumIterations(1000).setStepSize(1.0)

val model = linReg.run(testRDD)

assert(model.intercept >= 2.5 && model.intercept <= 3.5)
assert(model.weights.length === 2)
assert(model.weights(0) >= 9.0 && model.weights(0) <= 11.0)
assert(model.weights(1) >= 9.0 && model.weights(1) <= 11.0)

val weights = model.weights
assert(weights.size === 2)
assert(weights(0) >= 9.0 && weights(0) <= 11.0)
assert(weights(1) >= 9.0 && weights(1) <= 11.0)

val validationData = LinearDataGenerator.generateLinearInput(
3.0, Array(10.0, 10.0), 100, 17)
Expand All @@ -67,9 +68,11 @@ class LinearRegressionSuite extends FunSuite with LocalSparkContext {
val model = linReg.run(testRDD)

assert(model.intercept === 0.0)
assert(model.weights.length === 2)
assert(model.weights(0) >= 9.0 && model.weights(0) <= 11.0)
assert(model.weights(1) >= 9.0 && model.weights(1) <= 11.0)

val weights = model.weights
assert(weights.size === 2)
assert(weights(0) >= 9.0 && weights(0) <= 11.0)
assert(weights(1) >= 9.0 && weights(1) <= 11.0)

val validationData = LinearDataGenerator.generateLinearInput(
0.0, Array(10.0, 10.0), 100, 17)
Expand Down

0 comments on commit 75c83a4

Please sign in to comment.