Encode categorical features as a one-hot numeric array. The input to this transformer should be an array-like of integers or strings, denoting the values taken on by categorical (discrete) features. The features are encoded using a one-hot (aka 'one-of-K' or 'dummy') encoding s
| 472 | |
| 473 | |
| 474 | class OneHotEncoder(_BaseEncoder): |
| 475 | """ |
| 476 | Encode categorical features as a one-hot numeric array. |
| 477 | |
| 478 | The input to this transformer should be an array-like of integers or |
| 479 | strings, denoting the values taken on by categorical (discrete) features. |
| 480 | The features are encoded using a one-hot (aka 'one-of-K' or 'dummy') |
| 481 | encoding scheme. This creates a binary column for each category and |
| 482 | returns a sparse matrix or dense array (depending on the ``sparse_output`` |
| 483 | parameter). |
| 484 | |
| 485 | By default, the encoder derives the categories based on the unique values |
| 486 | in each feature. Alternatively, you can also specify the `categories` |
| 487 | manually. |
| 488 | |
| 489 | This encoding is needed for feeding categorical data to many scikit-learn |
| 490 | estimators, notably linear models and SVMs with the standard kernels. |
| 491 | |
| 492 | Note: a one-hot encoding of y labels should use a LabelBinarizer |
| 493 | instead. |
| 494 | |
| 495 | Read more in the :ref:`User Guide <preprocessing_categorical_features>`. |
| 496 | For a comparison of different encoders, refer to: |
| 497 | :ref:`sphx_glr_auto_examples_preprocessing_plot_target_encoder.py`. |
| 498 | |
| 499 | Parameters |
| 500 | ---------- |
| 501 | categories : 'auto' or a list of array-like, default='auto' |
| 502 | Categories (unique values) per feature: |
| 503 | |
| 504 | - 'auto' : Determine categories automatically from the training data. |
| 505 | - list : ``categories[i]`` holds the categories expected in the ith |
| 506 | column. The passed categories should not mix strings and numeric |
| 507 | values within a single feature, and should be sorted in case of |
| 508 | numeric values. |
| 509 | |
| 510 | The used categories can be found in the ``categories_`` attribute. |
| 511 | |
| 512 | .. versionadded:: 0.20 |
| 513 | |
| 514 | drop : {'first', 'if_binary'} or an array-like of shape (n_features,), \ |
| 515 | default=None |
| 516 | Specifies a methodology to use to drop one of the categories per |
| 517 | feature. This is useful in situations where perfectly collinear |
| 518 | features cause problems, such as when feeding the resulting data |
| 519 | into an unregularized linear regression model. |
| 520 | |
| 521 | However, dropping one category breaks the symmetry of the original |
| 522 | representation and can therefore induce a bias in downstream models, |
| 523 | for instance for penalized linear classification or regression models. |
| 524 | |
| 525 | - None : retain all features (the default). |
| 526 | - 'first' : drop the first category in each feature. If only one |
| 527 | category is present, the feature will be dropped entirely. |
| 528 | - 'if_binary' : drop the first category in each feature with two |
| 529 | categories. Features with 1 or more than 2 categories are |
| 530 | left intact. |
| 531 | - array : ``drop[i]`` is the category in feature ``X[:, i]`` that |
searching dependent graphs…