开始使用免费开始使用

分类列

在航班数据中有两列 carrierorg,包含分类数据。您需要将这些列转换为带索引的数值。

本练习是课程的一部分

使用 PySpark 进行机器学习

查看课程

练习说明

  • 导入合适的类,并创建一个索引器对象,将 carrier 列从字符串转换为数值索引。
  • 在航班数据上拟合该索引器对象。
  • 使用已拟合的索引器创建数值索引列。
  • org 列重复上述过程。

交互式实操练习

通过完成这段示例代码来试试这个练习。

from pyspark.ml.feature import ____

# Create an indexer
indexer = ____(inputCol=____, outputCol='carrier_idx')

# Indexer identifies categories in the data
indexer_model = indexer.____(flights)

# Indexer creates a new column with numeric index values
flights_indexed = ____.____(____)

# Repeat the process for the other categorical feature
flights_indexed = ____(inputCol=____, outputCol='org_idx').____(____).____(____)
flights_indexed.show(5)
编辑并运行代码