使用 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)