|
| 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.api.python |
| 19 | + |
| 20 | +import org.scalatest.FunSuite |
| 21 | + |
| 22 | +import org.apache.spark.mllib.linalg.Vectors |
| 23 | +import org.apache.spark.mllib.regression.LabeledPoint |
| 24 | + |
| 25 | +class PythonMLLibAPISuite extends FunSuite { |
| 26 | + val py = new PythonMLLibAPI |
| 27 | + |
| 28 | + test("vector serialization") { |
| 29 | + val vectors = Seq( |
| 30 | + Vectors.dense(Array.empty[Double]), |
| 31 | + Vectors.dense(0.0), |
| 32 | + Vectors.dense(0.0, -2.0), |
| 33 | + Vectors.sparse(0, Array.empty[Int], Array.empty[Double]), |
| 34 | + Vectors.sparse(1, Array.empty[Int], Array.empty[Double]), |
| 35 | + Vectors.sparse(2, Array(1), Array(-2.0))) |
| 36 | + vectors.foreach { v => |
| 37 | + val bytes = py.serializeDoubleVector(v) |
| 38 | + val u = py.deserializeDoubleVector(bytes) |
| 39 | + assert(u.getClass === v.getClass) |
| 40 | + assert(u === v) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + test("labeled point serialization") { |
| 45 | + val points = Seq( |
| 46 | + LabeledPoint(0.0, Vectors.dense(Array.empty[Double])), |
| 47 | + LabeledPoint(1.0, Vectors.dense(0.0)), |
| 48 | + LabeledPoint(-0.5, Vectors.dense(0.0, -2.0)), |
| 49 | + LabeledPoint(0.0, Vectors.sparse(0, Array.empty[Int], Array.empty[Double])), |
| 50 | + LabeledPoint(1.0, Vectors.sparse(1, Array.empty[Int], Array.empty[Double])), |
| 51 | + LabeledPoint(-0.5, Vectors.sparse(2, Array(1), Array(-2.0)))) |
| 52 | + points.foreach { p => |
| 53 | + val bytes = py.serializeLabeledPoint(p) |
| 54 | + val q = py.deserializeLabeledPoint(bytes) |
| 55 | + assert(q.label === p.label) |
| 56 | + assert(q.features.getClass === p.features.getClass) |
| 57 | + assert(q.features === p.features) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments