2017-06-01 2 views
0

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

+0

Sag mir, wie es geht! – eliasah

+0

@eliasah bitte überprüfen Sie, was ich als bearbeitete Frage hinzufügen –

+0

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

Antwort

2

Sie die RFormula in diesem Fall verwenden können:

import org.apache.spark.ml.feature.{RFormula, PCA} 

val columnsToUse: Seq[String] = ??? 
val k: Int = ??? 

val df = spark.read.format("csv").options(Map("header" -> "true", "inferSchema" -> "true")).load("/tmp/foo.csv") 

val rf = new RFormula().setFormula(s"~ ${columnsToUse.mkString(" + ")}") 
val pca = new PCA().setInputCol("features").setK(k) 

val featurized = rf.fit(df).transform(df) 
val projected = pca.fit(featurized).transform(featurized) 
0

java.lang.NumberFormatException: For input string: "DateTime"

bedeutet dies, dass in Ihrer Eingabedatei DateTime ein Wert, den Sie dann in Double zu konvertieren versuchen.

Wahrscheinlich ist es irgendwo in der Kopfzeile Sie Eingabedatei

+0

Ja, ich weiß, was ich will ist Specefic Spalten aus der Eingabedatei auswählen, wie kann Ich habe eine bestimmte Spalten ausgewählt, danke. Jede Hilfe –

Verwandte Themen