zoukankan      html  css  js  c++  java
  • spark Mllib SVM实例

    Mllib SVM实例

    1、数据

    数据格式为:标签, 特征1 特征2 特征3……

    0 128:51 129:159 130:253 131:159 132:50 155:48 156:238 157:252 158:252 159:252 160:237 182:54 183:227 184:253 185:252 186:239 187:233 188:252 189:57 190:6 208:10 209:60 210:224 211:252 212:253 213:252 214:202 215:84 216:252 217:253 218:122 236:163 237:252 238:252 239:252 240:253 241:252 242:252 243:96 244:189 245:253 246:167 263:51 264:238 265:253 266:253 267:190 268:114 269:253 270:228 271:47 272:79 273:255 274:168 290:48 291:238 292:252 293:252 294:179 295:12 296:75 297:121 298:21 301:253 302:243 303:50 317:38 318:165 319:253 320:233 321:208 322:84 329:253 330:252 331:165 344:7 345:178 346:252 347:240 348:71 349:19 350:28 357:253 358:252 359:195 372:57 373:252 374:252 375:63 385:253 386:252 387:195 400:198 401:253 402:190 413:255 414:253 415:196 427:76 428:246 429:252 430:112 441:253 442:252 443:148 455:85 456:252 457:230 458:25 467:7 468:135 469:253 470:186 471:12 483:85 484:252 485:223 494:7 495:131 496:252 497:225 498:71 511:85 512:252 513:145 521:48 522:165 523:252 524:173 539:86 540:253 541:225 548:114 549:238 550:253 551:162 567:85 568:252 569:249 570:146 571:48 572:29 573:85 574:178 575:225 576:253 577:223 578:167 579:56 595:85 596:252 597:252 598:252 599:229 600:215 601:252 602:252 603:252 604:196 605:130 623:28 624:199 625:252 626:252 627:253 628:252 629:252 630:233 631:145 652:25 653:128 654:252 655:253 656:252 657:141 658:37

    1 159:124 160:253 161:255 162:63 186:96 187:244 188:251 189:253 190:62 214:127 215:251 216:251 217:253 218:62 241:68 242:236 243:251 244:211 245:31 246:8 268:60 269:228 270:251 271:251 272:94 296:155 297:253 298:253 299:189 323:20 324:253 325:251 326:235 327:66 350:32 351:205 352:253 353:251 354:126 378:104 379:251 380:253 381:184 382:15 405:80 406:240 407:251 408:193 409:23 432:32 433:253 434:253 435:253 436:159 460:151 461:251 462:251 463:251 464:39 487:48 488:221 489:251 490:251 491:172 515:234 516:251 517:251 518:196 519:12 543:253 544:251 545:251 546:89 570:159 571:255 572:253 573:253 574:31 597:48 598:228 599:253 600:247 601:140 602:8 625:64 626:251 627:253 628:220 653:64 654:251 655:253 656:220 681:24 682:193 683:253 684:220

    ……

    2、代码

     1 //1 读取样本数据
     2 
     3 val data_path = "/user/tmp/sample_libsvm_data.txt"
     4 
     5 val examples = MLUtils.loadLibSVMFile(sc, data_path).cache()
     6 
     7 //2 样本数据划分训练样本与测试样本
     8 
     9 val splits = examples.randomSplit(Array(0.6, 0.4), seed = 11L)
    10 
    11 val training = splits(0).cache()
    12 
    13 val test = splits(1)
    14 
    15 val numTraining = training.count()
    16 
    17 val numTest = test.count()
    18 
    19 println(s"Training: $numTraining, test: $numTest.")
    20 
    21 //3 新建SVM模型,并设置训练参数
    22 
    23 val numIterations = 1000
    24 
    25 val stepSize = 1
    26 
    27 val miniBatchFraction = 1.0
    28 
    29 val model = SVMWithSGD.train(training, numIterations, stepSize, miniBatchFraction)
    30 //4 对测试样本进行测试
    31 
    32 val prediction = model.predict(test.map(_.features))
    33 
    34 val predictionAndLabel = prediction.zip(test.map(_.label))
    35 
    36 //5 计算测试误差
    37 
    38 val metrics = new MulticlassMetrics(predictionAndLabel)
    39 
    40 val precision = metrics.precision
    41 
    42 println("Precision = " + precision)

    ------------------------------------我叫分隔线------------------------------------

      


    The following code snippet illustrates how to load a sample dataset, execute a training algorithm on this training data using a static method in the algorithm object, and make predictions with the resulting model to compute the training error.

     1 import org.apache.spark.SparkContext
     2 import org.apache.spark.mllib.classification.{SVMModel, SVMWithSGD}
     3 import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics
     4 import org.apache.spark.mllib.regression.LabeledPoint
     5 import org.apache.spark.mllib.linalg.Vectors
     6 import org.apache.spark.mllib.util.MLUtils
     7 
     8 // Load training data in LIBSVM format.
     9 val data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")
    10 
    11 // Split data into training (60%) and test (40%).
    12 val splits = data.randomSplit(Array(0.6, 0.4), seed = 11L)
    13 val training = splits(0).cache()
    14 val test = splits(1)
    15 
    16 // Run training algorithm to build the model
    17 val numIterations = 100
    18 val model = SVMWithSGD.train(training, numIterations)
    19 
    20 // Clear the default threshold.
    21 model.clearThreshold()
    22 
    23 // Compute raw scores on the test set.
    24 val scoreAndLabels = test.map { point =>
    25   val score = model.predict(point.features)
    26   (score, point.label)
    27 }
    28 
    29 // Get evaluation metrics.
    30 val metrics = new BinaryClassificationMetrics(scoreAndLabels)
    31 val auROC = metrics.areaUnderROC()
    32 
    33 println("Area under ROC = " + auROC)
    34 
    35 // Save and load model
    36 model.save(sc, "myModelPath")
    37 val sameModel = SVMModel.load(sc, "myModelPath")

    The SVMWithSGD.train() method by default performs L2 regularization with the regularization parameter set to 1.0. If we want to configure this algorithm, we can customize SVMWithSGD further by creating a new object directly and calling setter methods. All other MLlib algorithms support customization in this way as well. For example, the following code produces an L1 regularized variant of SVMs with regularization parameter set to 0.1, and runs the training algorithm for 200 iterations.

    1 import org.apache.spark.mllib.optimization.L1Updater
    2 
    3 val svmAlg = new SVMWithSGD()
    4 svmAlg.optimizer.
    5   setNumIterations(200).
    6   setRegParam(0.1).
    7   setUpdater(new L1Updater)
    8 val modelL1 = svmAlg.run(training)

    ------------------------------------我叫分隔线------------------------------------

    1.数据

    0 2.857738033247042 0 2.061393766919624 2.619965104088255 0 2.004684436494304 2.000347299268466 2.122974378789621 2.228387042742021 2.228387042742023 0 0 0 0 12.72816758217773 0

    1 2.857738033247042 0 2.061393766919624 2.619965104088255 0 2.004684436494304 2.000347299268466 2.122974378789621 2.228387042742021 2.228387042742023 0 0 12.72816758217773 0 0 0

    1 2.857738033247042 2.52078447201548 0 2.619965104088255 0 2.004684436494304 0 2.122974378789621 0 0 0 0 0 0 0 0

    2.代码

     1 import org.apache.spark.SparkContext 
     2 import org.apache.spark.mllib.classification.SVMWithSGD 
     3 import org.apache.spark.mllib.linalg.Vectors
     4 import org.apache.spark.mllib.regression.LabeledPoint  
     5 // Load and parse the data file 
     6 val data = sc.textFile("mllib/data/sample_svm_data.txt") 
     7 val parsedData = data.map { line =>
     8     val parts = line.split(' ')
     9     LabeledPoint(parts(0).toDouble, Vectors.dense(parts.tail.map(x => x.toDouble).toArray))
    10     }  
    11 // Run training algorithm to build the model 
    12 val numIterations = 20 
    13 val model = SVMWithSGD.train(parsedData, numIterations)  
    14 // Evaluate model on training examples and compute training error 
    15 val labelAndPreds = parsedData.map { point =>
    16     val prediction = model.predict(point.features)
    17     (point.label, prediction) } 
    18 val trainErr = labelAndPreds.filter( r => 
    19     r._1 != r._2).count.toDouble / parsedData.count
    20     println("Training Error = " + trainErr)
    21 
    22 file:///home/hadoop/suanec/suae/workspace/data/mllib/sample_svm_data.txt

  • 相关阅读:
    空类型指针实践
    参数作用域实践
    内联函数实践:有疑惑未解决
    可变参数实践
    默认参数实践
    函数指针实践
    Windows下开发环境搭建
    Test
    C++ 左值与右值
    如何打包成多个资源文件
  • 原文地址:https://www.cnblogs.com/suanec/p/4786725.html
Copyright © 2011-2022 走看看