開始使用免費開始

測試 QuantileTransformer

標準化與 z 分數有相同的風險。兩者的計算都使用平均數與標準差,因此對極端值非常敏感。

為了避開這個問題,你應該使用以分位數為基礎的 QuantileTransformer。在分配中,分位數不會因離群值的幅度而改變。

當資料近似常態分配(可用長條圖檢查)時,才使用 StandardScaler。對於其他分配,QuantileTransformer 會是更好的選擇。

你將使用已載入的 females 資料集進行練習。matplotlib.pyplot 已以慣用別名 plt 載入。

本練習屬於課程

Python 中的異常偵測

檢視課程

練習說明

  • 建立一個將特徵轉換為常態分配的 QuantileTransformer(),並指派給 qt。
  • 對特徵陣列 X 進行擬合與轉換,並保留欄位名稱。
  • 繪製 palmlength 欄位的長條圖。

動手互動練習

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

from sklearn.preprocessing import QuantileTransformer

# Instantiate an instance that casts to normal
qt = ____

# Fit and transform the feature array
X.____ = ____

# Plot a histogram of palm length
plt.____(____, color='red')

plt.xlabel("Palm length")
plt.show()
編輯並執行程式碼