栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

9.5.3、Spark Mllib

9.5.3、Spark Mllib

特征工程对数据进行处理
ct Demo05Rando {

  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession
      .builder()
      .master("local[4]")
      .appName("Demo2Person")
      .getOrCreate()

    import spark.implicits._
    import org.apache.spark.sql.functions._

    
    val imageDF: Dataframe = spark.read
      .format("image")
      .load("C:\Users\ASUSDCC\Desktop\train")
    //打印数据结构
    imageDF.printSchema()

    //第2层的数据通过.取出来
    val image_data: Dataframe = imageDF
      .select($"image.origin", $"image.data")
      .as[(String, Array[Byte])]
      .map {
        case (filePath: String, bytes: Array[Byte]) => {
          val ints: Array[Int] = bytes.map(_.toInt) //图片数据为16进制,转成二进制数据
          val doubless: Array[Double] = ints.map(i => {
           //将数据进行转化,使用0与1进行表示
            if (i >= 0) {
              0.0
            } else {
              1.0
            }
          })
          val filename: String = filePath.split("/").last //获取图片名的最后一部分,用于关联数字
          (filename, doubless)
        }
      }.toDF("filename", "features") //列名

    //读取图片名称和数字的数据
    val labelDF: Dataframe = spark.read
      .format("csv")
      .option("sep", " ")
      .schema("filename String,lable Double")
      .load("sparkproject/data/image_res.txt")

    //将数字与图片关联
    image_Data
      .join(labelDF, "filename")
      .select($"lable", $"features")
      .write
      .mode(SaveMode.Overwrite)
      .format("libsvm")
      .save("sparkproject/data/images")

  }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/671500.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号