python - CatBoostRegressor 在测试直线上进行预测

标签 python machine-learning regression catboost

测试数据集中的 CatBoostRegressor 拟合直线

Red is CatBoostRegressor

第一个图是训练数据集(基于噪声正弦训练的 CatBoostRegressor) 第二张图是测试数据集

为什么它适合一条直线?其他函数也是如此(如 f(x)=x 等)

x = np.linspace(0, 2*np.pi, 100)
y = func(x) + np.random.normal(0, 3, len(x))

x_test = np.linspace(0*np.pi, 4*np.pi, 200)
y_test = func(x_test)

train_pool = Pool(x.reshape((-1,1)), y)
test_pool = Pool(x_test.reshape((-1,1))) 

model = CatBoostRegressor(iterations=100, depth=2, loss_function="RMSE",
                          verbose=True
                          )
model.fit(train_pool)

y_pred = model.predict(x.reshape((-1,1)))
y_test_pred = model.predict(test_pool)

poly = Polynomial(4)
p = poly.fit(x,y);


plt.plot(x, y, 'ko')
plt.plot(x, func(x), 'k')
plt.plot(x, y_pred, 'r')
plt.plot(x, poly.evaluate(p, x), 'b')

plt.show()

plt.plot(x_test, y_test, 'k')
plt.plot(x_test, y_test_pred, 'r')
plt.show()
plt.plot(x_test, y_test, 'k')
plt.plot(x_test, poly.evaluate(p, x_test), 'b')
plt.show()

最佳答案

这是因为决策树是分段常数函数,而Catboost完全基于决策树。因此catboost 总是用一个常数进行推断

因此,Catboost(以及其他基于树的算法,如 XGBoost 或随机森林的所有实现)在外推方面很差(除非您进行巧妙的特征工程,实际上它会自行外推)。

在您的示例中,Catboost 用常数推断正弦,这很不酷。但多项式拟合更糟糕:它很快就会趋于无穷大!

enter image description here

这是生成图片的完整代码:

import numpy as np
func = np.sin
from catboost import Pool, CatBoostRegressor
from numpy.polynomial.polynomial import Polynomial
import matplotlib.pyplot as plt

np.random.seed(1)

x = np.linspace(0, 2*np.pi, 100)
y = func(x) + np.random.normal(0, 3, len(x))

x_test = np.linspace(0*np.pi, 4*np.pi, 200)
y_test = func(x_test)

train_pool = Pool(x.reshape((-1,1)), y)
test_pool = Pool(x_test.reshape((-1,1))) 

model = CatBoostRegressor(iterations=100, depth=2, loss_function="RMSE",verbose=False)
model.fit(train_pool, verbose=False)

y_pred = model.predict(x.reshape((-1,1)))
y_test_pred = model.predict(test_pool)

p = np.polyfit(x, y, deg=4)

plt.scatter(x, y, s=3, c='k')
plt.plot(x_test, y_test, 'k')
plt.plot(x_test, y_test_pred, 'r')
plt.plot(x_test, np.polyval(p, x_test), 'b')
plt.title('Out-of-sample performance of trees and polynomials')
plt.legend(['training data', 'true', 'catboost', 'polynomial'])
plt.ylim([-4, 4])
plt.show()

关于python - CatBoostRegressor 在测试直线上进行预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46310237/

相关文章:

r - 如何将回归摘要(例如 p 值和系数)输出到 rasterbrick 中?

python - 具有 Statsmodel ValueError : zero-size array to reduction operation maximum which has no identity 的多重 OLS 回归

machine-learning - 为推荐引擎生成测试集

machine-learning - ML 训练过程不在 GPU 上

python - Keras:嵌入层的加权平均值

python - pybrain NNregression工具参数

python - python unittest在setUp方法中报错的正确方法是什么?

python - 获取所有下义词的完整列表

python - 使用 numpy 数组的真值错误

python - Python-声明中的L错误