Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pytest单元测试中比较XGBoost模型对象(一个已初始化/安装,另一个从文件读取)?_Python_Pytest_Xgboost - Fatal编程技术网

Python 如何在pytest单元测试中比较XGBoost模型对象(一个已初始化/安装,另一个从文件读取)?

Python 如何在pytest单元测试中比较XGBoost模型对象(一个已初始化/安装,另一个从文件读取)?,python,pytest,xgboost,Python,Pytest,Xgboost,我对用于封装XGBoost模型的类进行了简单测试。为了测试这个类,我训练了一个XGBoost模型并将其保存到文件中,我想使用这个训练过的模型,我将从文件中读取它来测试我的模型训练代码。我不确定如何才能最好地将我将使用已知参数/数据训练的XGBoost模型与我保存到文件中的模型进行比较。例如,我训练并保存了一个XGBoost模型,如下所示: # specify parameters to use for training the XGBoost model params = { 'max_

我对用于封装XGBoost模型的类进行了简单测试。为了测试这个类,我训练了一个XGBoost模型并将其保存到文件中,我想使用这个训练过的模型,我将从文件中读取它来测试我的模型训练代码。我不确定如何才能最好地将我将使用已知参数/数据训练的XGBoost模型与我保存到文件中的模型进行比较。例如,我训练并保存了一个XGBoost模型,如下所示:

# specify parameters to use for training the XGBoost model
params = {
    'max_depth': 6,  # the maximum depth of each tree
    'eta': 0.25,  # the training step for each iteration
    'silent': 1,  # logging mode - quiet
    'objective': 'reg:tweedie',
    'booster': 'gbtree',
    'subsample': 0.7,
    'gamma': 0.3,  # regularization parameter
    'colsample_bytree': 0.2,
    'rate_drop': 0.3,
    'skip_drop': 0.2,
    'early_stopping_rounds': 10,
    'eval_metric': ['rmse', 'mae'],  # error evaluation for multiclass training
}

# split X and y into train and test sets
features_train, features_test, target_train, target_test = \
    train_test_split(features, target, test_size=test_percentage, random_state=31)

# package the dataset splits as input for XGBoost
dtrain = xgb.DMatrix(features_train, label=target_train)
dtest = xgb.DMatrix(features_test, label=target_test)
evallist = [(dtest, 'eval'), (dtrain, 'train')]

# train the XGBoost model
xgbooster = xgb.train(params, dtrain, training_iterations, evallist, verbose_eval=0)
pickle.dump(xgbooster, open("/path/to/fitted_model.dat", "wb"))
在模型类的(pytest)单元测试中,我想测试我是否按照预期训练模型,因此我从文件中读取此保存的模型,以便与应该匹配的模型进行比较:

def test_xgboost_fit():

    features_train_df = pd.read_csv("/path/to/features_train.csv"))
    labels_train_df = pd.read_csv("/path/to/labels_train.csv"))
    fixture_xgbooster = pickle.load(open("/path/to/fitted_model.dat", "rb"))

    # train/fit the model
    xgbooster = mymodelclass.XGBoostModel()
    xgbooster.fit(features_train_df, labels_train_df)

    # compare the trained model against the expected model read from file
    assert xgbooster.model == fixture_xgbooster
在这里使用双等号似乎不足以进行比较(否则我还有其他问题,因为它显示了具有相同参数和拟合相同训练数据的两个模型是不相等的)


我应该如何在测试中进行比较?或者是否有更好的方法来测试此代码?

这是一个很好的问题,对其他回答感兴趣。我这样做的方法是测试参考特征矩阵的预测是否与参考
fixture\u xgbooster
模型的预测相等(或在某些范围内足够接近)。这对您的用例有效吗?或者这还不够严格/全面?谢谢你的建议,Max Power。事实上,我已经对predict函数进行了测试,但它使用的是保存/加载在这里的同一个模型,而不是我训练/拟合的模型。我在上面设计的测试是为了更全面,也就是说,将拟合/训练与预测分开。这可能有些过分,你的建议也许是一个合理的折衷方案。