MCPcopy Create free account
hub / github.com/catboost/catboost / test_shap_assert

Function test_shap_assert

catboost/python-package/ut/medium/test.py:9779–9806  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9777
9778
9779def test_shap_assert():
9780 model_path = test_output_path('model.json')
9781 pool = Pool([[0, ], [1, ], ], [0, 1])
9782 model = train(pool, {'iterations': 1, 'task_type': 'CPU', 'devices': '0'})
9783 model.save_model(model_path, format='json')
9784
9785 json_model = json.load(open(model_path))
9786 json_model['scale_and_bias'] = [1, [1]]
9787 json.dump(json_model, open(model_path, 'w'))
9788 model = CatBoost().load_model(model_path, format='json')
9789 shap_values = model.get_feature_importance(type='ShapValues', data=pool)
9790 predictions = model.predict(pool)
9791 assert (len(predictions) == len(shap_values))
9792 for i, pred_idx in enumerate(range(len(predictions))):
9793 assert (abs(sum(shap_values[pred_idx]) - predictions[pred_idx]) < 1e-9), (sum(shap_values[pred_idx]) - predictions[pred_idx])
9794
9795 json_model['oblivious_trees'] = [{
9796 'leaf_values': [1, 2],
9797 'leaf_weights': [1, 0],
9798 'splits': [{'border': 0.5, 'float_feature_index': 1, 'split_index': 0, 'split_type': 'FloatFeature'}]
9799 }]
9800 json_model['features_info'] = {
9801 'float_features': [{'borders': [0.5], 'feature_index': 0, 'flat_feature_index': 0, 'has_nans': False, 'nan_value_treatment': 'AsIs'}]
9802 }
9803
9804 json.dump(json_model, open(model_path, 'w'))
9805 model = CatBoost().load_model(model_path, format='json')
9806 model.get_feature_importance(type='ShapValues', data=pool)
9807
9808
9809@pytest.mark.parametrize('shrink_mode', ['Constant', 'Decreasing'])

Callers

nothing calls this directly

Calls 14

PoolClass · 0.90
trainFunction · 0.90
CatBoostClass · 0.90
test_output_pathFunction · 0.85
lenFunction · 0.85
save_modelMethod · 0.80
load_modelMethod · 0.80
openFunction · 0.50
rangeFunction · 0.50
absFunction · 0.50
loadMethod · 0.45

Tested by

no test coverage detected