波士顿预测房价
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)