Python-intro机器学习-笔记-20201109-HK

机器学习入门笔记整理

  • 决策树回归器:
    设置Max_leaf_nodes,防止过拟合;
def get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y):
    model = DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes, random_state=0)

  • 对比多个Max_leaf_nodes参数,找到MAE最优的那个:
# compare MAE with differing values of max_leaf_nodes
for max_leaf_nodes in [5, 50, 500, 5000]:
    my_mae = get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y)
    print("Max leaf nodes: %d  \t\t Mean Absolute Error:  %d" %(max_leaf_nodes, my_mae))
  • 找到有缺失值的columns,使用推导式:
    推导式分为三个部分:col结果,for循环,if过滤条件;
    col结果是根据if条件过滤而来的,过滤的范围是for循环。
# Get names of columns with missing values
cols_with_missing = [col for col in X_train.columns
                     if X_train[col].isnull().any()]
  • 有时“直接drop掉含有缺失值的列”与“预测缺失值并填充进NAN处”相比,模型MAE会更小,为什么?
    Answer:
    由于模型中有些字段的值的缺失是有意义的,将这类值预测出来并填充反而加重了data的噪声。example:车库修建年份,这个字段的缺失值代表“房子没有车库”,这会影响到房子的价值,如果预测并填充进一个年份,会导致这组数据变成偏差很大的数据,noise。

猜你喜欢

转载自blog.csdn.net/weixin_42012759/article/details/109557696
HK