1. 程式人生 > >EL之Bagging:利用DIY資料集(預留30%資料+兩種樹深)訓練Bagging演算法(DTR)

EL之Bagging:利用DIY資料集(預留30%資料+兩種樹深)訓練Bagging演算法(DTR)

EL之Bagging:利用DIY資料集(預留30%資料+兩種樹深)訓練Bagging演算法(DTR)

輸出結果

1、treeDepth=1

2、treeDepth=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))