特征工程对数据进行处理
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")
}
}