當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python pyspark Interaction用法及代碼示例

本文簡要介紹 pyspark.ml.feature.Interaction 的用法。

用法:

class pyspark.ml.feature.Interaction(*, inputCols=None, outputCol=None)

實現特征交互變換。該轉換器采用 Double 和 Vector 類型的列,並輸出它們的特征交互的扁平向量。為了處理交互,我們首先 one-hot 編碼任何名義特征。然後,生成特征cross-products的向量。

例如,給定輸入特征值 Double(2)Vector(3, 4) ,如果所有輸入特征都是數字,則輸出將為 Vector(6, 8)。如果第一個特征是具有四個類別的名義特征,則輸出將為 Vector(0, 0, 0, 0, 3, 4, 0, 0)

3.0.0 版中的新函數。

例子

>>> df = spark.createDataFrame([(0.0, 1.0), (2.0, 3.0)], ["a", "b"])
>>> interaction = Interaction()
>>> interaction.setInputCols(["a", "b"])
Interaction...
>>> interaction.setOutputCol("ab")
Interaction...
>>> interaction.transform(df).show()
+---+---+-----+
|  a|  b|   ab|
+---+---+-----+
|0.0|1.0|[0.0]|
|2.0|3.0|[6.0]|
+---+---+-----+
...
>>> interactionPath = temp_path + "/interaction"
>>> interaction.save(interactionPath)
>>> loadedInteraction = Interaction.load(interactionPath)
>>> loadedInteraction.transform(df).head().ab == interaction.transform(df).head().ab
True

相關用法


注:本文由純淨天空篩選整理自spark.apache.org大神的英文原創作品 pyspark.ml.feature.Interaction。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。