如何连接决策树回归器预测输出到原始数据



我正在开发一个使用DecisionTreeRegressor的模型。 我使用训练数据构建并拟合了树,并从最近的数据中预测结果以确认模型的准确性。

要构建和适合树,请执行以下操作: X = np.matrix ( pre_x ) y = np.matrix( pre_y ) regr_b = 决策树回归器(max_depth = 4 ) regr_b.fit(X, y)

要预测新数据,请执行以下操作: X = np.matrix ( pre_test_x ) trial_pred = regr_b.predict(X, check_input=True)

trial_pred是预测值的数组。 我需要将其连接回pre_test_x以便我可以看到预测与实际发生的事情的匹配程度。

我尝试过合并:

all_pred = pre_pre_test_x.merge(predictions, left_index = True, right_index = True)

all_pred = pd.merge (pre_pre_test_x, predictions, how='left', left_index=True, right_index=True  )

并且要么不获取任何结果,要么获取附加到数据帧底部的列的第二个副本,并在所有现有列中具有 NaN。

事实证明这很简单。 将预测输出保留为数组,然后运行: w_pred = pre_pre_test_x.copy(deep=True) w_pred['pred_val']=trial_pred

最新更新