擷取特徵

使用 PySpark 進行特徵工程

John Hogue

Lead Data Scientist, General Mills

用文字比對擷取年限

ROOF
Asphalt Shingles, Pitched, Age 8 Years or Less
Asphalt Shingles, Age Over 8 Years
Asphalt Shingles, Age 8 Years or Less
Asphalt Shingles

Roof_Age 轉為 Roof>8yrs
Age 8 Years or Less 0
Age Over 8 Years 1
Age 8 Years or Less 0
NULL NULL

使用 PySpark 進行特徵工程

用文字比對擷取年限

from pyspark.sql.functions import when

# 建立布林篩選條件 find_under_8 = df['ROOF'].like('%Age 8 Years or Less%') find_over_8 = df['ROOF'].like('%Age Over 8 Years%')
# 用 when() 和 otherwise() 套用篩選 df = df.withColumn('old_roof', (when(find_over_8, 1) .when(find_under_8, 0) .otherwise(None)))
# 檢視結果 df[['ROOF', 'old_roof']].show(3, truncate=100)
+----------------------------------------------+--------+
|                                          ROOF|old_roof|
+----------------------------------------------+--------+
|                                          null|    null|
|Asphalt Shingles, Pitched, Age 8 Years or Less|       0|
|            Asphalt Shingles, Age Over 8 Years|       1|
+----------------------------------------------+--------+
only showing top 3 rows
使用 PySpark 進行特徵工程

分割欄位

ROOF 轉為 Roof_Material
Asphalt Shingles, Pitched, Age 8 Years or Less Asphalt Shingles
Null
Asphalt Shingles, Age Over 8 Years Asphalt Shingles
Metal, Age 8 Years or Less Metal
Tile, Age 8 Years or Less Tile
Asphalt Shingles Asphalt Shingles
使用 PySpark 進行特徵工程

分割欄位

from pyspark.sql.functions import split

# 依逗號分割欄位為清單 split_col = split(df['ROOF'], ',')
# 取清單第 1 個值放入新欄位 df = df.withColumn('Roof_Material', split_col.getItem(0))
# 檢視結果 df[['ROOF', 'Roof_Material']].show(5, truncate=100)
+----------------------------------------------+----------------+
|                                          ROOF|   Roof_Material|
+----------------------------------------------+----------------+
|                                          null|            null|
|Asphalt Shingles, Pitched, Age 8 Years or Less|Asphalt Shingles|
|                                          null|            null|
|Asphalt Shingles, Pitched, Age 8 Years or Less|Asphalt Shingles|
|            Asphalt Shingles, Age Over 8 Years|Asphalt Shingles|
+----------------------------------------------+----------------+
only showing top 5 rows
使用 PySpark 進行特徵工程

Explode!

起始紀錄

NO roof_list
2 [Asphalt Shingles, Pitched, Age 8 Years or Less]

展開後的紀錄

NO ex_roof_list
2 Asphalt Shingles
2 Pitched
2 Age 8 Years or Less
使用 PySpark 進行特徵工程

Pivot!

展開後的紀錄

NO ex_roof_list
2 Asphalt Shingles
2 Pitched
2 Age 8 Years or Less

樞紐分析後的紀錄

NO Age 8 Years or Less Age Over 8 Years Asphalt Shingles Flat Metal Other Pitched ...
2 0 1 1 0 0 0 1 ...
使用 PySpark 進行特徵工程

Explode 與 Pivot!

from pyspark.sql.functions import split, explode, lit, coalesce, first
# 依逗號分割欄位為清單
df = df.withColumn('roof_list', split(df['ROOF'], ', '))
# 將清單展開為多筆紀錄(每個值一筆)
ex_df = df.withColumn('ex_roof_list', explode(df['roof_list']))
# 建立常數虛擬欄位
ex_df = ex_df.withColumn('constant_val', lit(1))
# 將值樞紐化為布林欄位
piv_df = ex_df.groupBy('NO').pivot('ex_roof_list')\
  .agg(coalesce(first('constant_val')))
使用 PySpark 進行特徵工程

來整理一些特徵吧!

使用 PySpark 進行特徵工程

Preparing Video For Download...