使用AdaBoosting预测波士顿房价
作者:互联网
AdaBoosting示例
使用AdaBoosting预测波士顿房价
决策树示例:波士顿房价预测
根据13个特征预测房价价格
import sklearn.datasets as sd
import sklearn.utils as su
import sklearn.tree as st
import sklearn.metrics as sm
import sklearn.ensemble as se
读取数据集
boston = sd.load_boston()
# for sample in boston.data:
# print(sample)
样本随机化(打乱)
random_seed = 7 # 随机种子,用来产生随机数
x, y = su.shuffle(boston.data,
boston.target,
random_state=random_seed)
分训练集、测试集
train_size = int(len(x) * 0.8) # 计算训练集大小(80%)
train_x = x[:train_size] # 切出前面80%
test_x = x[train_size:] # 切出后面20%
train_y = y[:train_size] # 切出前面80%
test_y = y[train_size:] # 切出后面20%
model = se.AdaBoostRegressor(
st.DecisionTreeRegressor(max_depth=4), # 原始
n_estimators=400) # 数棵树
model.fit(train_x, train_y) # 训练
pred_test_y = model.predict(test_x) # 预测
计算并打印R2值
print(sm.r2_score(test_y, pred_test_y))
随机森林
model2 = se.RandomForestRegressor(
max_depth=10, # 最大深度
n_estimators=1000, # 树数量
min_samples_split=2) # 最小样本数量
model2.fit(train_x, train_y)
pred_test_y = model2.predict(test_x) # 预测
计算并打印R2值
print(sm.r2_score(test_y, pred_test_y))
标签:sklearn,房价,boston,AdaBoosting,train,test,import,波士顿,size 来源: https://blog.csdn.net/weixin_49304690/article/details/112626947