Ich versuche zufällige Gesamtstrukturklassifizierung mit Spark ML api ausführen, aber ich habe Probleme mit der Erstellung von rechten Datenrahmen Eingabe in Pipeline. HierSo erstellen Sie den richtigen Datenrahmen für die Klassifizierung in Spark ML
ist Beispieldaten:
age,hours_per_week,education,sex,salaryRange
38,40,"hs-grad","male","A"
28,40,"bachelors","female","A"
52,45,"hs-grad","male","B"
31,50,"masters","female","B"
42,40,"bachelors","male","B"
Alter und hours_per_week ganze Zahlen sind, während andere Funktionen, einschließlich Label salaryRange kategorische sind (String)
Laden des CSV-Datei (nennen wir es sample.csv) kann von Spark csv library wie folgt ausgeführt werden:
val data = sqlContext.csvFile("/home/dusan/sample.csv")
Standardmäßig werden alle Spalten als String importiert werden, so müssen wir "Alter" und "hours_per_week" ändern Int:
val toInt = udf[Int, String](_.toInt)
val dataFixed = data.withColumn("age", toInt(data("age"))).withColumn("hours_per_week",toInt(data("hours_per_week")))
Nur um zu überprüfen, wie Schema sieht jetzt:
scala> dataFixed.printSchema
root
|-- age: integer (nullable = true)
|-- hours_per_week: integer (nullable = true)
|-- education: string (nullable = true)
|-- sex: string (nullable = true)
|-- salaryRange: string (nullable = true)
Dann Hiermit kann der Quer Validator und Pipeline gesetzt:
val rf = new RandomForestClassifier()
val pipeline = new Pipeline().setStages(Array(rf))
val cv = new CrossValidator().setNumFolds(10).setEstimator(pipeline).setEvaluator(new BinaryClassificationEvaluator)
Fehler zeigt sich, wenn diese Zeile ausgeführt wird:
val cmModel = cv.fit(dataFixed)
java.lang.IllegalArgumentException: Feld "Features" existiert nicht.
Es ist möglich, Label-Spalte und Feature-Spalte in RandomForestClassifier zu setzen, jedoch habe ich 4 Spalten als Prädiktoren (Features) nicht nur eins.
Wie sollte ich meinen Datenrahmen organisieren, damit die Spalten für Etiketten und Funktionen korrekt organisiert sind?
Für Ihre Bequemlichkeit hier ist voll Code:
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.ml.classification.RandomForestClassifier
import org.apache.spark.ml.evaluation.BinaryClassificationEvaluator
import org.apache.spark.ml.tuning.CrossValidator
import org.apache.spark.ml.Pipeline
import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.functions._
import org.apache.spark.mllib.linalg.{Vector, Vectors}
object SampleClassification {
def main(args: Array[String]): Unit = {
//set spark context
val conf = new SparkConf().setAppName("Simple Application").setMaster("local");
val sc = new SparkContext(conf)
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
import sqlContext.implicits._
import com.databricks.spark.csv._
//load data by using databricks "Spark CSV Library"
val data = sqlContext.csvFile("/home/dusan/sample.csv")
//by default all columns are imported as string so we need to change "age" and "hours_per_week" to Int
val toInt = udf[Int, String](_.toInt)
val dataFixed = data.withColumn("age", toInt(data("age"))).withColumn("hours_per_week",toInt(data("hours_per_week")))
val rf = new RandomForestClassifier()
val pipeline = new Pipeline().setStages(Array(rf))
val cv = new CrossValidator().setNumFolds(10).setEstimator(pipeline).setEvaluator(new BinaryClassificationEvaluator)
// this fails with error
//java.lang.IllegalArgumentException: Field "features" does not exist.
val cmModel = cv.fit(dataFixed)
}
}
Dank für die Hilfe!
Die Scala-Sprache ist nicht bekannt, aber wo werden Beschriftungen und Features aus dem Dataset wie LabeledPoint (Beschriftungen, Liste (Features)) festgelegt, überprüfen Sie das Beispiel in https://spark.apache.org/docs/latest/mllib -linear-methods.html –
@ABC, Bitte überprüfen Sie meinen Kommentar in der folgenden Frage. –
Überprüfen Sie dieses Beispiel https://github.com/apache/spark/blob/master/examples/src/main/scala/org/apache/spark/examples/ml/SimpleTextClassificationPipeline.scala, wobei val model = pipeline.fit (training .toDF()) benutzt den Dataframe in der Pipeline –