【问题标题】:CatBoostRegression predict on test straight lineCatBoost回归预测测试直线
【发布时间】:2018-02-28 19:36:57
【问题描述】:

测试数据集中的 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()

【问题讨论】:

  • 因为你的超参数选择不正确

标签: python machine-learning regression catboost


【解决方案1】:

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

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

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

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

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 2022-01-21
    • 2019-12-31
    • 2017-01-17
    • 2018-10-09
    • 1970-01-01
    相关资源
    最近更新 更多