def predict(self, rows_new): """ :param rows_new: 待预测数据集 :return: 判别: 投票最高标签; 回归: 预测结果均值 """ results = [] for r in rows_new: sub_results = [] for t in self.trees: predicted = dts.predict_single(t, r, out="value") sub_results.append(predicted) if self.target == "classification": # 判别: 投票 tmp_dict = {} for i in sub_results: tmp_dict[i] = tmp_dict.get(i, 0) + 1 k_tracker = None v_tracker = 0.0 for k, v in tmp_dict.items(): if v > v_tracker: k_tracker = k v_tracker = v results.append(k_tracker) else: # 回归: 求均值 results.append(np.mean(sub_results)) return results
def predict(self, rows_new): """ 将self.trees的每个预测结果累加, 得到最终预测值 :param rows_new: 待预测数据, 嵌套的list或tuple :return: gbdt预测的y值 """ result = [] for r in rows_new: y_new = 0.0 # 预测结果为所有树的结果累加 for t in self.trees: y_new += dts.predict_single(t, r) result.append(y_new) return result