|
| 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 | +import os |
| 19 | +import tempfile |
| 20 | +import unittest |
| 21 | + |
| 22 | +import numpy as np |
| 23 | + |
| 24 | +from pyspark.ml.linalg import Vectors |
| 25 | +from pyspark.ml.classification import ( |
| 26 | + LinearSVC, |
| 27 | + LinearSVCModel, |
| 28 | + OneVsRest, |
| 29 | + OneVsRestModel, |
| 30 | +) |
| 31 | +from pyspark.testing.sqlutils import ReusedSQLTestCase |
| 32 | + |
| 33 | + |
| 34 | +class OneVsRestTestsMixin: |
| 35 | + def test_one_vs_rest(self): |
| 36 | + spark = self.spark |
| 37 | + df = ( |
| 38 | + spark.createDataFrame( |
| 39 | + [ |
| 40 | + (0, 1.0, Vectors.dense(0.0, 5.0)), |
| 41 | + (1, 0.0, Vectors.dense(1.0, 2.0)), |
| 42 | + (2, 1.0, Vectors.dense(2.0, 1.0)), |
| 43 | + (3, 2.0, Vectors.dense(3.0, 3.0)), |
| 44 | + ], |
| 45 | + ["index", "label", "features"], |
| 46 | + ) |
| 47 | + .coalesce(1) |
| 48 | + .sortWithinPartitions("index") |
| 49 | + .select("label", "features") |
| 50 | + ) |
| 51 | + |
| 52 | + svc = LinearSVC(maxIter=1, regParam=1.0) |
| 53 | + self.assertEqual(svc.getMaxIter(), 1) |
| 54 | + self.assertEqual(svc.getRegParam(), 1.0) |
| 55 | + |
| 56 | + ovr = OneVsRest(classifier=svc, parallelism=1) |
| 57 | + self.assertEqual(ovr.getParallelism(), 1) |
| 58 | + |
| 59 | + model = ovr.fit(df) |
| 60 | + self.assertIsInstance(model, OneVsRestModel) |
| 61 | + self.assertEqual(len(model.models), 3) |
| 62 | + for submodel in model.models: |
| 63 | + self.assertIsInstance(submodel, LinearSVCModel) |
| 64 | + |
| 65 | + self.assertTrue( |
| 66 | + np.allclose(model.models[0].intercept, 0.06279247869226989, atol=1e-4), |
| 67 | + model.models[0].intercept, |
| 68 | + ) |
| 69 | + self.assertTrue( |
| 70 | + np.allclose( |
| 71 | + model.models[0].coefficients.toArray(), |
| 72 | + [-0.1198765502306968, -0.1027513287691687], |
| 73 | + atol=1e-4, |
| 74 | + ), |
| 75 | + model.models[0].coefficients, |
| 76 | + ) |
| 77 | + |
| 78 | + self.assertTrue( |
| 79 | + np.allclose(model.models[1].intercept, 0.025877458475338313, atol=1e-4), |
| 80 | + model.models[1].intercept, |
| 81 | + ) |
| 82 | + self.assertTrue( |
| 83 | + np.allclose( |
| 84 | + model.models[1].coefficients.toArray(), |
| 85 | + [-0.0362284418654736, 0.010350983390135305], |
| 86 | + atol=1e-4, |
| 87 | + ), |
| 88 | + model.models[1].coefficients, |
| 89 | + ) |
| 90 | + |
| 91 | + self.assertTrue( |
| 92 | + np.allclose(model.models[2].intercept, -0.37024065419409624, atol=1e-4), |
| 93 | + model.models[2].intercept, |
| 94 | + ) |
| 95 | + self.assertTrue( |
| 96 | + np.allclose( |
| 97 | + model.models[2].coefficients.toArray(), |
| 98 | + [0.12886829400126, 0.012273170857262873], |
| 99 | + atol=1e-4, |
| 100 | + ), |
| 101 | + model.models[2].coefficients, |
| 102 | + ) |
| 103 | + |
| 104 | + output = model.transform(df) |
| 105 | + expected_cols = ["label", "features", "rawPrediction", "prediction"] |
| 106 | + self.assertEqual(output.columns, expected_cols) |
| 107 | + self.assertEqual(output.count(), 4) |
| 108 | + |
| 109 | + # Model save & load |
| 110 | + with tempfile.TemporaryDirectory(prefix="linear_svc") as d: |
| 111 | + path1 = os.path.join(d, "ovr") |
| 112 | + ovr.write().overwrite().save(path1) |
| 113 | + ovr2 = OneVsRest.load(path1) |
| 114 | + self.assertEqual(str(ovr), str(ovr2)) |
| 115 | + |
| 116 | + path2 = os.path.join(d, "ovr_model") |
| 117 | + model.write().overwrite().save(path2) |
| 118 | + model2 = OneVsRestModel.load(path2) |
| 119 | + self.assertEqual(str(model), str(model2)) |
| 120 | + |
| 121 | + |
| 122 | +class OneVsRestTests(OneVsRestTestsMixin, ReusedSQLTestCase): |
| 123 | + pass |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + from pyspark.ml.tests.test_ovr import * # noqa: F401,F403 |
| 128 | + |
| 129 | + try: |
| 130 | + import xmlrunner # type: ignore[import] |
| 131 | + |
| 132 | + testRunner = xmlrunner.XMLTestRunner(output="target/test-reports", verbosity=2) |
| 133 | + except ImportError: |
| 134 | + testRunner = None |
| 135 | + unittest.main(testRunner=testRunner, verbosity=2) |
0 commit comments