zoukankan      html  css  js  c++  java
  • spark机器学习

    多层感知器(MLP)

     1 from __future__ import print_function
     2 from pyspark.ml.classification import MultilayerPerceptronClassifier
     3 from pyspark.ml.evaluation import MulticlassClassificationEvaluator
     4 from pyspark.sql import SparkSession
     5 
     6 spark = SparkSession
     7     .builder.appName("multilayer_perceptron_classification_example").getOrCreate()
     8 
     9 # 加载数据
    10 data = spark.read.format("libsvm")
    11     .load("data/mllib/sample_multiclass_classification_data.txt")
    12 
    13 # 切分训练集和测试集
    14 splits = data.randomSplit([0.6, 0.4], 1234)
    15 train = splits[0]
    16 test = splits[1]
    17 
    18 # 输入、隐层、隐层、输出个数
    19 layers = [4, 5, 4, 3]
    20 
    21 # 创建多层感知器
    22 trainer = MultilayerPerceptronClassifier(maxIter=100, layers=layers, blockSize=128, seed=1234)
    23 
    24 # 训练模型
    25 model = trainer.fit(train)
    26 
    27 # 预测和计算准确度
    28 result = model.transform(test)
    29 result.show()
    30 predictionAndLabels = result.select("prediction", "label")
    31 evaluator = MulticlassClassificationEvaluator(metricName="accuracy")
    32 print("Test set accuracy = " + str(evaluator.evaluate(predictionAndLabels)))
    33 
    34 spark.stop()
    +-----+--------------------+----------+
    |label|            features|prediction|
    +-----+--------------------+----------+
    |  0.0|(4,[0,1,2,3],[-0....|       2.0|
    |  0.0|(4,[0,1,2,3],[-0....|       0.0|
    |  0.0|(4,[0,1,2,3],[-0....|       0.0|
    |  0.0|(4,[0,1,2,3],[-0....|       2.0|
    |  0.0|(4,[0,1,2,3],[-0....|       2.0|
    |  0.0|(4,[0,1,2,3],[-1....|       2.0|
    |  0.0|(4,[0,1,2,3],[0.1...|       0.0|
    |  0.0|(4,[0,1,2,3],[0.2...|       0.0|
    |  0.0|(4,[0,1,2,3],[0.3...|       0.0|
    |  0.0|(4,[0,1,2,3],[0.3...|       0.0|
    |  0.0|(4,[0,1,2,3],[0.3...|       0.0|
    |  0.0|(4,[0,1,2,3],[0.4...|       0.0|
    |  0.0|(4,[0,1,2,3],[0.5...|       0.0|
    |  0.0|(4,[0,1,2,3],[0.7...|       0.0|
    |  0.0|(4,[0,1,2,3],[0.8...|       0.0|
    |  0.0|(4,[0,1,2,3],[1.0...|       0.0|
    |  0.0|(4,[0,2,3],[0.166...|       0.0|
    |  0.0|(4,[0,2,3],[0.388...|       0.0|
    |  1.0|(4,[0,1,2,3],[-0....|       1.0|
    |  1.0|(4,[0,1,2,3],[-0....|       1.0|
    +-----+--------------------+----------+
    only showing top 20 rows
    
    Test set accuracy = 0.901960784314
  • 相关阅读:
    UOJ222 【NOI2016】区间
    BZOJ3631 [JLOI2014]松鼠的新家
    BZOJ 1001 [BeiJing2006]狼抓兔子
    poj2488 A Knight's Journey裸dfs
    hdu 4289 网络流拆点,类似最小割(可做模板)邻接矩阵实现
    hdu 4183 EK最大流算法
    HDU 4180 扩展欧几里得
    HDU 4178 模拟
    HDU 4177 模拟时间问题
    hdu 4185 二分图最大匹配
  • 原文地址:https://www.cnblogs.com/luozeng/p/9054475.html
Copyright © 2011-2022 走看看