我正在使用 Catboost 并希望可视化 shap_values:
from catboost import CatBoostClassifier
model = CatBoostClassifier(iterations=300)
model.fit(X, y,cat_features=cat_features)
pool1 = Pool(data=X, label=y, cat_features=cat_features)
shap_values = model.get_feature_importance(data=pool1, fstr_type='ShapValues', verbose=10000)
shap_values.shape
Output: (32769, 10)
X.shape
Output: (32769, 9)
Run Code Online (Sandbox Code Playgroud)
然后我执行以下操作并引发异常:
shap.initjs()
shap.force_plot(shap_values[0,:-1], X.iloc[0,:])
Run Code Online (Sandbox Code Playgroud)
例外:在 v0.20 force_plot 现在需要基值作为第一个参数!尝试 shap.force_plot(explainer.expected_value, shap_values) 或对于多输出模型尝试 shap.force_plot(explainer.expected_value[0], shap_values[0])。
以下工作,但我想让 force_plot() 工作:
shap.initjs()
shap.summary_plot(shap_values[:,:-1], X)
Run Code Online (Sandbox Code Playgroud)
我阅读了文档,但无法理解解释器。我试过:
explainer = shap.TreeExplainer(model,data=pool1)
#Also tried:
explainer = shap.TreeExplainer(model,data=X)
Run Code Online (Sandbox Code Playgroud)
但我得到:TypeError: ufunc 'isnan' 不支持输入类型,并且根据转换规则 ''safe'' 无法将输入安全地强制转换为任何支持的类型
任何人都可以指出我正确的方向吗?谢谢
我想在随机搜索中使用默认超参数,我该怎么做?(此处为 per_float_feature_quantization 参数)
grid = {'learning_rate': [0.1, 0.16, 0.2],
'depth': [4, 6, 10],
'l2_leaf_reg': [1, 3, 5, 7, 9],
'iterations': [800, 1000, 1500, 2000],
'bagging_temperature': [1, 2, 3, 4, 5],
'border_count': [128, 256, 512],
'grow_policy': ['SymmetricTree', 'Depthwise'],
'per_float_feature_quantization':[None, '3:border_count=1024']}
model = CatBoostClassifier(loss_function='MultiClass',
custom_metric='Accuracy',
eval_metric='TotalF1',
od_type='Iter',
od_wait=40,
task_type="GPU",
devices='0:1',
random_seed=42,
cat_features=cat_features)
randomized_search_result = model.randomized_search(grid,
X=X,
y=y
)
Run Code Online (Sandbox Code Playgroud)
我有
CatBoostError: library/cpp/json/writer/json_value.cpp:499: Not a map
Run Code Online (Sandbox Code Playgroud) 在介绍/宣传视频 ( https://www.youtube.com/watch?v=s8Q_orF4tcI ) 中,您提到 Catboost 可以分析时间序列历史数据以进行天气预报。
但是我在教程中找不到这样的东西:https : //github.com/catboost/catboost/tree/master/catboost/tutorials
我有一个关于随机森林的问题。想象一下,我有关于用户与项目交互的数据。项目数量很大,大约 10 000 个。我的随机森林输出应该是用户可能与之交互的项目(如推荐系统)。对于任何用户,我想使用一个描述用户过去交互过的项目的功能。然而,将分类产品特征映射为 one-hot 编码似乎内存效率非常低,因为用户最多与不超过几百个项目进行交互,有时甚至只有 5 个。
当输入特征之一是具有约 10 000 个可能值的分类变量并且输出是具有约 10 000 个可能值的分类变量时,您将如何构建随机森林?我应该使用具有分类特征的 CatBoost 吗?或者我应该使用 one-hot 编码,如果是这样,您认为 XGBoost 还是 CatBoost 更好?
machine-learning random-forest categorical-data xgboost catboost
我使用CatBoostRegressor在Catboost库的Python版本.
根据文档,我可以使用过度拟合探测器,我正在这样做:
model = CatBoostRegressor(iterations=iters, learning_rate=0.03, depth=depth, verbose=True, od_pval=1, od_type='IncToDec', od_wait=20)
model.fit(train_pool, eval_set=validation_pool)
# this code didn't executed
model.save_model(model_name)
Run Code Online (Sandbox Code Playgroud)
然而,在过度拟合发生之后,我的Python脚本被中断,过早停止,选择你想要的任何短语,并且保存模型部分没有被执行,这导致了很多腰部时间并且最终没有结果.我没有得到任何堆栈跟踪.
有没有可能在CatBoost中处理它并节省工作时间?
我一直在研究 catboost 算法,但我很难看出使用对称树的意义。关于这一点,我在他们的github上找到了:
该算法的一个重要部分是它使用对称树并逐层构建它们。对称树是每一层节点使用相同分裂的树。这允许使用索引对叶子的路径进行编码。例如,有一棵深度为2的树。第一层的分裂是f1<2,第二层的分裂是f2<4。那么 f1=5,f2=0 的对象将有编号为 01b 的叶子。
他们说这有助于减少过度拟合并进行更快的推理,但是,直观地对我来说,这就像你需要两倍的深度来探索相同数量的分割。
那么,有人能解释一下使用这种类型的树实际上有什么好处吗?
非常感谢。
我接受过训练CatBoostClassifier
来解决我的分类任务。现在我需要保存模型并在另一个应用程序中使用它进行预测。为此,我通过save_model
方法保存模型并通过方法恢复它load_model
。
但是,每次我调用predict
恢复的模型时,都会收到错误消息:
CatboostError: There is no trained model to use predict(). Use fit() to train model. Then use predict().
Run Code Online (Sandbox Code Playgroud)
所以看起来我需要再次训练我的模型,而我需要恢复预训练模型并将其仅用于预测。
我在这里做错了什么?我应该使用一种特殊的方式来加载模型进行预测吗?
我的训练过程是这样的:
model = CatBoostClassifier(
custom_loss=['Accuracy'],
random_seed=42,
logging_level='Silent',
loss_function='MultiClass')
model.fit(
x_train,
y_train,
cat_features=None,
eval_set=(x_validation, y_validation),
plot=True)
...
model.save("model.cbm")
Run Code Online (Sandbox Code Playgroud)
我使用以下代码恢复模型:
model = CatBoostClassifier(
custom_loss=['Accuracy'],
random_seed=42,
logging_level='Silent',
loss_function='MultiClass')
model.load_model("model.cbm")
...
predict = self.model.predict(inputs)
Run Code Online (Sandbox Code Playgroud) 我使用 CatBoostClassifier,我的类高度不平衡。我应用了一个scale_pos_weight参数来解决这个问题。在使用评估数据集(测试)进行训练时,CatBoost 在测试中显示出很高的精度。然而,当我使用预测方法对测试进行预测时,我只得到低精度分数(使用 sklearn.metrics 计算)。
我认为这可能与我应用的班级权重有关。但是,我不太明白精度分数是如何受此影响的。
params = frozendict({
'task_type': 'CPU',
'loss_function': 'Logloss',
'eval_metric': 'F1',
'custom_metric': ['F1', 'Precision', 'Recall'],
'iterations': 100,
'random_seed': 20190128,
'scale_pos_weight': 56.88657244809081,
'learning_rate': 0.5412829495147387,
'depth': 7,
'l2_leaf_reg': 9.526905230698302
})
from catboost import CatBoostClassifier
model = cb.CatBoostClassifier(**params)
model.fit(
X_train, y_train,
cat_features=np.where(X_train.dtypes == np.object)[0],
eval_set=(X_test, y_test),
verbose=False,
plot=True
)
model.get_best_score()
{'learn': {'Recall': 0.9243007537531925,
'Logloss': 0.15892360013680026,
'F1': 0.9416723809244181,
'Precision': 0.9640191600545249},
'validation_0': {'Recall': 0.914252301192093,
'Logloss': 0.1714387314107052,
'F1': 0.9357892623978286,
'Precision': 0.9642642597943112}}
y_test_pred = model.predict(data=X_test)
from sklearn.metrics import balanced_accuracy_score, recall_score, precision_score, f1_score
print('Balanced …
Run Code Online (Sandbox Code Playgroud) 当我尝试运行 CatBoost 算法时出现此错误。它是参数之一,所以我不明白为什么当我注释掉 Early_stopping_rounds 时会发生此错误,我对 cat_features 参数遇到相同的错误
from catboost import CatBoostClassifier
categorical_indexes = np.where(X.dtypes == 'object')[0]
X.drop(["id"], axis = 1, inplace = True)
params = {'loss_function':'Logloss',
'eval_metric':'AUC',
'verbose': 200,
"early_stopping_rounds": 200,
"cat_features": categorical_indexes,
'random_seed': 17}
cat = CatBoostClassifier(**params)
cat.fit(X,y)
Run Code Online (Sandbox Code Playgroud)
编辑:
通过 pip 升级 catboost 包现在我收到另一个错误
但是,在我的代码中,我正在过滤分类值,然后将它们分配给 cat_features 参数
TypeError Traceback (most recent call last)
_catboost.pyx in _catboost.get_float_feature()
_catboost.pyx in _catboost._FloatOrNan()
_catboost.pyx in _catboost._FloatOrNanFromString()
TypeError: Cannot convert 'b'Triangle'' to float
During handling of the above exception, another exception occurred:
CatBoostError Traceback …
Run Code Online (Sandbox Code Playgroud) 我正在研究一个数据科学回归问题,训练集上约有 90,000 行,测试集上有 8500 行。有 9 个分类列,无缺失数据。对于这种情况,我应用了一个 catboostregressor,它给了我相当好的 R2(98.51)和 MAE(3.77)。其他节点LGBM、XGBOOST在catboost下执行。
现在我想增加 R2 值并减少 MAE 以获得更准确的结果。需求也是如此。
我通过添加不同值的 'loss_function': ['MAE'], 'l2_leaf_reg':[3], 'random_strength': [4], 'bagging_Temperature':[0.5] 进行了多次调整,但性能是相同的。
谁能帮助我如何通过最小化 MAE 和 MSE 来提高 R2 值?