决策树 DecisionTreeRegressor

波士顿预测房价 

import pandas as pd

# 读入文件
iowa_file_path = '../input/home-data-for-ml-course/train.csv'
home_data = pd.read_csv(iowa_file_path)

#指定标签y
y = home_data.SalePrice
# 创建作为自变量X的因子列表
feature_names =['LotArea','YearBuilt','1stFlrSF','2ndFlrSF','FullBath','BedroomAbvGr','TotRmsAbvGrd']
X = home_data[feature_names]

#构建模型
from sklearn.tree import DecisionTreeRegressor
iowa_model = DecisionTreeRegressor(random_state=1)
iowa_model.fit(X,y)
predictions = iowa_model.predict(X)
print(predictions)

模型验证:

from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error
#将数据划分为训练集和验证集,根据训练集训练模型,用该模型预测验证集,输出平均绝对误差  

train_X, val_X, train_y, val_y = train_test_split(X, y, random_state = 0)

melbourne_model = DecisionTreeRegressor()
melbourne_model.fit(train_X, train_y)
val_predictions = melbourne_model.predict(val_X)
print(mean_absolute_error(val_y, val_predictions))

解决欠拟合和过拟合问题

 

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)
    model.fit(train_X, train_y)
    preds_val = model.predict(val_X)
    mae = mean_absolute_error(val_y, preds_val)
    return(mae)

#找能使my_mae最小的max_leaf_nodes
candidate_max_leaf_nodes = [5, 25, 50, 100, 250, 500]
ans,min_mae=5,1000000000
for max_leaf_nodes in candidate_max_leaf_nodes:
    my_mae = get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y)
    if my_mae<min_mae:
        ans=max_leaf_nodes
        min_mae=my_mae
best_tree_size = ans

#构建时指定max_leaf_nodes这个参数
final_model = DecisionTreeRegressor(max_leaf_nodes=best_tree_size)
# 用所有的数据进行训练
final_model.fit(X,y)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值