開始使用免費開始

條件式篩選數值欄位

再一次強調,理解資料的情境非常重要。我們想知道房屋銷售價格的正常區間。讓我們把相較於平均值高出或低出許多的離群房屋排除掉。在這裡,你會計算平均數與標準差,並用它們來篩選近似常態分布的欄位 log_SalesClosePrice

本練習屬於課程

使用 PySpark 進行特徵工程

檢視課程

練習說明

  • pyspark.sql.functions 匯入 mean()stddev()
  • 使用 agg(),搭配匯入的函式,計算 'log_SalesClosePrice' 的平均數與標準差。
  • mean_val 加減 3 倍的 stddev_val 建立上下界。
  • 針對 'log_SalesClosePrice' 建立 where() 篩選條件,同時使用 low_boundhi_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[____] > ____))
編輯並執行程式碼