開始使用免費開始

對數轉換

在前面的練習中,你以線性方式縮放資料,這不會改變資料的形狀。如果你的資料是常態分佈(或接近常態),這樣做很有效,而許多機器學習模型也假設如此。有時你會處理非常接近常態的資料,例如一個族群的身高或體重。然而,真實世界中的許多變數並不符合這種模式,例如工資或族群的年齡。在本練習中,你將對 so_numeric_df DataFrame 的 ConvertedSalary 欄位套用對數轉換,因為它的大多數值集中在較低範圍,但也包含非常高的數值。這類分佈通常被稱為具有長右尾。

本練習屬於課程

Feature Engineering for Machine Learning in Python

檢視課程

練習說明

  • sklearnpreprocessing 模組匯入 PowerTransformer
  • PowerTransformer() 具現化為 pow_trans
  • so_numeric_dfConvertedSalary 欄位上擬合 PowerTransformer
  • 使用你剛剛擬合的縮放器轉換同一個欄位。

動手互動練習

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

# Import PowerTransformer
from sklearn.preprocessing import ____

# Instantiate PowerTransformer
pow_trans = ____

# Train the transform on the data
____

# Apply the power transform to the data
so_numeric_df['ConvertedSalary_LG'] = ____(so_numeric_df[['ConvertedSalary']])

# Plot the data before and after the transformation
so_numeric_df[['ConvertedSalary', 'ConvertedSalary_LG']].hist()
plt.show()
編輯並執行程式碼