替换缺失的信用数据
现在,您需要检查是否存在缺失数据。若在 loan_status 中发现缺失值,您将无法用这些数据来预测违约概率,因为您不知道贷款是否违约。person_emp_length 中的缺失值危害较小,但仍会导致训练报错。
因此,请检查 person_emp_length 列中的缺失数据,并将所有缺失值替换为其中位数。
数据集 cr_loan 已加载到工作空间。
本练习是课程的一部分
Python 信用风险建模
练习说明
- 使用
.isnull()打印包含缺失数据的列名数组,并结合.any()进行筛选。 - 打印在
person_emp_length存在缺失数据的前 5 行记录。 - 使用
.fillna()将缺失值替换为所有就业年限的中位数。 - 为
person_emp_length列创建直方图,以检查其分布。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Print a null value column array
print(____.columns[____.____().any()])
# Print the top five rows with nulls for employment length
print(____[____[____].____()].head())
# Impute the null values with the median value for all employment lengths
____[____].____((cr_loan['person_emp_length'].____()), inplace=True)
# Create a histogram of employment length
n, bins, patches = plt.____(____[____], bins='auto', color='blue')
plt.xlabel("Person Employment Length")
plt.____()