org.apache.spark.mllib.classification.SVMModel Scala Examples
The following examples show how to use org.apache.spark.mllib.classification.SVMModel.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: PMMLModelExportFactory.scala From iolap with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.dmg.pmml.RegressionNormalizationMethodType import org.apache.spark.mllib.classification.LogisticRegressionModel import org.apache.spark.mllib.classification.SVMModel import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.regression.LassoModel import org.apache.spark.mllib.regression.LinearRegressionModel import org.apache.spark.mllib.regression.RidgeRegressionModel private[mllib] object PMMLModelExportFactory { def createPMMLModelExport(model: Any): PMMLModelExport = { model match { case kmeans: KMeansModel => new KMeansPMMLModelExport(kmeans) case linear: LinearRegressionModel => new GeneralizedLinearPMMLModelExport(linear, "linear regression") case ridge: RidgeRegressionModel => new GeneralizedLinearPMMLModelExport(ridge, "ridge regression") case lasso: LassoModel => new GeneralizedLinearPMMLModelExport(lasso, "lasso regression") case svm: SVMModel => new BinaryClassificationPMMLModelExport( svm, "linear SVM", RegressionNormalizationMethodType.NONE, svm.getThreshold.getOrElse(0.0)) case logistic: LogisticRegressionModel => if (logistic.numClasses == 2) { new BinaryClassificationPMMLModelExport( logistic, "logistic regression", RegressionNormalizationMethodType.LOGIT, logistic.getThreshold.getOrElse(0.5)) } else { throw new IllegalArgumentException( "PMML Export not supported for Multinomial Logistic Regression") } case _ => throw new IllegalArgumentException( "PMML Export not supported for model: " + model.getClass.getName) } } }
Example 2
Source File: SVMWithSGDExample.scala From Swallow with Apache License 2.0 | 5 votes |
package com.intel.hibench.sparkbench.ml import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.mllib.classification.{SVMModel, SVMWithSGD} import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics import org.apache.spark.rdd.RDD import org.apache.spark.mllib.regression.LabeledPoint import scopt.OptionParser object SVMWithSGDExample { case class Params( numIterations: Int = 100, stepSize: Double = 1.0, regParam: Double = 0.01, dataPath: String = null ) def main(args: Array[String]): Unit = { val defaultParams = Params() val parser = new OptionParser[Params]("SVM") { head("SVM: an example of SVM for classification.") opt[Int]("numIterations") .text(s"numIterations, default: ${defaultParams.numIterations}") .action((x,c) => c.copy(numIterations = x)) opt[Double]("stepSize") .text(s"stepSize, default: ${defaultParams.stepSize}") .action((x,c) => c.copy(stepSize = x)) opt[Double]("regParam") .text(s"regParam, default: ${defaultParams.regParam}") .action((x,c) => c.copy(regParam = x)) arg[String]("<dataPath>") .required() .text("data path of SVM") .action((x, c) => c.copy(dataPath = x)) } parser.parse(args, defaultParams) match { case Some(params) => run(params) case _ => sys.exit(1) } } def run(params: Params): Unit = { val conf = new SparkConf().setAppName(s"SVM with $params") .set("spark.shuffle.compress", "false") .set("spark.io.compression.codec", "org.apache.spark.io.LZFCompressionCodec") .set("spark.smartCompress", "false") val sc = new SparkContext(conf) val dataPath = params.dataPath val numIterations = params.numIterations val stepSize = params.stepSize val regParam = params.regParam val data: RDD[LabeledPoint] = sc.objectFile(dataPath) // Split data into training (60%) and test (40%). val splits = data.randomSplit(Array(0.6, 0.4), seed = 11L) val training = splits(0).cache() val test = splits(1) // Run training algorithm to build the model val model = SVMWithSGD.train(training, numIterations, stepSize, regParam) // Clear the default threshold. model.clearThreshold() // Compute raw scores on the test set. val scoreAndLabels = test.map { point => val score = model.predict(point.features) (score, point.label) } // Get evaluation metrics. val metrics = new BinaryClassificationMetrics(scoreAndLabels) val auROC = metrics.areaUnderROC() println("Area under ROC = " + auROC) sc.stop() } }
Example 3
Source File: PMMLModelExportFactorySuite.scala From BigDatalog with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.apache.spark.SparkFunSuite import org.apache.spark.mllib.classification.{LogisticRegressionModel, SVMModel} import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.{LassoModel, LinearRegressionModel, RidgeRegressionModel} import org.apache.spark.mllib.util.LinearDataGenerator class PMMLModelExportFactorySuite extends SparkFunSuite { test("PMMLModelExportFactory create KMeansPMMLModelExport when passing a KMeansModel") { val clusterCenters = Array( Vectors.dense(1.0, 2.0, 6.0), Vectors.dense(1.0, 3.0, 0.0), Vectors.dense(1.0, 4.0, 6.0)) val kmeansModel = new KMeansModel(clusterCenters) val modelExport = PMMLModelExportFactory.createPMMLModelExport(kmeansModel) assert(modelExport.isInstanceOf[KMeansPMMLModelExport]) } test("PMMLModelExportFactory create GeneralizedLinearPMMLModelExport when passing a " + "LinearRegressionModel, RidgeRegressionModel or LassoModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val linearRegressionModel = new LinearRegressionModel(linearInput(0).features, linearInput(0).label) val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel) assert(linearModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val ridgeRegressionModel = new RidgeRegressionModel(linearInput(0).features, linearInput(0).label) val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel) assert(ridgeModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val lassoModel = new LassoModel(linearInput(0).features, linearInput(0).label) val lassoModelExport = PMMLModelExportFactory.createPMMLModelExport(lassoModel) assert(lassoModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) } test("PMMLModelExportFactory create BinaryClassificationPMMLModelExport " + "when passing a LogisticRegressionModel or SVMModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val logisticRegressionModel = new LogisticRegressionModel(linearInput(0).features, linearInput(0).label) val logisticRegressionModelExport = PMMLModelExportFactory.createPMMLModelExport(logisticRegressionModel) assert(logisticRegressionModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) val svmModel = new SVMModel(linearInput(0).features, linearInput(0).label) val svmModelExport = PMMLModelExportFactory.createPMMLModelExport(svmModel) assert(svmModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) } test("PMMLModelExportFactory throw IllegalArgumentException " + "when passing a Multinomial Logistic Regression") { val multiclassLogisticRegressionModel = new LogisticRegressionModel( weights = Vectors.dense(0.1, 0.2, 0.3, 0.4), intercept = 1.0, numFeatures = 2, numClasses = 3) intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(multiclassLogisticRegressionModel) } } test("PMMLModelExportFactory throw IllegalArgumentException when passing an unsupported model") { val invalidModel = new Object intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(invalidModel) } } }
Example 4
Source File: PMMLModelExportFactory.scala From BigDatalog with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.dmg.pmml.RegressionNormalizationMethodType import org.apache.spark.mllib.classification.LogisticRegressionModel import org.apache.spark.mllib.classification.SVMModel import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.regression.LassoModel import org.apache.spark.mllib.regression.LinearRegressionModel import org.apache.spark.mllib.regression.RidgeRegressionModel private[mllib] object PMMLModelExportFactory { def createPMMLModelExport(model: Any): PMMLModelExport = { model match { case kmeans: KMeansModel => new KMeansPMMLModelExport(kmeans) case linear: LinearRegressionModel => new GeneralizedLinearPMMLModelExport(linear, "linear regression") case ridge: RidgeRegressionModel => new GeneralizedLinearPMMLModelExport(ridge, "ridge regression") case lasso: LassoModel => new GeneralizedLinearPMMLModelExport(lasso, "lasso regression") case svm: SVMModel => new BinaryClassificationPMMLModelExport( svm, "linear SVM", RegressionNormalizationMethodType.NONE, svm.getThreshold.getOrElse(0.0)) case logistic: LogisticRegressionModel => if (logistic.numClasses == 2) { new BinaryClassificationPMMLModelExport( logistic, "logistic regression", RegressionNormalizationMethodType.LOGIT, logistic.getThreshold.getOrElse(0.5)) } else { throw new IllegalArgumentException( "PMML Export not supported for Multinomial Logistic Regression") } case _ => throw new IllegalArgumentException( "PMML Export not supported for model: " + model.getClass.getName) } } }
Example 5
Source File: PMMLModelExportFactorySuite.scala From Spark-2.3.1 with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.apache.spark.SparkFunSuite import org.apache.spark.mllib.classification.{LogisticRegressionModel, SVMModel} import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.{LassoModel, LinearRegressionModel, RidgeRegressionModel} import org.apache.spark.mllib.util.LinearDataGenerator class PMMLModelExportFactorySuite extends SparkFunSuite { test("PMMLModelExportFactory create KMeansPMMLModelExport when passing a KMeansModel") { val clusterCenters = Array( Vectors.dense(1.0, 2.0, 6.0), Vectors.dense(1.0, 3.0, 0.0), Vectors.dense(1.0, 4.0, 6.0)) val kmeansModel = new KMeansModel(clusterCenters) val modelExport = PMMLModelExportFactory.createPMMLModelExport(kmeansModel) assert(modelExport.isInstanceOf[KMeansPMMLModelExport]) } test("PMMLModelExportFactory create GeneralizedLinearPMMLModelExport when passing a " + "LinearRegressionModel, RidgeRegressionModel or LassoModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val linearRegressionModel = new LinearRegressionModel(linearInput(0).features, linearInput(0).label) val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel) assert(linearModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val ridgeRegressionModel = new RidgeRegressionModel(linearInput(0).features, linearInput(0).label) val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel) assert(ridgeModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val lassoModel = new LassoModel(linearInput(0).features, linearInput(0).label) val lassoModelExport = PMMLModelExportFactory.createPMMLModelExport(lassoModel) assert(lassoModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) } test("PMMLModelExportFactory create BinaryClassificationPMMLModelExport " + "when passing a LogisticRegressionModel or SVMModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val logisticRegressionModel = new LogisticRegressionModel(linearInput(0).features, linearInput(0).label) val logisticRegressionModelExport = PMMLModelExportFactory.createPMMLModelExport(logisticRegressionModel) assert(logisticRegressionModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) val svmModel = new SVMModel(linearInput(0).features, linearInput(0).label) val svmModelExport = PMMLModelExportFactory.createPMMLModelExport(svmModel) assert(svmModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) } test("PMMLModelExportFactory throw IllegalArgumentException " + "when passing a Multinomial Logistic Regression") { val multiclassLogisticRegressionModel = new LogisticRegressionModel( weights = Vectors.dense(0.1, 0.2, 0.3, 0.4), intercept = 1.0, numFeatures = 2, numClasses = 3) intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(multiclassLogisticRegressionModel) } } test("PMMLModelExportFactory throw IllegalArgumentException when passing an unsupported model") { val invalidModel = new Object intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(invalidModel) } } }
Example 6
Source File: PMMLModelExportFactory.scala From Spark-2.3.1 with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.dmg.pmml.RegressionNormalizationMethodType import org.apache.spark.mllib.classification.LogisticRegressionModel import org.apache.spark.mllib.classification.SVMModel import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.regression.LassoModel import org.apache.spark.mllib.regression.LinearRegressionModel import org.apache.spark.mllib.regression.RidgeRegressionModel private[mllib] object PMMLModelExportFactory { def createPMMLModelExport(model: Any): PMMLModelExport = { model match { case kmeans: KMeansModel => new KMeansPMMLModelExport(kmeans) case linear: LinearRegressionModel => new GeneralizedLinearPMMLModelExport(linear, "linear regression") case ridge: RidgeRegressionModel => new GeneralizedLinearPMMLModelExport(ridge, "ridge regression") case lasso: LassoModel => new GeneralizedLinearPMMLModelExport(lasso, "lasso regression") case svm: SVMModel => new BinaryClassificationPMMLModelExport( svm, "linear SVM", RegressionNormalizationMethodType.NONE, svm.getThreshold.getOrElse(0.0)) case logistic: LogisticRegressionModel => if (logistic.numClasses == 2) { new BinaryClassificationPMMLModelExport( logistic, "logistic regression", RegressionNormalizationMethodType.LOGIT, logistic.getThreshold.getOrElse(0.5)) } else { throw new IllegalArgumentException( "PMML Export not supported for Multinomial Logistic Regression") } case _ => throw new IllegalArgumentException( "PMML Export not supported for model: " + model.getClass.getName) } } }
Example 7
Source File: PMMLModelExportFactorySuite.scala From spark1.52 with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.apache.spark.SparkFunSuite import org.apache.spark.mllib.classification.{LogisticRegressionModel, SVMModel} import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.{LassoModel, LinearRegressionModel, RidgeRegressionModel} import org.apache.spark.mllib.util.LinearDataGenerator val multiclassLogisticRegressionModel = new LogisticRegressionModel( weights = Vectors.dense(0.1, 0.2, 0.3, 0.4), intercept = 1.0, //numClasses 分类数 numFeatures = 2, numClasses = 3) intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(multiclassLogisticRegressionModel) } } test("PMMLModelExportFactory throw IllegalArgumentException when passing an unsupported model") { val invalidModel = new Object intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(invalidModel) } } }
Example 8
Source File: PMMLModelExportFactory.scala From spark1.52 with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.dmg.pmml.RegressionNormalizationMethodType import org.apache.spark.mllib.classification.LogisticRegressionModel import org.apache.spark.mllib.classification.SVMModel import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.regression.LassoModel import org.apache.spark.mllib.regression.LinearRegressionModel import org.apache.spark.mllib.regression.RidgeRegressionModel private[mllib] object PMMLModelExportFactory { def createPMMLModelExport(model: Any): PMMLModelExport = { model match { case kmeans: KMeansModel => new KMeansPMMLModelExport(kmeans) case linear: LinearRegressionModel => new GeneralizedLinearPMMLModelExport(linear, "linear regression") case ridge: RidgeRegressionModel => new GeneralizedLinearPMMLModelExport(ridge, "ridge regression") case lasso: LassoModel => new GeneralizedLinearPMMLModelExport(lasso, "lasso regression") case svm: SVMModel => new BinaryClassificationPMMLModelExport( svm, "linear SVM", RegressionNormalizationMethodType.NONE, svm.getThreshold.getOrElse(0.0)) case logistic: LogisticRegressionModel => if (logistic.numClasses == 2) { new BinaryClassificationPMMLModelExport( logistic, "logistic regression", RegressionNormalizationMethodType.LOGIT, logistic.getThreshold.getOrElse(0.5)) } else { throw new IllegalArgumentException( "PMML Export not supported for Multinomial Logistic Regression") } case _ => throw new IllegalArgumentException( "PMML Export not supported for model: " + model.getClass.getName) } } }
Example 9
Source File: PMMLModelExportFactorySuite.scala From iolap with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.apache.spark.SparkFunSuite import org.apache.spark.mllib.classification.{LogisticRegressionModel, SVMModel} import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.{LassoModel, LinearRegressionModel, RidgeRegressionModel} import org.apache.spark.mllib.util.LinearDataGenerator class PMMLModelExportFactorySuite extends SparkFunSuite { test("PMMLModelExportFactory create KMeansPMMLModelExport when passing a KMeansModel") { val clusterCenters = Array( Vectors.dense(1.0, 2.0, 6.0), Vectors.dense(1.0, 3.0, 0.0), Vectors.dense(1.0, 4.0, 6.0)) val kmeansModel = new KMeansModel(clusterCenters) val modelExport = PMMLModelExportFactory.createPMMLModelExport(kmeansModel) assert(modelExport.isInstanceOf[KMeansPMMLModelExport]) } test("PMMLModelExportFactory create GeneralizedLinearPMMLModelExport when passing a " + "LinearRegressionModel, RidgeRegressionModel or LassoModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val linearRegressionModel = new LinearRegressionModel(linearInput(0).features, linearInput(0).label) val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel) assert(linearModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val ridgeRegressionModel = new RidgeRegressionModel(linearInput(0).features, linearInput(0).label) val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel) assert(ridgeModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val lassoModel = new LassoModel(linearInput(0).features, linearInput(0).label) val lassoModelExport = PMMLModelExportFactory.createPMMLModelExport(lassoModel) assert(lassoModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) } test("PMMLModelExportFactory create BinaryClassificationPMMLModelExport " + "when passing a LogisticRegressionModel or SVMModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val logisticRegressionModel = new LogisticRegressionModel(linearInput(0).features, linearInput(0).label) val logisticRegressionModelExport = PMMLModelExportFactory.createPMMLModelExport(logisticRegressionModel) assert(logisticRegressionModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) val svmModel = new SVMModel(linearInput(0).features, linearInput(0).label) val svmModelExport = PMMLModelExportFactory.createPMMLModelExport(svmModel) assert(svmModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) } test("PMMLModelExportFactory throw IllegalArgumentException " + "when passing a Multinomial Logistic Regression") { val multiclassLogisticRegressionModel = new LogisticRegressionModel( weights = Vectors.dense(0.1, 0.2, 0.3, 0.4), intercept = 1.0, numFeatures = 2, numClasses = 3) intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(multiclassLogisticRegressionModel) } } test("PMMLModelExportFactory throw IllegalArgumentException when passing an unsupported model") { val invalidModel = new Object intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(invalidModel) } } }
Example 10
Source File: PMMLModelExportFactory.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.dmg.pmml.RegressionNormalizationMethodType import org.apache.spark.mllib.classification.LogisticRegressionModel import org.apache.spark.mllib.classification.SVMModel import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.regression.LassoModel import org.apache.spark.mllib.regression.LinearRegressionModel import org.apache.spark.mllib.regression.RidgeRegressionModel private[mllib] object PMMLModelExportFactory { def createPMMLModelExport(model: Any): PMMLModelExport = { model match { case kmeans: KMeansModel => new KMeansPMMLModelExport(kmeans) case linear: LinearRegressionModel => new GeneralizedLinearPMMLModelExport(linear, "linear regression") case ridge: RidgeRegressionModel => new GeneralizedLinearPMMLModelExport(ridge, "ridge regression") case lasso: LassoModel => new GeneralizedLinearPMMLModelExport(lasso, "lasso regression") case svm: SVMModel => new BinaryClassificationPMMLModelExport( svm, "linear SVM", RegressionNormalizationMethodType.NONE, svm.getThreshold.getOrElse(0.0)) case logistic: LogisticRegressionModel => if (logistic.numClasses == 2) { new BinaryClassificationPMMLModelExport( logistic, "logistic regression", RegressionNormalizationMethodType.LOGIT, logistic.getThreshold.getOrElse(0.5)) } else { throw new IllegalArgumentException( "PMML Export not supported for Multinomial Logistic Regression") } case _ => throw new IllegalArgumentException( "PMML Export not supported for model: " + model.getClass.getName) } } }
Example 11
Source File: PMMLModelExportFactorySuite.scala From multi-tenancy-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.apache.spark.SparkFunSuite import org.apache.spark.mllib.classification.{LogisticRegressionModel, SVMModel} import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.{LassoModel, LinearRegressionModel, RidgeRegressionModel} import org.apache.spark.mllib.util.LinearDataGenerator class PMMLModelExportFactorySuite extends SparkFunSuite { test("PMMLModelExportFactory create KMeansPMMLModelExport when passing a KMeansModel") { val clusterCenters = Array( Vectors.dense(1.0, 2.0, 6.0), Vectors.dense(1.0, 3.0, 0.0), Vectors.dense(1.0, 4.0, 6.0)) val kmeansModel = new KMeansModel(clusterCenters) val modelExport = PMMLModelExportFactory.createPMMLModelExport(kmeansModel) assert(modelExport.isInstanceOf[KMeansPMMLModelExport]) } test("PMMLModelExportFactory create GeneralizedLinearPMMLModelExport when passing a " + "LinearRegressionModel, RidgeRegressionModel or LassoModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val linearRegressionModel = new LinearRegressionModel(linearInput(0).features, linearInput(0).label) val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel) assert(linearModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val ridgeRegressionModel = new RidgeRegressionModel(linearInput(0).features, linearInput(0).label) val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel) assert(ridgeModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val lassoModel = new LassoModel(linearInput(0).features, linearInput(0).label) val lassoModelExport = PMMLModelExportFactory.createPMMLModelExport(lassoModel) assert(lassoModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) } test("PMMLModelExportFactory create BinaryClassificationPMMLModelExport " + "when passing a LogisticRegressionModel or SVMModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val logisticRegressionModel = new LogisticRegressionModel(linearInput(0).features, linearInput(0).label) val logisticRegressionModelExport = PMMLModelExportFactory.createPMMLModelExport(logisticRegressionModel) assert(logisticRegressionModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) val svmModel = new SVMModel(linearInput(0).features, linearInput(0).label) val svmModelExport = PMMLModelExportFactory.createPMMLModelExport(svmModel) assert(svmModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) } test("PMMLModelExportFactory throw IllegalArgumentException " + "when passing a Multinomial Logistic Regression") { val multiclassLogisticRegressionModel = new LogisticRegressionModel( weights = Vectors.dense(0.1, 0.2, 0.3, 0.4), intercept = 1.0, numFeatures = 2, numClasses = 3) intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(multiclassLogisticRegressionModel) } } test("PMMLModelExportFactory throw IllegalArgumentException when passing an unsupported model") { val invalidModel = new Object intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(invalidModel) } } }
Example 12
Source File: PMMLModelExportFactory.scala From multi-tenancy-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.dmg.pmml.RegressionNormalizationMethodType import org.apache.spark.mllib.classification.LogisticRegressionModel import org.apache.spark.mllib.classification.SVMModel import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.regression.LassoModel import org.apache.spark.mllib.regression.LinearRegressionModel import org.apache.spark.mllib.regression.RidgeRegressionModel private[mllib] object PMMLModelExportFactory { def createPMMLModelExport(model: Any): PMMLModelExport = { model match { case kmeans: KMeansModel => new KMeansPMMLModelExport(kmeans) case linear: LinearRegressionModel => new GeneralizedLinearPMMLModelExport(linear, "linear regression") case ridge: RidgeRegressionModel => new GeneralizedLinearPMMLModelExport(ridge, "ridge regression") case lasso: LassoModel => new GeneralizedLinearPMMLModelExport(lasso, "lasso regression") case svm: SVMModel => new BinaryClassificationPMMLModelExport( svm, "linear SVM", RegressionNormalizationMethodType.NONE, svm.getThreshold.getOrElse(0.0)) case logistic: LogisticRegressionModel => if (logistic.numClasses == 2) { new BinaryClassificationPMMLModelExport( logistic, "logistic regression", RegressionNormalizationMethodType.LOGIT, logistic.getThreshold.getOrElse(0.5)) } else { throw new IllegalArgumentException( "PMML Export not supported for Multinomial Logistic Regression") } case _ => throw new IllegalArgumentException( "PMML Export not supported for model: " + model.getClass.getName) } } }
Example 13
Source File: PMMLModelExportFactorySuite.scala From sparkoscope with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.apache.spark.SparkFunSuite import org.apache.spark.mllib.classification.{LogisticRegressionModel, SVMModel} import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.{LassoModel, LinearRegressionModel, RidgeRegressionModel} import org.apache.spark.mllib.util.LinearDataGenerator class PMMLModelExportFactorySuite extends SparkFunSuite { test("PMMLModelExportFactory create KMeansPMMLModelExport when passing a KMeansModel") { val clusterCenters = Array( Vectors.dense(1.0, 2.0, 6.0), Vectors.dense(1.0, 3.0, 0.0), Vectors.dense(1.0, 4.0, 6.0)) val kmeansModel = new KMeansModel(clusterCenters) val modelExport = PMMLModelExportFactory.createPMMLModelExport(kmeansModel) assert(modelExport.isInstanceOf[KMeansPMMLModelExport]) } test("PMMLModelExportFactory create GeneralizedLinearPMMLModelExport when passing a " + "LinearRegressionModel, RidgeRegressionModel or LassoModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val linearRegressionModel = new LinearRegressionModel(linearInput(0).features, linearInput(0).label) val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel) assert(linearModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val ridgeRegressionModel = new RidgeRegressionModel(linearInput(0).features, linearInput(0).label) val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel) assert(ridgeModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val lassoModel = new LassoModel(linearInput(0).features, linearInput(0).label) val lassoModelExport = PMMLModelExportFactory.createPMMLModelExport(lassoModel) assert(lassoModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) } test("PMMLModelExportFactory create BinaryClassificationPMMLModelExport " + "when passing a LogisticRegressionModel or SVMModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val logisticRegressionModel = new LogisticRegressionModel(linearInput(0).features, linearInput(0).label) val logisticRegressionModelExport = PMMLModelExportFactory.createPMMLModelExport(logisticRegressionModel) assert(logisticRegressionModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) val svmModel = new SVMModel(linearInput(0).features, linearInput(0).label) val svmModelExport = PMMLModelExportFactory.createPMMLModelExport(svmModel) assert(svmModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) } test("PMMLModelExportFactory throw IllegalArgumentException " + "when passing a Multinomial Logistic Regression") { val multiclassLogisticRegressionModel = new LogisticRegressionModel( weights = Vectors.dense(0.1, 0.2, 0.3, 0.4), intercept = 1.0, numFeatures = 2, numClasses = 3) intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(multiclassLogisticRegressionModel) } } test("PMMLModelExportFactory throw IllegalArgumentException when passing an unsupported model") { val invalidModel = new Object intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(invalidModel) } } }
Example 14
Source File: PMMLModelExportFactory.scala From sparkoscope with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.dmg.pmml.RegressionNormalizationMethodType import org.apache.spark.mllib.classification.LogisticRegressionModel import org.apache.spark.mllib.classification.SVMModel import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.regression.LassoModel import org.apache.spark.mllib.regression.LinearRegressionModel import org.apache.spark.mllib.regression.RidgeRegressionModel private[mllib] object PMMLModelExportFactory { def createPMMLModelExport(model: Any): PMMLModelExport = { model match { case kmeans: KMeansModel => new KMeansPMMLModelExport(kmeans) case linear: LinearRegressionModel => new GeneralizedLinearPMMLModelExport(linear, "linear regression") case ridge: RidgeRegressionModel => new GeneralizedLinearPMMLModelExport(ridge, "ridge regression") case lasso: LassoModel => new GeneralizedLinearPMMLModelExport(lasso, "lasso regression") case svm: SVMModel => new BinaryClassificationPMMLModelExport( svm, "linear SVM", RegressionNormalizationMethodType.NONE, svm.getThreshold.getOrElse(0.0)) case logistic: LogisticRegressionModel => if (logistic.numClasses == 2) { new BinaryClassificationPMMLModelExport( logistic, "logistic regression", RegressionNormalizationMethodType.LOGIT, logistic.getThreshold.getOrElse(0.5)) } else { throw new IllegalArgumentException( "PMML Export not supported for Multinomial Logistic Regression") } case _ => throw new IllegalArgumentException( "PMML Export not supported for model: " + model.getClass.getName) } } }
Example 15
package se.uu.farmbio.cp.alg import org.apache.spark.mllib.classification.SVMModel import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.optimization.HingeGradient import org.apache.spark.mllib.optimization.LBFGS import org.apache.spark.mllib.optimization.SquaredL2Updater import org.apache.spark.mllib.regression.LabeledPoint import org.apache.spark.mllib.util.MLUtils import org.apache.spark.rdd.RDD import se.uu.farmbio.cp.UnderlyingAlgorithm //Define a SVMs UnderlyingAlgorithm private object SVM { def trainingProcedure( input: RDD[LabeledPoint], maxNumItearations: Int, regParam: Double, numCorrections: Int, convergenceTol: Double) = { //Train SVM with LBFGS val numFeatures = input.take(1)(0).features.size val training = input.map(x => (x.label, MLUtils.appendBias(x.features))).cache() val initialWeightsWithIntercept = Vectors.dense(new Array[Double](numFeatures + 1)) val (weightsWithIntercept, _) = LBFGS.runLBFGS( training, new HingeGradient(), new SquaredL2Updater(), numCorrections, convergenceTol, maxNumItearations, regParam, initialWeightsWithIntercept) //Create the model using the weights val model = new SVMModel( Vectors.dense(weightsWithIntercept.toArray.slice(0, weightsWithIntercept.size - 1)), weightsWithIntercept(weightsWithIntercept.size - 1)) //Return raw score predictor model.clearThreshold() model } } class SVM(val model: SVMModel) extends UnderlyingAlgorithm(model.predict) { def this( input: RDD[LabeledPoint], maxNumItearations: Int = 100, regParam: Double = 0.1, numCorrections: Int = 10, convergenceTol: Double = 1e-4) = { this(SVM.trainingProcedure( input, maxNumItearations, regParam, numCorrections, convergenceTol)) } def nonConformityMeasure(newSample: LabeledPoint) = { val score = predictor(newSample.features) if (newSample.label == 1.0) { -score } else { score } } }
Example 16
Source File: LibLinAlg.scala From spark-cp with Apache License 2.0 | 5 votes |
package se.uu.farmbio.cp.liblinear import org.apache.spark.mllib.classification.SVMModel import org.apache.spark.mllib.linalg.Vector import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.LabeledPoint import de.bwaldvogel.liblinear.Feature import de.bwaldvogel.liblinear.FeatureNode import de.bwaldvogel.liblinear.Linear import de.bwaldvogel.liblinear.Parameter import de.bwaldvogel.liblinear.Problem import de.bwaldvogel.liblinear.SolverType import se.uu.farmbio.cp.UnderlyingAlgorithm import se.uu.farmbio.cp.Deserializer object LibLinAlg { private def vectorToFeatures(v: Vector) = { val indices = v.toSparse.indices val values = v.toSparse.values indices .zip(values) .sortBy { case (i, v) => i } .map { case (i, v) => new FeatureNode(i + 1, v) .asInstanceOf[Feature] } } private def train( input: Array[LabeledPoint], solverType: SolverType, c: Double, tol: Double) = { //configure problem val problem = new Problem problem.l = input.length problem.n = input(0).features.size problem.x = input.map { p => vectorToFeatures(p.features) } problem.y = input.map(_.label + 1.0) problem.bias = -1.0 //train val parameter = new Parameter(solverType, c, tol) val libLinModel = Linear.train(problem, parameter) //convert to Spark SVMModel val weights = libLinModel.getFeatureWeights val intercept = libLinModel.getBias val svmModel = new SVMModel(Vectors.dense(weights).toSparse, intercept) svmModel.clearThreshold svmModel } } object LibLinAlgDeserializer extends Deserializer[LibLinAlg] { override def deserialize(alg: String) = { val splitted = alg.split(",", 2) val intercept = splitted(0) val weights = splitted(1) val model = new SVMModel(Vectors.parse(weights).toSparse, intercept.toDouble) model.clearThreshold() new LibLinAlg(model) } } class LibLinAlg( val svmModel: SVMModel) extends UnderlyingAlgorithm( (features: Vector) => svmModel.predict(features)) { def this( training: Array[LabeledPoint], solverType: SolverType, regParam: Double, tol: Double) = { this(LibLinAlg.train(training, solverType, regParam, tol)) } override def nonConformityMeasure(newSample: LabeledPoint) = { val score = predictor(newSample.features) if (newSample.label == 1.0) { score } else { -score } } override def toString = { this.svmModel.intercept + "," + this.svmModel.weights.toString } }
Example 17
Source File: PMMLModelExportFactorySuite.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.mllib.pmml.export import org.apache.spark.SparkFunSuite import org.apache.spark.mllib.classification.{LogisticRegressionModel, SVMModel} import org.apache.spark.mllib.clustering.KMeansModel import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.{LassoModel, LinearRegressionModel, RidgeRegressionModel} import org.apache.spark.mllib.util.LinearDataGenerator class PMMLModelExportFactorySuite extends SparkFunSuite { test("PMMLModelExportFactory create KMeansPMMLModelExport when passing a KMeansModel") { val clusterCenters = Array( Vectors.dense(1.0, 2.0, 6.0), Vectors.dense(1.0, 3.0, 0.0), Vectors.dense(1.0, 4.0, 6.0)) val kmeansModel = new KMeansModel(clusterCenters) val modelExport = PMMLModelExportFactory.createPMMLModelExport(kmeansModel) assert(modelExport.isInstanceOf[KMeansPMMLModelExport]) } test("PMMLModelExportFactory create GeneralizedLinearPMMLModelExport when passing a " + "LinearRegressionModel, RidgeRegressionModel or LassoModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val linearRegressionModel = new LinearRegressionModel(linearInput(0).features, linearInput(0).label) val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel) assert(linearModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val ridgeRegressionModel = new RidgeRegressionModel(linearInput(0).features, linearInput(0).label) val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel) assert(ridgeModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) val lassoModel = new LassoModel(linearInput(0).features, linearInput(0).label) val lassoModelExport = PMMLModelExportFactory.createPMMLModelExport(lassoModel) assert(lassoModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport]) } test("PMMLModelExportFactory create BinaryClassificationPMMLModelExport " + "when passing a LogisticRegressionModel or SVMModel") { val linearInput = LinearDataGenerator.generateLinearInput(3.0, Array(10.0, 10.0), 1, 17) val logisticRegressionModel = new LogisticRegressionModel(linearInput(0).features, linearInput(0).label) val logisticRegressionModelExport = PMMLModelExportFactory.createPMMLModelExport(logisticRegressionModel) assert(logisticRegressionModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) val svmModel = new SVMModel(linearInput(0).features, linearInput(0).label) val svmModelExport = PMMLModelExportFactory.createPMMLModelExport(svmModel) assert(svmModelExport.isInstanceOf[BinaryClassificationPMMLModelExport]) } test("PMMLModelExportFactory throw IllegalArgumentException " + "when passing a Multinomial Logistic Regression") { val multiclassLogisticRegressionModel = new LogisticRegressionModel( weights = Vectors.dense(0.1, 0.2, 0.3, 0.4), intercept = 1.0, numFeatures = 2, numClasses = 3) intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(multiclassLogisticRegressionModel) } } test("PMMLModelExportFactory throw IllegalArgumentException when passing an unsupported model") { val invalidModel = new Object intercept[IllegalArgumentException] { PMMLModelExportFactory.createPMMLModelExport(invalidModel) } } }