使用 Corr()
那句老話「相關不代表因果」是一個重要的提醒。不過,相關性仍能提示你從哪裡開始尋找可望成為模型特徵的方向。 用這個練習先熟悉如何在資料中初步搜尋並嘗試找出模式。
已幫你建立一個名為 columns 的清單,裡面包含欄位名稱。在本練習中,你要計算這些欄位與 'SALESCLOSEPRICE' 之間的相關性,並找出其中的最大值。
本練習屬於課程
使用 PySpark 進行特徵工程
練習說明
- 使用
for迴圈走訪columns。 - 在每一次迴圈中,使用
corr()方法計算目前欄位與'SALESCLOSEPRICE'的相關性。 - 撰寫邏輯以更新觀察到的最大相關性,以及對應的是哪個欄位。
- 列印出與
'SALESCLOSEPRICE'具有最大相關性的欄位名稱。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Name and value of col with max corr
corr_max = 0
corr_max_col = columns[0]
# Loop to check all columns contained in list
for ____ in ____:
# Check the correlation of a pair of columns
corr_val = df.____(____, ____)
# Logic to compare corr_max with current corr_val
if ____ ____ ____:
# Update the column name and corr value
corr_max = corr_val
corr_max_col = col
print(corr_max_col)