Using AdaBoosting to predict Boston housing prices

AdaBoosting example

Using AdaBoosting to predict Boston housing prices

Decision tree example: Boston housing price prediction

Predict housing prices based on 13 characteristics

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

Read data set

boston = sd.load_boston()
# for sample in boston.data:
#     print(sample)

Sample randomization (scrambling)

random_seed = 7  # 随机种子,用来产生随机数
x, y = su.shuffle(boston.data,
                  boston.target,
                  random_state=random_seed)

Sub-training set, test set

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)  # 预测

Calculate and print R2 value

print(sm.r2_score(test_y, pred_test_y))

Random forest

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)  # 预测

Calculate and print R2 value

print(sm.r2_score(test_y, pred_test_y))

Guess you like

Origin blog.csdn.net/weixin_49304690/article/details/112626947