这一段的print无法输出最优结果,为什么,怎么改

为什么这里print不打印?

def XGTSearch(X, y):

print("Parameter optimization")
n_estimators = [50, 100, 200, 400,600,800]
max_depth = [2, 4, 5,6,7, 8]
learning_rate = [0.0001, 0.001, 0.01, 0.05, 0.1, 0.2]
param_grid = dict(max_depth=max_depth, n_estimators=n_estimators, learning_rate=learning_rate)
print("param_grid:",param_grid)
xgb_model = XGBRegressor(objective='reg:squarederror')  
kfold = TimeSeriesSplit(n_splits=5).get_n_splits([X, y])
fit_params = {"eval_metric": "rmse"}
grid_search = GridSearchCV(xgb_model, param_grid, verbose=1, fit_params=fit_params, cv=kfold)
grid_result = grid_search.fit(X, y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
  print("%f (%f) with: %r" % (mean, stdev, param))
  rgs = GridSearchCV(xgb_model, param_grid)
  rgs.fit(X, y)
  print(rgs.fit(X, y), flush=True)
return mean, stdev, param, grid_result 

这是个函数定义,你调用函数了么?

1.仔细检查函数到底调用了没有
2.如果函数调用了,那循环到底走没走,是不是zip的结果是个空列表

第9行 : fit_params = {"eval_metric": "rmse"}

from os import renames
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import metrics
import pickle
from xgboost.sklearn import XGBRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, mean_squared_error
from sklearn.model_selection import KFold, train_test_split, GridSearchCV, cross_val_score
from sklearn.model_selection import TimeSeriesSplit


def XGTSearch(X, y):
    print("Parameter optimization")
    n_estimators = [50, 100, 200, 400, 600, 800]
    max_depth = [2, 4, 5, 6, 7, 8]
    learning_rate = [0.0001, 0.001, 0.01, 0.05, 0.1, 0.2]
    param_grid = dict(max_depth=max_depth, n_estimators=n_estimators, learning_rate=learning_rate)
    print("param_grid:", param_grid)
    xgb_model = XGBRegressor(objective='reg:squarederror')
    kfold = TimeSeriesSplit(n_splits=5).get_n_splits([X, y])
    fit_params = {"eval_metric": "rmse"}
    grid_search = GridSearchCV(xgb_model, param_grid, verbose=1, fit_params=fit_params, cv=kfold)
    grid_result = grid_search.fit(X, y)
    # summarize results
    print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
    means = grid_result.cv_results_['mean_test_score']
    stds = grid_result.cv_results_['std_test_score']
    params = grid_result.cv_results_['params']
    for mean, stdev, param in zip(means, stds, params):
        print("%f (%f) with: %r" % (mean, stdev, param))
        rgs = GridSearchCV(xgb_model, param_grid)
        rgs.fit(X, y)
        print(rgs.fit(X, y), flush=True)

    return mean, stdev, param, grid_result


if __name__ == '__main__':
  # 输入正确的 X, y
    XGTSearch(None, None)

报错信息:

Parameter optimization
param_grid: {'max_depth': [2, 4, 5, 6, 7, 8], 'n_estimators': [50, 100, 200, 400, 600, 800], 'learning_rate': [0.0001, 0.001, 0.01, 0.05, 0.1, 0.2]}
Traceback (most recent call last):
  File "/home/geekplusa/ai/projects/github/paddle/paddleocr/PaddleOCR/PPOCRLabel/mm/a.py", line 41, in <module>
    XGTSearch(None, None)
  File "/home/geekplusa/ai/projects/github/paddle/paddleocr/PaddleOCR/PPOCRLabel/mm/a.py", line 24, in XGTSearch
    grid_search = GridSearchCV(xgb_model, param_grid, verbose=1, fit_params=fit_params, cv=kfold)
TypeError: __init__() got an unexpected keyword argument 'fit_params'

Process finished with exit code 1

检查一下有没有调用这个函数:XGTSearch(), 我看你发的调用里没有这个函数,还有就是这一行打印没有:

print("Parameter optimization")


用debug模式运行这一段代码,走到哪一行出的问题一目了然

已经解决,return后面加了XGTSearch(data,data) ,就正常运行了

def XGTSearch(X, y):
print("Parameter optimization")
n_estimators = [50, 100, 200, 300,400]
max_depth = [ 4, 5,6,7, 8]
learning_rate = [ 0.02927, 0.03, 0.05, 0.07,0.08,0.09,0.1,0.12,0.14,0.16,0.18,0.2]
param_grid = dict(max_depth=max_depth, n_estimators=n_estimators, learning_rate=learning_rate)
print("param_grid:",param_grid)
xgb_model = XGBRegressor(objective='reg:squarederror')
kfold = TimeSeriesSplit(n_splits=3).get_n_splits([X, y])
grid_search = GridSearchCV(xgb_model, param_grid, verbose=3, cv=kfold)
grid_result = grid_search.fit(X, y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))
return mean, stdev, param, grid_result
XGTSearch(data,data)