交叉验证获取最佳决策树深度报错 5C

代码如下:

def best_depth_decision(d):

    lr2 = DecisionTreeRegressor(max_depth=d)

    kfold_depth = KFold(n_splits=10,random_state=1).split(x_train_pca,y_train)

    mse_kfold_test = []

    mse_kfold_train = []

    for k, (train, test) in enumerate(kfold_depth):

        lr2.fit(x_train_pca[train],y_train[train])

        y_test_pred2 = lr2.predict(x_train_pca[test])

        y_train_pred2 = lr2.predict(x_train_pca[train])

        s_train = mean_squared_error(y_train[train],y_train_pred2)

        s_test = mean_squared_error(y_train[test],y_test_pred2)

        mse_kfold_test = np.mean(mse_kfold_test.append(s_test))

        mse_kfold_train = np.mean(mse_kfold_train.append(s_train))

        return (mse_kfold_train,mse_kfold_test)

depths = np.arange(1,31)

scores2 = [best_depth_decision(d) for d in depths]

train_mse = [s[0] for s in scores2]

test_mse = [s[1] for s in scores2]

train_best_index2 = np.argmin(train_mse)

test_best_index2 = np.argmin(test_mse)

plt.plot(depths,test_mse)

plt.show()

报错如下:
图片说明

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问