準備進行隨機森林迴歸

使用 PySpark 進行特徵工程

John Hogue

Lead Data Scientist, General Mills

特徵所需的假設

Random Forest Regression

  • 偏態/非常態?OK
  • 未標準化?OK
  • 有遺漏值?OK
  • 類別型資料?OK

Assumptions

使用 PySpark 進行特徵工程

外加特徵

經濟

  • 30 年房貸利率

政府

  • 城市中古屋價中位數
  • 城市屋齡占比
  • 城市屋坪數占比

社會

  • Walk Score(步行分數)
  • Bike Score(自行車分數)

季節性

  • 銀行假日
使用 PySpark 進行特徵工程

特徵工程

時間特徵

  • 僅 1 年資料,資訊有限
  • 假期週

比率與加總

  • 商業情境
  • 個人情境

擴充特徵

  • 非自由文字欄位
  • 需要移除低出現次數
# What is shape of our data?
print((df.count(), len(df.columns)))
(5000, 126)
使用 PySpark 進行特徵工程

DataFrame 欄位轉為特徵向量

from pyspark.ml.feature import VectorAssembler
# Replace Missing values
df = df.fillna(-1)
# Define the columns to be converted to vectors
features_cols = list(df.columns)
# Remove the dependent variable from the list
features_cols.remove('SALESCLOSEPRICE')
使用 PySpark 進行特徵工程

DataFrame 欄位轉為特徵向量

# Create the vector assembler transformer
vec = VectorAssembler(inputCols=features_cols, outputCol='features')

# Apply the vector transformer to data df = vec.transform(df)
# Select only the feature vectors and the dependent variable ml_ready_df = df.select(['SALESCLOSEPRICE', 'features'])
# Inspect Results ml_ready_df.show(5)
+----------------+--------------------+
| SALESCLOSEPRICE|            features|
+----------------+--------------------+
|143000          |(125,[0,1,2,3,5,6...|
|190000          |(125,[0,1,2,3,5,6...|
|225000          |(125,[0,1,2,3,5,6...|
|265000          |(125,[0,1,2,3,4,5...|
|249900          |(125,[0,1,2,3,4,5...|
+----------------+--------------------+
only showing top 5 rows
使用 PySpark 進行特徵工程

現在可以開始做機器學習了!

使用 PySpark 進行特徵工程

Preparing Video For Download...