|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.mllib.export.pmml |
| 19 | + |
| 20 | +import org.apache.spark.annotation.DeveloperApi |
| 21 | +import org.apache.spark.mllib.export.ModelExportFactory |
| 22 | +import org.apache.spark.mllib.export.ModelExportType |
| 23 | +import org.apache.spark.mllib.regression.LassoModel |
| 24 | +import org.apache.spark.mllib.regression.LinearRegressionModel |
| 25 | +import org.apache.spark.mllib.regression.RidgeRegressionModel |
| 26 | +import org.apache.spark.mllib.util.LinearDataGenerator |
| 27 | +import org.scalatest.FunSuite |
| 28 | +import org.dmg.pmml.RegressionModel |
| 29 | + |
| 30 | +class GeneralizedLinearPMMLModelExportSuite extends FunSuite{ |
| 31 | + |
| 32 | + test("GeneralizedLinearPMMLModelExport generate PMML format") { |
| 33 | + |
| 34 | + //arrange models to test |
| 35 | + val linearInput = LinearDataGenerator.generateLinearInput( |
| 36 | + 3.0, Array(10.0, 10.0), 1, 17) |
| 37 | + val linearRegressionModel = new LinearRegressionModel(linearInput(0).features, linearInput(0).label); |
| 38 | + val ridgeRegressionModel = new RidgeRegressionModel(linearInput(0).features, linearInput(0).label); |
| 39 | + val lassoModel = new LassoModel(linearInput(0).features, linearInput(0).label); |
| 40 | + |
| 41 | + //act by exporting the model to the PMML format |
| 42 | + val linearModelExport = ModelExportFactory.createModelExport(linearRegressionModel, ModelExportType.PMML) |
| 43 | + //assert that the PMML format is as expected |
| 44 | + assert(linearModelExport.isInstanceOf[PMMLModelExport]) |
| 45 | + var pmml = linearModelExport.asInstanceOf[PMMLModelExport].getPmml() |
| 46 | + assert(pmml.getHeader().getDescription() === "linear regression") |
| 47 | + //check that the number of fields match the weights size |
| 48 | + assert(pmml.getDataDictionary().getNumberOfFields() === linearRegressionModel.weights.size) |
| 49 | + //this verify that there is a model attached to the pmml object and the model is a regression one |
| 50 | + //it also verifies that the pmml model has a regression table with the same number of predictors of the model weights |
| 51 | + assert(pmml.getModels().get(0).asInstanceOf[RegressionModel] |
| 52 | + .getRegressionTables().get(0).getNumericPredictors().size() === linearRegressionModel.weights.size) |
| 53 | + |
| 54 | + //act |
| 55 | + val ridgeModelExport = ModelExportFactory.createModelExport(ridgeRegressionModel, ModelExportType.PMML) |
| 56 | + //assert that the PMML format is as expected |
| 57 | + assert(ridgeModelExport.isInstanceOf[PMMLModelExport]) |
| 58 | + pmml = ridgeModelExport.asInstanceOf[PMMLModelExport].getPmml() |
| 59 | + assert(pmml.getHeader().getDescription() === "ridge regression") |
| 60 | + //check that the number of fields match the weights size |
| 61 | + assert(pmml.getDataDictionary().getNumberOfFields() === ridgeRegressionModel.weights.size) |
| 62 | + //this verify that there is a model attached to the pmml object and the model is a regression one |
| 63 | + //it also verifies that the pmml model has a regression table with the same number of predictors of the model weights |
| 64 | + assert(pmml.getModels().get(0).asInstanceOf[RegressionModel] |
| 65 | + .getRegressionTables().get(0).getNumericPredictors().size() === ridgeRegressionModel.weights.size) |
| 66 | + |
| 67 | + //act |
| 68 | + val lassoModelExport = ModelExportFactory.createModelExport(lassoModel, ModelExportType.PMML) |
| 69 | + //assert that the PMML format is as expected |
| 70 | + assert(lassoModelExport.isInstanceOf[PMMLModelExport]) |
| 71 | + pmml = lassoModelExport.asInstanceOf[PMMLModelExport].getPmml() |
| 72 | + assert(pmml.getHeader().getDescription() === "lasso regression") |
| 73 | + //check that the number of fields match the weights size |
| 74 | + assert(pmml.getDataDictionary().getNumberOfFields() === lassoModel.weights.size) |
| 75 | + //this verify that there is a model attached to the pmml object and the model is a regression one |
| 76 | + //it also verifies that the pmml model has a regression table with the same number of predictors of the model weights |
| 77 | + assert(pmml.getModels().get(0).asInstanceOf[RegressionModel] |
| 78 | + .getRegressionTables().get(0).getNumericPredictors().size() === lassoModel.weights.size) |
| 79 | + |
| 80 | + //manual checking |
| 81 | + //ModelExporter.toPMML(linearRegressionModel,"/tmp/linearregression.xml") |
| 82 | + //ModelExporter.toPMML(ridgeRegressionModel,"/tmp/ridgeregression.xml") |
| 83 | + //ModelExporter.toPMML(lassoModel,"/tmp/lassoregression.xml") |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments