可视化缺失数据
能够绘制缺失值分布,有助于快速了解数据中缺失的比例。它还能帮助您发现变量是否存在成规律的缺失模式;如果有,就需要谨慎处理,否则模型可能会产生偏差。
哪个变量的缺失值最多?请运行除最后一行外的所有代码来找出答案。确认后,填写该变量名并点击 "Submit Answer"。
本练习是课程的一部分
使用 PySpark 进行特征工程
练习说明
- 使用
select()结合列名列表columns从数据框df中取子集,并用提供的sample()函数进行抽样,将结果数据框赋给变量sample_df。 - 将该子集转换为
pandas数据框pandas_df,再使用pandas的isnull()将这个DataFrame转换为 True/False。把结果存到tf_df。 - 使用 seaborn 的
heatmap()绘制tf_df。 - 点击 "Run Code" 查看图形。然后把缺失值最多的变量名赋给
answer。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Sample the dataframe and convert to Pandas
____ = df.select(____).sample(False, 0.1, 42)
____ = ____.toPandas()
# Convert all values to T/F
tf_df = ____.____()
# Plot it
sns.____(data=____)
plt.xticks(rotation=30, fontsize=10)
plt.yticks(rotation=0, fontsize=10)
plt.show()
# Set the answer to the column with the most missing data
answer = '____'