開始使用免費開始

類別型欄位

在航班資料中有兩個欄位 carrierorg,內容為類別型資料。你需要將這些欄位轉換成具索引的數值。

本練習屬於課程

使用 PySpark 的機器學習

檢視課程

練習說明

  • 匯入適當的類別,並建立一個 indexer 物件,將 carrier 欄位從字串轉換為數值索引。
  • 在航班資料上先準備(fit)這個 indexer 物件。
  • 使用已準備好的 indexer 建立數值索引欄位。
  • 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)
編輯並執行程式碼