|
| 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.recommendation |
| 19 | + |
| 20 | +import org.apache.spark.mllib.util.MLlibTestSparkContext |
| 21 | +import org.apache.spark.rdd.RDD |
| 22 | +import org.scalatest.FunSuite |
| 23 | +import org.apache.spark.mllib.util.TestingUtils._ |
| 24 | + |
| 25 | + |
| 26 | +class MatrixFactorizationModelSuite extends FunSuite with MLlibTestSparkContext { |
| 27 | + |
| 28 | + val rank = 2 |
| 29 | + var userFeatures: RDD[(Int, Array[Double])] = _ |
| 30 | + var prodFeatures: RDD[(Int, Array[Double])] = _ |
| 31 | + |
| 32 | + override def beforeAll(): Unit = { |
| 33 | + super.beforeAll() |
| 34 | + userFeatures = sc.parallelize(Seq((0, Array(1.0, 2.0)), (1, Array(3.0, 4.0)))) |
| 35 | + prodFeatures = sc.parallelize(Seq((2, Array(5.0, 6.0)))) |
| 36 | + } |
| 37 | + |
| 38 | + test("constructor") { |
| 39 | + val model = new MatrixFactorizationModel(rank, userFeatures, prodFeatures) |
| 40 | + assert(model.predict(0, 2) ~== 17.0 relTol 1e-14) |
| 41 | + |
| 42 | + intercept[IllegalArgumentException] { |
| 43 | + new MatrixFactorizationModel(1, userFeatures, prodFeatures) |
| 44 | + } |
| 45 | + |
| 46 | + val userFeatures1 = sc.parallelize(Seq((0, Array(1.0)), (1, Array(3.0)))) |
| 47 | + intercept[IllegalArgumentException] { |
| 48 | + new MatrixFactorizationModel(rank, userFeatures1, prodFeatures) |
| 49 | + } |
| 50 | + |
| 51 | + val prodFeatures1 = sc.parallelize(Seq((2, Array(5.0)))) |
| 52 | + intercept[IllegalArgumentException] { |
| 53 | + new MatrixFactorizationModel(rank, userFeatures, prodFeatures1) |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments