| 58 | |
| 59 | |
| 60 | def create_cd( |
| 61 | label=None, |
| 62 | cat_features=None, |
| 63 | text_features=None, |
| 64 | embedding_features=None, |
| 65 | weight=None, |
| 66 | baseline=None, |
| 67 | doc_id=None, |
| 68 | group_id=None, |
| 69 | subgroup_id=None, |
| 70 | timestamp=None, |
| 71 | auxiliary_columns=None, |
| 72 | feature_names=None, |
| 73 | output_path='train.cd' |
| 74 | ): |
| 75 | _from_param_to_cd = { |
| 76 | 'label': 'Label', |
| 77 | 'weight': 'Weight', |
| 78 | 'baseline': 'Baseline', |
| 79 | 'doc_id': 'DocId', |
| 80 | 'group_id': 'GroupId', |
| 81 | 'subgroup_id': 'SubgroupId', |
| 82 | 'timestamp': 'Timestamp' |
| 83 | } |
| 84 | _column_description = defaultdict(lambda: ['Num', '']) |
| 85 | for key, value in locals().copy().items(): |
| 86 | if not (key.startswith('_') or value is None): |
| 87 | if key in ('cat_features', 'text_features', 'embedding_features', 'auxiliary_columns'): |
| 88 | if isinstance(value, int): |
| 89 | value = [value] |
| 90 | for index in value: |
| 91 | if not isinstance(index, int): |
| 92 | raise CatBoostError('Unsupported index type. Expected int, got {}'.format(type(index))) |
| 93 | if index in _column_description: |
| 94 | raise CatBoostError('The index {} occurs more than once'.format(index)) |
| 95 | if key == 'cat_features': |
| 96 | _column_description[index] = ['Categ', ''] |
| 97 | elif key == 'text_features': |
| 98 | _column_description[index] = ['Text', ''] |
| 99 | elif key == 'embedding_features': |
| 100 | _column_description[index] = ['NumVector', ''] |
| 101 | else: |
| 102 | _column_description[index] = ['Auxiliary', ''] |
| 103 | elif key not in ('feature_names', 'output_path'): |
| 104 | if not isinstance(value, int): |
| 105 | raise CatBoostError('Unsupported index type. Expected int, got {}'.format(type(value))) |
| 106 | if value in _column_description: |
| 107 | raise CatBoostError('The index {} occurs more than once'.format(value)) |
| 108 | _column_description[value] = [_from_param_to_cd[key], ''] |
| 109 | if feature_names is not None: |
| 110 | for feature_column_index, name in feature_names.items(): |
| 111 | if _column_description[feature_column_index][0] not in ('Num', 'Categ', 'Text', 'NumVector'): |
| 112 | raise CatBoostError('feature_names contains index {} that does not correspond to feature column'.format(feature_column_index)) |
| 113 | _column_description[feature_column_index][1] = name |
| 114 | with open(fspath(output_path), 'w') as f: |
| 115 | for index, (title, name) in sorted(_column_description.items()): |
| 116 | f.write('{}\t{}\t{}\n'.format(index, title, name)) |
| 117 | |