|
| 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 com.intel.hibench.sparkbench.ml |
| 19 | + |
| 20 | +import org.apache.spark.{SparkConf, SparkContext} |
| 21 | +import org.apache.spark.mllib.classification.{SVMModel, SVMWithSGD} |
| 22 | +import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics |
| 23 | +import org.apache.spark.rdd.RDD |
| 24 | +import org.apache.spark.mllib.regression.LabeledPoint |
| 25 | + |
| 26 | +import scopt.OptionParser |
| 27 | + |
| 28 | +object SVMWithSGD { |
| 29 | + |
| 30 | + case class Params( |
| 31 | + numIterations: Int = 100, |
| 32 | + stepSize: Double = 1.0, |
| 33 | + regParam: Double = 0.01, |
| 34 | + dataPath: String = null |
| 35 | + ) |
| 36 | + |
| 37 | + def main(args: Array[String]): Unit = { |
| 38 | + val defaultParams = Params() |
| 39 | + |
| 40 | + val parser = new OptionParser[Params]("SVM") { |
| 41 | + head("SVM: an example of SVM for classification.") |
| 42 | + opt[Int]("numIterations") |
| 43 | + .text(s"numIterations, default: ${defaultParams.numIterations}") |
| 44 | + .action((x,c) => c.copy(numIterations = x)) |
| 45 | + opt[Double]("stepSize") |
| 46 | + .text(s"stepSize, default: ${defaultParams.stepSize}") |
| 47 | + .action((x,c) => c.copy(stepSize = x)) |
| 48 | + opt[Double]("regParam") |
| 49 | + .text(s"regParam, default: ${defaultParams.regParam}") |
| 50 | + .action((x,c) => c.copy(regParam = x)) |
| 51 | + arg[String]("<dataPath>") |
| 52 | + .required() |
| 53 | + .text("data path of SVM") |
| 54 | + .action((x, c) => c.copy(dataPath = x)) |
| 55 | + } |
| 56 | + parser.parse(args, defaultParams) match { |
| 57 | + case Some(params) => run(params) |
| 58 | + case _ => sys.exit(1) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + def run(params: Params): Unit = { |
| 63 | + |
| 64 | + val conf = new SparkConf().setAppName(s"SVM with $params") |
| 65 | + val sc = new SparkContext(conf) |
| 66 | + |
| 67 | + val dataPath = params.dataPath |
| 68 | + val numIterations = params.numIterations |
| 69 | + val stepSize = params.stepSize |
| 70 | + val regParam = params.regParam |
| 71 | + |
| 72 | + val data: RDD[LabeledPoint] = sc.objectFile(dataPath) |
| 73 | + |
| 74 | + // Split data into training (60%) and test (40%). |
| 75 | + val splits = data.randomSplit(Array(0.6, 0.4), seed = 11L) |
| 76 | + val training = splits(0).cache() |
| 77 | + val test = splits(1) |
| 78 | + |
| 79 | + // Run training algorithm to build the model |
| 80 | + val model = SVMWithSGD.train(training, numIterations, stepSize, regParam) |
| 81 | + |
| 82 | + // Clear the default threshold. |
| 83 | + model.clearThreshold() |
| 84 | + |
| 85 | + // Compute raw scores on the test set. |
| 86 | + val scoreAndLabels = test.map { point => |
| 87 | + val score = model.predict(point.features) |
| 88 | + (score, point.label) |
| 89 | + } |
| 90 | + |
| 91 | + // Get evaluation metrics. |
| 92 | + val metrics = new BinaryClassificationMetrics(scoreAndLabels) |
| 93 | + val auROC = metrics.areaUnderROC() |
| 94 | + |
| 95 | + println("Area under ROC = " + auROC) |
| 96 | + |
| 97 | + sc.stop() |
| 98 | + } |
| 99 | +} |
0 commit comments