Other Features
Apart from training and using Deep Networks for tabular data, PyTorch Tabular also has some cool features which can help your classical ML/ sci-kit learn pipelines
Categorical Embeddings¶
The CategoryEmbedding Model can also be used as a way to encode your categorical columns. instead of using a One-hot encoder or a variant of TargetMean Encoding, you can use a learned embedding to encode your categorical features. And all this can be done using a scikit-learn style Transformer.
Usage Example¶
# passing the trained model as an argument
transformer = CategoricalEmbeddingTransformer(tabular_model)
# passing the train dataframe to extract the embeddings and replace categorical features
# defined in the trained tabular_model
train_transformed = transformer.fit_transform(train)
# using the extracted embeddings on new dataframe
val_transformed = transformer.transform(val)
pytorch_tabular.categorical_encoders.CategoricalEmbeddingTransformer
¶
Bases: BaseEstimator
, TransformerMixin
Source code in src/pytorch_tabular/categorical_encoders.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
|
__init__(tabular_model)
¶
Initializes the Transformer and extracts the neural embeddings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tabular_model |
TabularModel
|
The trained TabularModel object |
required |
Source code in src/pytorch_tabular/categorical_encoders.py
fit(X, y=None)
¶
fit_transform(X, y=None)
¶
Encode given columns of X based on the learned embedding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
DataFrame
|
DataFrame of features, shape (n_samples, n_features). Must contain columns to encode. |
required |
y |
[type]
|
Only for compatibility. Not used. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The encoded dataframe |
Source code in src/pytorch_tabular/categorical_encoders.py
transform(X, y=None)
¶
Transforms the categorical columns specified to the trained neural embedding from the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
DataFrame
|
DataFrame of features, shape (n_samples, n_features). Must contain columns to encode. |
required |
y |
[type]
|
Only for compatibility. Not used. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
[description] |
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The encoded dataframe |
Source code in src/pytorch_tabular/categorical_encoders.py
Feature Extractor¶
What if you want to use the features learnt by the Neural Network in your ML model? Pytorch Tabular let's you do that as well, and with ease. Again, a scikit-learn style Transformer does the job for you.
Usage Example¶
# passing the trained model as an argument
dt = DeepFeatureExtractor(tabular_model)
# passing the train dataframe to extract the last layer features
# here `fit` is there only for compatibility and does not do anything
enc_df = dt.fit_transform(train)
# using the extracted embeddings on new dataframe
val_transformed = transformer.transform(val)
pytorch_tabular.feature_extractor.DeepFeatureExtractor
¶
Bases: BaseEstimator
, TransformerMixin
Source code in src/pytorch_tabular/feature_extractor.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
__init__(tabular_model, extract_keys=['backbone_features'], drop_original=True)
¶
Initializes the Transformer and extracts the neural features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tabular_model |
TabularModel
|
The trained TabularModel object |
required |
extract_keys |
list
|
The keys of the features to extract. Defaults to ["backbone_features"]. |
['backbone_features']
|
drop_original |
bool
|
Whether to drop the original columns. Defaults to True. |
True
|
Source code in src/pytorch_tabular/feature_extractor.py
fit(X, y=None)
¶
fit_transform(X, y=None)
¶
Encode given columns of X based on the learned features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
DataFrame
|
DataFrame of features, shape (n_samples, n_features). Must contain columns to encode. |
required |
y |
[type]
|
Only for compatibility. Not used. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: The encoded dataframe |
Source code in src/pytorch_tabular/feature_extractor.py
load_from_object_file(path)
¶
Loads the feature extractor from a pickle file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path to load the file from |
required |
Source code in src/pytorch_tabular/feature_extractor.py
save_as_object_file(path)
¶
Saves the feature extractor as a pickle file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path to save the file |
required |
Source code in src/pytorch_tabular/feature_extractor.py
transform(X, y=None)
¶
Transforms the categorical columns specified to the trained neural features from the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
DataFrame
|
DataFrame of features, shape (n_samples, n_features). Must contain columns to encode. |
required |
y |
[type]
|
Only for compatibility. Not used. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
[description] |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: The encoded dataframe |