Github user tillrohrmann commented on a diff in the pull request:
https://github.com/apache/flink/pull/1156#discussion_r39964582
--- Diff:
flink-staging/flink-ml/src/test/scala/org/apache/flink/ml/classification/MultinomialNaiveBayesValidation.scala
---
@@ -0,0 +1,422 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.ml.classification
+
+import java.io.File
+
+import org.apache.commons.io.FileUtils
+import org.apache.flink.api.scala._
+import org.apache.flink.core.fs.FileSystem.WriteMode
+import org.apache.flink.test.util.FlinkTestBase
+import org.scalatest.{FlatSpec, Matchers}
+
+import scala.util.Sorting
+
+/**
+ * This test is used to compare whether the different versions of
[[MultinomialNaiveBayes]]
+ * resulting of the chooseable "possibilities" give the same result, what
they should.
+ */
+class MultinomialNaiveBayesValidation extends FlatSpec with Matchers with
FlinkTestBase {
+
+ val outputFolder = "tmp/"
+
+
+ behavior of "The MultinomialNaiveBayes implementation"
+
+
+ it should "train the classifier with the basic configuration" in {
+ val env = ExecutionEnvironment.getExecutionEnvironment
+ env.setParallelism(1)
+ val nnb = MultinomialNaiveBayes()
+
+ val trainingDS = env.fromCollection(Classification.bbcTrainData)
+ nnb.fit(trainingDS)
+
+ nnb.saveModelDataSet(outputFolder + "basicConfigModelWord.csv",
outputFolder +
+ "basicConfigModelClass.csv")
+
+ env.execute()
+ }
+
+ it should "use the basicConfigModel model to predict" in {
+
+ val env = ExecutionEnvironment.getExecutionEnvironment
+ env.setParallelism(1)
+
+ val nnb = MultinomialNaiveBayes()
+ nnb.setModelDataSet(env.readCsvFile[(String, String,
Double)](outputFolder +
+ "/basicConfigModelWord.csv", "\n", "|"),
+ env.readCsvFile[(String, Double, Double)](outputFolder +
+ "/basicConfigModelClass.csv", "\n", "|"))
+
+ val solution =
nnb.predict(env.fromCollection(Classification.bbcTestData))
+ solution.writeAsCsv(outputFolder + "basicConfigSolution.csv", "\n",
"\t", WriteMode.OVERWRITE)
+
+ env.execute()
+
+ }
+
+ it should "validate, that basicConfig predicted everything but 357 and
358 correctly" in {
+ val basicA = scala.io.Source.fromFile(outputFolder +
"basicConfigSolution.csv").getLines()
+ .toArray
+ for (line <- basicA) {
+ val split = line.split("\t")
+ if (split(0).toInt <= 10) {
+ assert(split(1).equals("business"))
+ } else if (split(0).toInt <= 356 || split(0).toInt >= 359) {
+ assert(split(1).equals("entertainment"))
+ } else if (split(0).toInt == 357 || split(0).toInt == 358) {
+ assert(split(1).equals("business")) //wrong predicted, but we want
this
+ } else {
+ fail("unknown identifier number in basicConfigSolution.csv" +
split(0))
+ }
+ }
+
+ }
+
+ it should "train the classifier with p1 = 0" in {
+ val env = ExecutionEnvironment.getExecutionEnvironment
+ env.setParallelism(1)
+ val nnb = MultinomialNaiveBayes().setP1(0)
+
+ val trainingDS = env.fromCollection(Classification.bbcTrainData)
+ nnb.fit(trainingDS)
+
+ nnb.saveModelDataSet(outputFolder + "p1_0ModelWord.csv", outputFolder
+ "p1_0ModelClass.csv")
+
+ env.execute()
+ }
+
+ it should "use the p1 = 0 model to predict" in {
+
+ val env = ExecutionEnvironment.getExecutionEnvironment
+ env.setParallelism(1)
+
+
+ val nnb = MultinomialNaiveBayes().setP2(0)
+ nnb.setModelDataSet(env.readCsvFile[(String, String,
Double)](outputFolder +
+ "/p1_0ModelWord.csv", "\n", "|"),
+ env.readCsvFile[(String, Double, Double)](outputFolder +
"/p1_0ModelClass.csv", "\n", "|"))
+
+ val solution =
nnb.predict(env.fromCollection(Classification.bbcTestData))
+ solution.writeAsCsv(outputFolder + "p1_0Solution.csv", "\n", "\t",
WriteMode.OVERWRITE)
+
+
+ env.execute()
+
--- End diff --
line break
---
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.
---