gpt4 book ai didi

python - CATBoost 和 GridSearch

转载 作者:太空宇宙 更新时间:2023-11-03 20:06:21 25 4
gpt4 key购买 nike

model.fit(train_data, y=label_data, eval_set=eval_dataset)
eval_dataset = Pool(val_data, val_labels)
model = CatBoostClassifier(depth=8 or 10, iterations=10, task_type="GPU", devices='0-2', eval_metric='Accuracy', boosting_type="Ordered", bagging_temperature=0, use_best_model=True)

当我运行上面的代码时(两次单独运行/深度设置为 8 或 10),我得到以下结果:

深度 10:0.6864865深度8:0.6756757

我想以某种方式设置和运行 GridSearch - 因此它运行完全相同的组合并产生完全相同的结果 - 与我手动运行代码时一样。

网格搜索代码:

model = CatBoostClassifier(iterations=10, task_type="GPU", devices='0-2', eval_metric='Accuracy', boosting_type="Ordered", depth=10, bagging_temperature=0, use_best_model=True)

grid = {'depth': [8,10]}
grid_search_result = GridSearchCV(model, grid, cv=2)
results = grid_search_result.fit(train_data, y=label_data, eval_set=eval_dataset)

问题:

  1. 我希望 GridSearch 使用我的“eval_set”来比较/验证所有不同的运行(例如手动运行时) - 但它使用了其他东西,我不明白它是什么并且它不明白似乎根本就看“eval_set”?

  2. 它不仅产生 2 个结果 - 还取决于“cv”(交叉验证分割策略)参数,它运行 3、5、7、9 或 11 次运行?我不想这样。

  3. 我尝试通过调试器检查整个“结果”对象 - 但我根本找不到最佳或所有其他运行的验证“准确性”分数。我可以找到很多其他值 - 但它们都不符合我正在寻找的值。这些数字与“eval_set”数据集生成的数字不匹配?

我通过实现自己的简单 GridSearch 解决了我的问题(如果它可以帮助/启发其他人:-)):如果您对代码有任何意见,请告诉我:-)

import pandas as pd
from catboost import CatBoostClassifier, Pool
from sklearn.model_selection import GridSearchCV
import csv
from datetime import datetime

# Initialize data

train_data = pd.read_csv('./train_x.csv')
label_data = pd.read_csv('./labels_train_x.csv')
val_data = pd.read_csv('./val_x.csv')
val_labels = pd.read_csv('./labels_val_x.csv')

eval_dataset = Pool(val_data, val_labels)

ite = [1000,2000]
depth = [6,7,8,9,10]
max_bin = [None,32,46,100,254]
l2_leaf_reg = [None,2,10,20,30]
bagging_temperature = [None,0,0.5,1]
random_strength = [None,1,5,10]
total_runs = len(ite) * len(depth) * len(max_bin) * len(l2_leaf_reg) * len(bagging_temperature) * len(random_strength)

print('Total runs: ' + str(total_runs))

counter = 0

file_name = './Results/Catboost_' + str(datetime.now().strftime("%d_%m_%Y_%H_%M_%S")) + '.csv'

row = ['Validation Accuray','Logloss','Iterations', 'Depth', 'Max_bin', 'L2_leaf_reg', 'Bagging_temperature', 'Random_strength']
with open(file_name, 'a') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(row)
csvFile.close()

for a in ite:
for b in depth:
for c in max_bin:
for d in l2_leaf_reg:
for e in bagging_temperature:
for f in random_strength:
model = CatBoostClassifier(task_type="GPU", devices='0-2', eval_metric='Accuracy', boosting_type="Ordered", use_best_model=True,
iterations=a, depth=b, max_bin=c, l2_leaf_reg=d, bagging_temperature=e, random_strength=f)
counter += 1
print('Run # ' + str(counter) + '/' + str(total_runs))
result = model.fit(train_data, y=label_data, eval_set=eval_dataset, verbose=1)

accuracy = float(result.best_score_['validation']['Accuracy'])
logLoss = result.best_score_['validation']['Logloss']

row = [ accuracy, logLoss,
('Auto' if a == None else a),
('Auto' if b == None else b),
('Auto' if c == None else c),
('Auto' if d == None else d),
('Auto' if e == None else e),
('Auto' if f == None else f)]

with open(file_name, 'a') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(row)
csvFile.close()

最佳答案

Catboost 中的评估集充当保留集。

在 GridSearchCV 中,CV 是在您的 train_data 上执行的。

一种解决方案是合并您的 train_data 和 eval_dataset 并在 GridSearchCV 中传递 train 和 eval 的索引。尝试在 cv 参数中生成两组索引。然后您将只有一次分割和准确度数字,这将为您提供相同的结果。

关于python - CATBoost 和 GridSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58951164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com