Backward Difference Coding

class category_encoders.backward_difference.BackwardDifferenceEncoder(verbose=0, cols=None, mapping=None, drop_invariant=False, return_df=True, handle_unknown='value', handle_missing='value')[source]

Backward difference contrast coding for encoding categorical variables.

Parameters:
verbose: int

integer indicating verbosity of the output. 0 for none.

cols: list

a list of columns to encode, if None, all string columns will be encoded.

drop_invariant: bool

boolean for whether or not to drop columns with 0 variance.

return_df: bool

boolean for whether to return a pandas DataFrame from transform (otherwise it will be a numpy array).

handle_unknown: str

options are ‘error’, ‘return_nan’, ‘value’, and ‘indicator’. The default is ‘value’. Warning: if indicator is used, an extra column will be added in if the transform matrix has unknown categories. This can cause unexpected changes in dimension in some cases.

handle_missing: str

options are ‘error’, ‘return_nan’, ‘value’, and ‘indicator’. The default is ‘value’. Warning: if indicator is used, an extra column will be added in if the transform matrix has nan values. This can cause unexpected changes in dimension in some cases.

References

[R39fb50438075-1]Contrast Coding Systems for Categorical Variables, from

https://stats.idre.ucla.edu/r/library/r-library-contrast-coding-systems-for-categorical-variables/

[R39fb50438075-2]Gregory Carey (2003). Coding Categorical Variables, from

http://psych.colorado.edu/~carey/Courses/PSYC5741/handouts/Coding%20Categorical%20Variables%202006-03-03.pdf

Methods

fit(self, X[, y]) Fits an ordinal encoder to produce a consistent mapping across applications and optionally finds generally invariant columns to drop consistently.
fit_transform(self, X[, y]) Fit to data, then transform it.
get_feature_names(self) Returns the names of all transformed / added columns.
get_params(self[, deep]) Get parameters for this estimator.
set_params(self, \*\*params) Set the parameters of this estimator.
transform(self, X[, override_return_df]) Perform the transformation to new categorical data.
backward_difference_coding  
fit_backward_difference_coding  
static backward_difference_coding(X_in, mapping)[source]
fit(self, X, y=None, **kwargs)[source]

Fits an ordinal encoder to produce a consistent mapping across applications and optionally finds generally invariant columns to drop consistently.

Parameters:
X : array-like, shape = [n_samples, n_features]

Training vectors, where n_samples is the number of samples and n_features is the number of features.

y : array-like, shape = [n_samples]

Target values.

Returns:
self : encoder

Returns self.

get_feature_names(self)[source]

Returns the names of all transformed / added columns.

Returns:
feature_names: list

A list with all feature names transformed or added. Note: potentially dropped features are not included!

transform(self, X, override_return_df=False)[source]

Perform the transformation to new categorical data.

Parameters:
X : array-like, shape = [n_samples, n_features]
Returns:
p : array, shape = [n_samples, n_numeric + N]

Transformed values with encoding applied.