forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#380 from mateiz/py-bayes
Add Naive Bayes to Python MLlib, and some API fixes - Added a Python wrapper for Naive Bayes - Updated the Scala Naive Bayes to match the style of our other algorithms better and in particular make it easier to call from Java (added builder pattern, removed default value in train method) - Updated Python MLlib functions to not require a SparkContext; we can get that from the RDD the user gives - Added a toString method in LabeledPoint - Made the Python MLlib tests run as part of run-tests as well (before they could only be run individually through each file)
- Loading branch information
Showing
20 changed files
with
297 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
0, 1 0 0 | ||
0, 2 0 0 | ||
1, 0 1 0 | ||
1, 0 2 0 | ||
2, 0 0 1 | ||
2, 0 0 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
mllib/src/test/java/org/apache/spark/mllib/classification/JavaNaiveBayesSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.apache.spark.mllib.classification; | ||
|
||
import org.apache.spark.api.java.JavaRDD; | ||
import org.apache.spark.api.java.JavaSparkContext; | ||
import org.apache.spark.mllib.regression.LabeledPoint; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.Serializable; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class JavaNaiveBayesSuite implements Serializable { | ||
private transient JavaSparkContext sc; | ||
|
||
@Before | ||
public void setUp() { | ||
sc = new JavaSparkContext("local", "JavaNaiveBayesSuite"); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
sc.stop(); | ||
sc = null; | ||
System.clearProperty("spark.driver.port"); | ||
} | ||
|
||
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}) | ||
); | ||
|
||
private int validatePrediction(List<LabeledPoint> points, NaiveBayesModel model) { | ||
int correct = 0; | ||
for (LabeledPoint p: points) { | ||
if (model.predict(p.features()) == p.label()) { | ||
correct += 1; | ||
} | ||
} | ||
return correct; | ||
} | ||
|
||
@Test | ||
public void runUsingConstructor() { | ||
JavaRDD<LabeledPoint> testRDD = sc.parallelize(POINTS, 2).cache(); | ||
|
||
NaiveBayes nb = new NaiveBayes().setLambda(1.0); | ||
NaiveBayesModel model = nb.run(testRDD.rdd()); | ||
|
||
int numAccurate = validatePrediction(POINTS, model); | ||
Assert.assertEquals(POINTS.size(), numAccurate); | ||
} | ||
|
||
@Test | ||
public void runUsingStaticMethods() { | ||
JavaRDD<LabeledPoint> testRDD = sc.parallelize(POINTS, 2).cache(); | ||
|
||
NaiveBayesModel model1 = NaiveBayes.train(testRDD.rdd()); | ||
int numAccurate1 = validatePrediction(POINTS, model1); | ||
Assert.assertEquals(POINTS.size(), numAccurate1); | ||
|
||
NaiveBayesModel model2 = NaiveBayes.train(testRDD.rdd(), 0.5); | ||
int numAccurate2 = validatePrediction(POINTS, model2); | ||
Assert.assertEquals(POINTS.size(), numAccurate2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.