对航班起飞机场进行编码
航班数据中的 org 列是一个分类变量,表示航班的起飞机场。
- ORD — O'Hare International Airport(芝加哥奥黑尔国际机场)
- SFO — San Francisco International Airport(旧金山国际机场)
- JFK — John F Kennedy International Airport(纽约约翰·F·肯尼迪国际机场)
- LGA — La Guardia Airport(纽约拉瓜迪亚机场)
- SMF — Sacramento(萨克拉门托)
- SJC — San Jose(圣何塞)
- OGG — Kahului(夏威夷卡胡卢伊)
显然,这只是机场的一个小子集。无论如何,由于这是分类变量,在用于回归模型之前需要进行独热编码(one-hot encoding)。
数据存放在名为 flights 的变量中。您已经使用字符串索引器创建了与 org 中字符串对应的索引值列。
您可以在 IPython Shell 旁边的 Slides 面板中回顾课程幻灯片,这可能会有所帮助。
本练习是课程的一部分
使用 PySpark 进行机器学习
练习说明
- 导入独热编码器类。
- 创建一个独热编码器实例,输入列命名为
org_idx,输出列命名为org_dummy。 - 将独热编码器应用到航班数据上。
- 生成从分类取值到二进制哑变量的映射摘要。只保留唯一值,并按
org_idx排序。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import the one hot encoder class
from pyspark.ml.____ import ____
# Create an instance of the one hot encoder
onehot = ____(inputCols=[____], outputCols=[____])
# Apply the one hot encoder to the flights data
onehot = onehot.____(____)
flights_onehot = onehot.____(____)
# Check the results
flights_onehot.____('org', 'org_idx', 'org_dummy').____().____('org_idx').show()