EL之Bagging:利用Bagging算法实现回归预测(实数值评分预测)问题

EL之Bagging:利用Bagging算法实现回归预测(实数值评分预测)问题


输出结果

设计思路

核心思路

#4.1、当treeDepth=1,对图进行可视化
#(1)、定义numTreesMax、treeDepth
numTreesMax = 30
treeDepth = 1                           # ----------------------▲▲▲▲▲

modelList = []
predList = []

#number of samples to draw for stochastic bagging
nBagSamples = int(len(xTrain) * 0.5)

for iTrees in range(numTreesMax):
    idxBag = []
    for i in range(nBagSamples):
        idxBag.append(random.choice(range(len(xTrain))))
    xTrainBag = [xTrain[i] for i in idxBag]
    yTrainBag = [yTrain[i] for i in idxBag]

    modelList.append(DecisionTreeRegressor(max_depth=treeDepth))
    modelList[-1].fit(xTrainBag, yTrainBag)

    latestPrediction = modelList[-1].predict(xTest)
    predList.append(list(latestPrediction))

mse = []
allPredictions = []
for iModels in range(len(modelList)):
    prediction = []
    for iPred in range(len(xTest)):
        prediction.append(sum([predList[i][iPred] for i in range(iModels + 1)])/(iModels + 1))

    allPredictions.append(prediction)
    errors = [(yTest[i] - prediction[i]) for i in range(len(yTest))]
    mse.append(sum([e * e for e in errors]) / len(yTest))

#4.2、当treeDepth=1,对图进行可视化
#(1)、定义numTreesMax、treeDepth
numTreesMax = 30
treeDepth = 5                           # ----------------------▲▲▲▲▲

#4.3、当treeDepth=12,对图进行可视化
#(1)、定义numTreesMax、treeDepth

numTreesMax = 100                       # ----------------------☆☆☆☆☆
treeDepth = 12                          # ----------------------☆☆☆☆☆

(0)

相关推荐