回答我自己的问题可能被认为是不好的做法,但我会在一段时间内保留我的问题(现在回答),看看它是否对其他人有用并且有用。
我理解如何在数据上使用模式,我可以为所有功能创建相应的模式属性。我原本想要处理每个向量中包含200多个特征值的数据集。必须为所有200个特征声明包含列属性的静态模式,这使得使用它变得不切实际。但是,可能有一种更动态的方式来创建模式,我还没有找到它。我决定在Iris.csv数据集上测试我的代码。这里的文件包含行属性;
Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species
哪个将作为架构实现:
val schema: Schema = new Schema.Builder() .addColumnInteger("Id") .addColumnDouble("SepalLengthCm") .addColumnDouble("SepalWidthCm") .addColumnDouble("PetalLengthCm") .addColumnDouble("PetalWidthCm") .addColumnString("Species") .build
我觉得使用模式的动机之一就是能够转换数据。因此,我想执行转换操作。 TransformProcess定义了一系列要对我们的数据执行的操作(使用DataVec附录F第405页DeepLearning:A从业者方法)。
A TransformProcess is constructed by specifying two things: 鈥� The Schema of the initial input data 鈥� The set of operations we wish to execute Using DataVec
我决定看看是否可以从读取数据中删除一列:
val process = new TransformProcess.Builder(schema) .removeColumns("Id") .build()
因此,我的代码成了:
import org.datavec.api.records.reader.impl.csv.CSVRecordReader import org.datavec.api.transform.{DataAction, TransformProcess} import org.datavec.api.transform.schema.Schema import java.io.File import org.apache.spark.api.java.JavaSparkContext import org.datavec.spark.transform.misc.StringToWritablesFunction import org.apache.spark.SparkConf import org.datavec.api.split.FileSplit import org.datavec.spark.transform.SparkTransformExecutor object S extends App{ val schema: Schema = new Schema.Builder() .addColumnInteger("Id") .addColumnDouble("SepalLengthCm") .addColumnDouble("SepalWidthCm") .addColumnDouble("PetalLengthCm") .addColumnDouble("PetalWidthCm") .addColumnString("Species") .build val recordReader = new CSVRecordReader(0, ",") val f = new File("./src/main/resources/Iris.csv") recordReader.initialize(new FileSplit(f)) println(recordReader.next()) val sparkConf:SparkConf = new SparkConf() sparkConf.setMaster("local[*]"); sparkConf.setAppName("DataVec Example"); val sc:JavaSparkContext = new JavaSparkContext(sparkConf) val lines = sc.textFile(f.getAbsolutePath); val examples = lines.map(new StringToWritablesFunction(new CSVRecordReader())) val process = new TransformProcess.Builder(schema) .removeColumns("Id") .build() val executor = new SparkTransformExecutor() val processed = executor.execute(examples, process) println(processed.first()) }
第一次印刷:
[Id, SepalLengthCm, SepalWidthCm, PetalLengthCm, PetalWidthCm, Species]
第二次打印
[SepalLengthCm, SepalWidthCm, PetalLengthCm, PetalWidthCm, Species]
编辑:我看到我遇到了崩溃 “org.deeplearning4j”%“deeplearning4j-core”%“0.6.0”作为我的libraryDependency
虽然有一个旧的依赖它有效
“org.deeplearning4j”%“deeplearning4j-core”%“0.0.3.2.7”
libraryDependencies ++= Seq( "org.datavec" % "datavec-spark_2.11" % "0.5.0", "org.datavec" % "datavec-api" % "0.5.0", "org.deeplearning4j" % "deeplearning4j-core" % "0.0.3.2.7" //"org.deeplearning4j" % "deeplearning4j-core" % "0.6.0" )