1- """Backward difference contrast encoding"""
1+ """Backward difference contrast encoding. """
22
3- from patsy .contrasts import Diff , ContrastMatrix
43import numpy as np
4+ from patsy .contrasts import ContrastMatrix , Diff
55
66from category_encoders .base_contrast_encoder import BaseContrastEncoder
77
@@ -13,31 +13,39 @@ class BackwardDifferenceEncoder(BaseContrastEncoder):
1313
1414 Parameters
1515 ----------
16-
1716 verbose: int
1817 integer indicating verbosity of the output. 0 for none.
1918 cols: list
2019 a list of columns to encode, if None, all string columns will be encoded.
2120 drop_invariant: bool
2221 boolean for whether or not to drop columns with 0 variance.
2322 return_df: bool
24- boolean for whether to return a pandas DataFrame from transform (otherwise it will be a numpy array).
23+ boolean for whether to return a pandas DataFrame from transform
24+ (otherwise it will be a numpy array).
2525 handle_unknown: str
26- options are 'error', 'return_nan', 'value', and 'indicator'. The default is 'value'. Warning: if indicator is used,
27- an extra column will be added in if the transform matrix has unknown categories. This can cause
28- unexpected changes in dimension in some cases.
26+ options are 'error', 'return_nan', 'value', and 'indicator'. The default is 'value'.
27+ Warning: if indicator is used, an extra column will be added in if the transform matrix
28+ has unknown categories. This can cause unexpected changes in dimension in some cases.
2929 handle_missing: str
30- options are 'error', 'return_nan', 'value', and 'indicator'. The default is 'value'. Warning: if indicator is used,
31- an extra column will be added in if the transform matrix has nan values. This can cause
32- unexpected changes in dimension in some cases.
30+ options are 'error', 'return_nan', 'value', and 'indicator'. The default is 'value'.
31+ Warning: if indicator is used, an extra column will be added in if the transform
32+ matrix has nan values. This can cause unexpected changes in dimension in some cases.
3333
3434 Example
3535 -------
3636 >>> from category_encoders import *
3737 >>> import pandas as pd
3838 >>> from sklearn.datasets import fetch_openml
39- >>> bunch = fetch_openml(name="house_prices", as_frame=True)
40- >>> display_cols = ["Id", "MSSubClass", "MSZoning", "LotFrontage", "YearBuilt", "Heating", "CentralAir"]
39+ >>> bunch = fetch_openml(name='house_prices', as_frame=True)
40+ >>> display_cols = [
41+ ... 'Id',
42+ ... 'MSSubClass',
43+ ... 'MSZoning',
44+ ... 'LotFrontage',
45+ ... 'YearBuilt',
46+ ... 'Heating',
47+ ... 'CentralAir',
48+ ... ]
4149 >>> y = bunch.target
4250 >>> X = pd.DataFrame(bunch.data, columns=bunch.feature_names)[display_cols]
4351 >>> enc = BackwardDifferenceEncoder(cols=['CentralAir', 'Heating']).fit(X, y)
@@ -46,12 +54,11 @@ class BackwardDifferenceEncoder(BaseContrastEncoder):
4654 <class 'pandas.core.frame.DataFrame'>
4755 RangeIndex: 1460 entries, 0 to 1459
4856 Data columns (total 12 columns):
49- # Column Non-Null Count Dtype
50- --- ------ -------------- -----
51- 0 intercept 1460 non-null int64
57+ # Column Non-Null Count Dtype
58+ --- ------ -------------- -----
5259 1 Id 1460 non-null float64
5360 2 MSSubClass 1460 non-null float64
54- 3 MSZoning 1460 non-null object
61+ 3 MSZoning 1460 non-null object
5562 4 LotFrontage 1201 non-null float64
5663 5 YearBuilt 1460 non-null float64
5764 6 Heating_0 1460 non-null float64
@@ -76,5 +83,5 @@ class BackwardDifferenceEncoder(BaseContrastEncoder):
7683 """
7784
7885 def get_contrast_matrix (self , values_to_encode : np .array ) -> ContrastMatrix :
86+ """Get the contrast matrix for the backward difference encoder."""
7987 return Diff ().code_without_intercept (values_to_encode )
80-
0 commit comments