(loss_function, label_type, class_count, task_type)
| 1999 | ] |
| 2000 | ) |
| 2001 | def test_custom_class_labels(loss_function, label_type, class_count, task_type): |
| 2002 | if label_type == 'boolean': |
| 2003 | assert (class_count == 2) |
| 2004 | labels = [False, True] |
| 2005 | elif label_type == 'int': |
| 2006 | labels = {2 : [1, 2], 5 : [1, 3, 2, 10, 20]}[class_count] |
| 2007 | elif label_type == 'string': |
| 2008 | labels = { |
| 2009 | 2 : ['Class1', 'Class2'], |
| 2010 | 5 : ['Class1', 'Class3', 'Class2', 'Class10', 'Class20'] |
| 2011 | }[class_count] |
| 2012 | |
| 2013 | prng = np.random.RandomState(seed=0) |
| 2014 | train_pool = Pool(prng.random_sample(size=(100, 10)), label=prng.choice(labels, size=100)) |
| 2015 | test_features = prng.random_sample(size=(50, 10)) |
| 2016 | test_label = prng.choice(labels, size=50) |
| 2017 | |
| 2018 | classifier = CatBoostClassifier(iterations=2, loss_function=loss_function, thread_count=8, task_type=task_type, gpu_ram_part=TEST_GPU_RAM_PART, devices='0') |
| 2019 | |
| 2020 | if (loss_function == 'Logloss') and (class_count != 2): |
| 2021 | with pytest.raises(CatBoostError): |
| 2022 | classifier.fit(train_pool) |
| 2023 | else: |
| 2024 | classifier.fit(train_pool) |
| 2025 | output_model_path = test_output_path(OUTPUT_MODEL_PATH) |
| 2026 | classifier.save_model(output_model_path) |
| 2027 | new_classifier = CatBoostClassifier() |
| 2028 | new_classifier.load_model(output_model_path) |
| 2029 | pred = new_classifier.predict_proba(test_features) |
| 2030 | classes = new_classifier.predict(test_features) |
| 2031 | assert pred.shape == (50, class_count) |
| 2032 | assert all(((class1 in labels) for class1 in classes)) |
| 2033 | preds_path = test_output_path(PREDS_TXT_PATH) |
| 2034 | np.savetxt(preds_path, np.array(pred), fmt='%.8f') |
| 2035 | |
| 2036 | score = new_classifier.score(test_features, test_label) |
| 2037 | |
| 2038 | score_path = test_output_path(SCORE_PATH) |
| 2039 | with open(score_path, 'w') as score_file: |
| 2040 | score_file.write(f'{score:.8f}') |
| 2041 | |
| 2042 | return [ |
| 2043 | local_canonical_file(preds_path, diff_tool=get_limited_precision_dsv_diff_tool(1e-6, False)), |
| 2044 | local_canonical_file(score_path, diff_tool=get_limited_precision_dsv_diff_tool(1e-6, False)), |
| 2045 | ] |
| 2046 | |
| 2047 | |
| 2048 | def test_multiclass_custom_class_labels_from_files(task_type): |
nothing calls this directly
no test coverage detected