私は本から二分決定木についてのコードの一部をお読みください。これは、生データでは、フィールド(3)である唯一のカテゴリの特徴を有し、一の-K(ワンホットエンコード)に変換されます。意思決定ツリーで複数のカテゴリカル機能を扱うにはどうすればよいですか?
def PrepareData(sc: SparkContext): (RDD[LabeledPoint], RDD[LabeledPoint], RDD[LabeledPoint], Map[String, Int]) = {
val rawDataWithHeader = sc.textFile("data/train.tsv")
val rawData = rawDataWithHeader.mapPartitionsWithIndex { (idx, iter) => if (idx == 0) iter.drop(1) else iter }
val lines = rawData.map(_.split("\t"))
val categoriesMap = lines.map(fields => fields(3)).distinct.collect.zipWithIndex.toMap
val labelpointRDD = lines.map { fields =>
val trFields = fields.map(_.replaceAll("\"", ""))
val categoryFeaturesArray = Array.ofDim[Double](categoriesMap.size)
val categoryIdx = categoriesMap(fields(3))
categoryFeaturesArray(categoryIdx) = 1
val numericalFeatures = trFields.slice(4, fields.size - 1).map(d => if (d == "?") 0.0 else d.toDouble)
val label = trFields(fields.size - 1).toInt
LabeledPoint(label, Vectors.dense(categoryFeaturesArray ++ numericalFeatures))
}
val Array(trainData, validationData, testData) = labelpointRDD.randomSplit(Array(8, 1, 1))
return (trainData, validationData, testData, categoriesMap)
}
私はいくつかのカテゴリの特徴は、生データに存在する場合、のは(3)フィールドを言わせて、コードを修正する方法を疑問に思う、フィールド(5)、フィールド(7)は、すべてのカテゴリ機能です。
Iは最初の行改訂:そして
def PrepareData(sc: SparkContext): (RDD[LabeledPoint], RDD[LabeledPoint], RDD[LabeledPoint], Map[String, Int], Map[String, Int], Map[String, Int], Map[String, Int]) =......
を、それが同様に行われたように、Iは、1の-Kのエンコードに別の2つのフィールドが変換:
val categoriesMap5 = lines.map(fields => fields(5)).distinct.collect.zipWithIndex.toMap
val categoriesMap7 = lines.map(fields => fields(7)).distinct.collect.zipWithIndex.toMap
val categoryFeaturesArray5 = Array.ofDim[Double](categoriesMap5.size)
val categoryFeaturesArray7 = Array.ofDim[Double](categoriesMap7.size)
val categoryIdx3 = categoriesMap5(fields(5))
val categoryIdx5 = categoriesMap7(fields(7))
categoryFeaturesArray5(categoryIdx5) = 1
categoryFeaturesArray7(categoryIdx7) = 1
最後に、私はLabeledPointを改訂返信のように:
LabeledPoint(label, Vectors.dense(categoryFeaturesArray ++ categoryFeaturesArray5 ++ categoryFeaturesArray7 ++ numericalFeatures))
return (trainData, validationData, testData, categoriesMap, categoriesMap5, categoriesMap7)
正しいですか?
============================================== ====私が遭遇した
第二の問題がある:その本から、次のコード、trainModelで、それはここで
DecisionTree.trainRegressor(trainingData, categoricalFeaturesInfo, impurity, maxDepth, maxBins)
を使用するコードは次のとおりです。
def trainModel(trainData: RDD[LabeledPoint], impurity: String, maxDepth: Int, maxBins: Int): (DecisionTreeModel, Double) = {
val startTime = new DateTime()
val model = DecisionTree.trainClassifier(trainData, 2, Map[Int, Int](), impurity, maxDepth, maxBins)
val endTime = new DateTime()
val duration = new Duration(startTime, endTime)
(model, duration.getMillis())
}
質問がされますCategoricalFeaturesInfoに前述の3つのカテゴリの機能がある場合、このメソッドにどのように渡しますか?
私は、決定木を使って、自分で予測システムを構築するための本の手順に従ってください。 性別:男性、女性
学歴:HS-大学院、学士、修士、博士、......
具体的には、私が選んだのデータセットは、のようないくつかのカテゴリ機能を備えています国:米国、カナダ、イングランド、オーストラリア、......
しかし、私はDecisionTree.trainRegressor()
ハム...私はちょうど決定木を使って自分自身で予測システムを構築するために、本のステップに従ってみたいです。 より具体的には、私が選んだデータセットには、次のようないくつかのカテゴリ機能があります。 性別:男性、女性; 教育:HS-grad、Bachelors、Master、PH.D、......; 国:米国、カナダ、イングランド、オーストラリア、......; ...など。 しかし、 "Vector.dense()"に入れる "categoryFeatures ++ numericalFeatures"と "DecisionTree.trainRegressor()"に入れる単一の "categoricalFeaturesInfo"にそれらをマージする方法はわかりません –
もしMLパイプラインを使用すると、エンコーダ、インデクサ、アセンブラなど、必要なすべてのツールを手に入れることができます。 – zero323
@ C.Y.Wu私はzero323と結びついていますが、もし私が本のタイトルは何かを聞くことができるでしょうか?私はそれを見てみたいと思います。 – eliasah