Ich versuche PCA auf ein Dataset anzuwenden, das eine Kopfzeile enthält und Felder enthält Hier ist der Code, den ich verwendet habe, jede Hilfe in der Lage, eine bestimmte Spalten zu wählen, auf die wir PCA anwenden .Anwenden von PCA auf bestimmte Spalten mit Apache Spark
val inputMatrix = sc.textFile("C:/Users/mhattabi/Desktop/Realase of 01_06_2017/TopDrive_WithoutConstant.csv").map { line =>
val values = line.split(",").map(_.toDouble)
Vectors.dense(values)
}
val mat: RowMatrix = new RowMatrix(inputMatrix)
val pc: Matrix = mat.computePrincipalComponents(4)
// Project the rows to the linear space spanned by the top 4 principal components.
val projected: RowMatrix = mat.multiply(pc)
// Version aktualisiert Ich habe versucht, diese
val spark = SparkSession.builder.master("local").appName("my-spark-app").getOrCreate()
val dataframe = spark.read.format("com.databricks.spark.csv")
val columnsToUse: Seq[String] = Array("Col0","Col1", "Col2", "Col3", "Col4").toSeq
val k: Int = 2
val df = spark.read.format("csv").options(Map("header" -> "true", "inferSchema" -> "true")).load("C:/Users/mhattabi/Desktop/donnee/cassandraTest_1.csv")
val rf = new RFormula().setFormula(s"~ ${columnsToUse.mkString(" + ")}")
val pca = new PCA().setInputCol("features").setOutputCol("pcaFeatures").setK(k)
val featurized = rf.fit(df).transform(df)
//prinpal component
val principalComponent = pca.fit(featurized).transform(featurized)
principalComponent.select("pcaFeatures").show(4,false)
+-----------------------------------------+
|pcaFeatures |
+-----------------------------------------+
|[-0.536798281241379,0.495499034754084] |
|[-0.32969328815797916,0.5672811417154808]|
|[-1.32283465170085,0.5982789033642704] |
|[-0.6199718696225502,0.3173072633712586] |
+-----------------------------------------+
ich dies für pricipal Komponente zu erledigen, die Frage, die ich diese Datei in csv speichern möchten, und fügen Sie header.Any vielen Dank helfen Jede Hilfe wäre willkommen.
Vielen Dank
Sag mir, wie es geht! – eliasah
@eliasah bitte überprüfen Sie, was ich als bearbeitete Frage hinzufügen –
Ich bin mir nicht sicher, dass ich das Update bekomme ... Es scheint wie eine neue Frage für mich und kein Update. Wenn ja, bitte akzeptieren Sie die Antwort und stellen Sie eine neue Frage – eliasah