開始使用免費開始

建立新特徵

特徵工程也包含實際建立新的特徵。建立新特徵很重要,因為模型的預測準確度仰賴這些特徵。在本練習中,你會檢查 3 個在資料中以整數呈現、但其實代表類別值的欄位性質。這 3 個欄位是:search_engine_typeproduct_typeadvertiser_type。你將為這 3 個欄位,以及 device_idsite_id 建立計數特徵。這些計數特徵代表各欄位中每個值的點擊次數,之後會用於預測。

pandas 模組已在你的工作環境中以 pd 提供,範例 DataFrame 已載入為 df

本練習屬於課程

用 Python 透過機器學習預測 CTR

檢視課程

練習說明

  • 列印 feature_list 清單中每個特徵的總值數與唯一值數。
  • 使用 .transform() 針對 new_feature_list 中的特徵計算各特徵的點擊次數,並據此建立新特徵。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Get counts of total and unique values for given features
feature_list = ["search_engine_type", "product_type", "advertiser_type"]
for feature in feature_list:
	print(df[feature].____)
	print(df[feature].____)

# Define new features as counts
new_feature_list = ['device_id', 'site_id'] + feature_list
for new_feature in new_feature_list:
  df[new_feature + '_count'] = df.____(
    new_feature)['click'].____("count")
print(df.head(5))
編輯並執行程式碼