Scikit-Learn-based Stages
PdPipeline stages dependent on the scikit-learn Python library.
Please note that the scikit-learn Python package must be installed for the stages in this module to work.
When attempting to load stages from this module, pdpipe will first attempt to import sklearn. If it fails, it will issue a warning, will not import any of the pipeline stages that make up this module, and continue to load other pipeline stages.
Classes
Encode
Bases: ColumnsBasedPipelineStage
A pipeline stage that encodes categorical columns to integer values.
The encoder for each column is saved in the attribute 'encoders', which is a dict mapping each encoded column name to the sklearn.preprocessing.LabelEncoder object used to encode it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
single label, list-like or callable, default None
|
Column labels in the DataFrame to be encoded. If columns is None then
all the columns with object or category dtype will be converted, except
those given in the exclude_columns parameter. Alternatively,
this parameter can be assigned a callable returning an iterable of
labels from an input pandas.DataFrame. See |
None
|
exclude_columns |
single label, list-like or callable, default None
|
Label or labels of columns to be excluded from encoding. If None then
no column is excluded. Alternatively, this parameter can be assigned a
callable returning an iterable of labels from an input
pandas.DataFrame. See |
None
|
drop |
bool, default True
|
If set to True, the source columns are dropped after being encoded, and the resulting encoded columns retain the names of the source columns. Otherwise, encoded columns gain the suffix '_enc'. |
True
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Attributes:
Name | Type | Description |
---|---|---|
encoders |
dict
|
A dictionary mapping each encoded column name to the corresponding sklearn.preprocessing.LabelEncoder object. Empty object if not fitted. |
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> data = [[3.2, "acd"], [7.2, "alk"], [12.1, "alk"]]
>>> df = pd.DataFrame(data, [1,2,3], ["ph","lbl"])
>>> encode_stage = pdp.Encode("lbl")
>>> encode_stage(df)
ph lbl
1 3.2 0
2 7.2 1
3 12.1 1
>>> encode_stage.encoders["lbl"].inverse_transform([0,1,1])
array(['acd', 'alk', 'alk'], dtype=object)
Source code in pdpipe/sklearn_stages.py
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
Attributes
encoders = {}
instance-attribute
Scale
Bases: ColumnsBasedPipelineStage
A pipeline stage that scales data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scaler |
str
|
The type of scaler to use to scale the data. One of 'StandardScaler', 'MinMaxScaler', 'MaxAbsScaler', 'RobustScaler', 'QuantileTransformer' and 'Normalizer'. Refer to scikit-learn's documentation for usage. |
required |
columns |
single label, list-like or callable, default None
|
Column labels in the DataFrame to be scaled. If columns is None then
all columns of numeric dtype will be scaled, except those given in the
exclude_columns parameter. Alternatively, this parameter can be
assigned a callable returning an iterable of labels from an input
pandas.DataFrame. See |
None
|
exclude_columns |
single label, list-like or callable, default None
|
Label or labels of columns to be excluded from encoding. Alternatively,
this parameter can be assigned a callable returning an iterable of
labels from an input pandas.DataFrame. See |
None
|
joint |
bool, default False
|
If set to True, all scaled columns will be scaled as a single value set (meaning, only the single largest value among all input columns will be scaled to 1, and not the largest one for each column). |
False
|
**kwargs |
extra keyword arguments
|
All valid extra keyword arguments are forwarded to the scaler constructor on scaler creation (e.g. 'n_quantiles' for QuantileTransformer). PdPipelineStage valid keyword arguments are used to override Scale class defaults. |
{}
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Attributes:
Name | Type | Description |
---|---|---|
scaler |
sklearn._OneToOneFeatureMixin
|
A scikit-learn scaler object. |
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> data = [[3.2, 0.3], [7.2, 0.35], [12.1, 0.29]]
>>> df = pd.DataFrame(data, [1,2,3], ["ph","gt"])
>>> scale_stage = pdp.Scale("StandardScaler")
>>> scale_stage(df)
ph gt
1 -1.181449 -0.508001
2 -0.082427 1.397001
3 1.263876 -0.889001
Source code in pdpipe/sklearn_stages.py
151 152 153 154 155 156 157 158 159 160 161 162 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 271 272 273 274 275 276 277 |
|
Attributes
scaler = scaler
instance-attribute
TfidfVectorizeTokenLists
Bases: PdPipelineStage
A pipeline stage TFIDF-vectorizing a token-list column to count columns.
Every cell in the input columns is assumed to be a list of strings, each representing a single token. The resulting TF-IDF vector is exploded into individual columns, each with the label 'lbl_i' where lbl is the original column label and i is the index of column in the count vector.
The resulting columns are concatenated to the end of the dataframe.
All valid sklearn.feature_extraction.text.TfidfVectorizer keyword arguments can be provided as keyword arguments to the constructor, except 'input' and 'analyzer', which will be ignored. As usual, all valid PdPipelineStage constructor parameters can also be provided as keyword arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column |
str
|
The label of the token-list column to TfIdf-vectorize. |
required |
drop |
bool, default True
|
If set to True, the source column is dropped after being transformed. |
True
|
hierarchical_labels |
bool, default False
|
If set to True, the labels of resulting columns are of the form 'P_F' where P is the label of the original token-list column and F is the feature name (i.e. the string token it corresponds to). Otherwise, it is simply the feature name itself. If you plan to have two different TfidfVectorizeTokenLists pipeline stages vectorizing two different token-list columns, you should set this to true, so tf-idf features originating in different text columns do not overwrite one another. |
False
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> data = [[2, ['hovercraft', 'eels']], [5, ['eels', 'urethra']]]
>>> df = pd.DataFrame(data, [1, 2], ['Age', 'tokens'])
>>> tfvectorizer = pdp.TfidfVectorizeTokenLists('tokens')
>>> tfvectorizer(df)
Age eels hovercraft urethra
1 2 0.579739 0.814802 0.000000
2 5 0.579739 0.000000 0.814802
Source code in pdpipe/sklearn_stages.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|
Decompose
Bases: ColumnsBasedPipelineStage
A stage applying dimensionality reduction through matrix decomposition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
transformer |
sklearn.TransformerMixin
|
An instance of a matrix decomposer transformer from the
|
required |
columns |
single label, list-like or callable, default None
|
Column labels in the DataFrame to be transformer. If columns is None
then all columns of numeric dtype will be scaled, except those given
in the exclude_columns parameter. Alternatively, this parameter can be
assigned a callable returning an iterable of labels from an input
pandas.DataFrame. See |
None
|
exclude_columns |
single label, list-like or callable, default None
|
Label or labels of columns to be excluded from encoding. Alternatively,
this parameter can be assigned a callable returning an iterable of
labels from an input pandas.DataFrame. See |
None
|
drop |
bool, default True
|
If set to True, decomposed columns are dropped, and the resulting set
of columns are concatenated to all un-transformed columns, with
matching column labels (see the |
True
|
lbl_format |
str, optional
|
An f-string with a single {} slot, used to generated post-decomposition column labels. For example, 'pca{:0>3}' will yield columns 'pca000', 'pca001', etc. If not provided, the default 'mdc{}' is used. |
None
|
**kwargs |
extra keyword arguments
|
PdPipelineStage valid keyword arguments are used to override Decompose class defaults. All other extra keyword arguments are forwarded to the transformer constructor on transformer creation. |
{}
|
Attributes:
Name | Type | Description |
---|---|---|
transformer |
sklearn.TransformerMixin
|
The transformer instance used to perform the decomposition. |
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> from sklearn.decomposition import PCA
>>> data = [[3, 1, 1], [7, 2, 4], [8, 3, 1]]
>>> df = pd.DataFrame(data, [1,2,3], ["a", "b", "c"])
>>> pca_stage = pdp.Decompose(PCA(), n_components=2)
>>> pca_stage(df)
mdc0 mdc1
1 3.313301 -0.148453
2 -1.432127 1.717269
3 -1.881174 -1.568816
Source code in pdpipe/sklearn_stages.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
|
Attributes
transformer = transformer
instance-attribute
EncodeLabel
Bases: PdPipelineStage
A pipeline stage that encodes the input label series to integer values.
The encoder for each column is saved in the attribute 'encoder', which
is a dict mapping each encoded column name to the
The used sklearn.preprocessing.LabelEncoder
object is saved in the
encoder_
attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Attributes:
Name | Type | Description |
---|---|---|
encoder_ |
sklearn.preprocessing.LabelEncoder
|
The sklearn.preprocessing.LabelEncoder object used to encode the series label. |
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> data = [[3.2, 31], [7.2, 33], [12.1, 28]]
>>> X = pd.DataFrame(data, [1,2,3], ["ph","temp"])
>>> y = pd.Series(["acd", "alk", "alk"])
>>> encode_stage = pdp.EncodeLabel()
>>> X, y = encode_stage(X, y)
>>> X
ph temp
1 3.2 31
2 7.2 33
3 12.1 28
>>> y
1 0
2 1
3 1
dtype: int...
>>> encode_stage.encoder_.inverse_transform([0,1,1])
array(['acd', 'alk', 'alk'], dtype=object)