條件式篩選數值欄位
再一次強調,理解資料的情境非常重要。我們想知道房屋銷售價格的正常區間。讓我們把相較於平均值高出或低出許多的離群房屋排除掉。在這裡,你會計算平均數與標準差,並用它們來篩選近似常態分布的欄位 log_SalesClosePrice。
本練習屬於課程
使用 PySpark 進行特徵工程
練習說明
- 從
pyspark.sql.functions匯入mean()與stddev()。 - 使用
agg(),搭配匯入的函式,計算'log_SalesClosePrice'的平均數與標準差。 - 以
mean_val加減 3 倍的stddev_val建立上下界。 - 針對
'log_SalesClosePrice'建立where()篩選條件,同時使用low_bound與hi_bound。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
from ____ import ____, ____
# Calculate values used for outlier filtering
mean_val = df.____({____: ____}).collect()[0][0]
stddev_val = df.____({____: ____}).collect()[0][0]
# Create three standard deviation (μ ± 3σ) lower and upper bounds for data
low_bound = ____ - (3 * ____)
hi_bound = ____ + (3 * ____)
# Filter the data to fit between the lower and upper bounds
df = df.____((df[____] < ____) ____ (df[____] > ____))