Generalized Linear Mixed Model Encoder

class category_encoders.glmm.GLMMEncoder(verbose=0, cols=None, drop_invariant=False, return_df=True, handle_unknown='value', handle_missing='value', random_state=None, randomized=False, sigma=0.05, binomial_target=None)[source]

Generalized linear mixed model.

Supported targets: binomial and continuous. For polynomial target support, see PolynomialWrapper.

This is a supervised encoder similar to TargetEncoder or MEstimateEncoder, but there are some advantages: 1) Solid statistical theory behind the technique. Mixed effects models are a mature branch of statistics. 2) No hyper-parameters to tune. The amount of shrinkage is automatically determined through the estimation process. In short, the less observations a category has and/or the more the outcome varies for a category then the higher the regularization towards “the prior” or “grand mean”. 3) The technique is applicable for both continuous and binomial targets. If the target is continuous, the encoder returns regularized difference of the observation’s category from the global mean. If the target is binomial, the encoder returns regularized log odds per category.

In comparison to JamesSteinEstimator, this encoder utilizes generalized linear mixed models from statsmodels library.

Note: This is an alpha implementation. The API of the method may change in the future.

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 encoded 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_missing: str

options are ‘return_nan’, ‘error’ and ‘value’, defaults to ‘value’, which returns 0.

handle_unknown: str

options are ‘return_nan’, ‘error’ and ‘value’, defaults to ‘value’, which returns 0.

randomized: bool,

adds normal (Gaussian) distribution noise into training data in order to decrease overfitting (testing data are untouched).

sigma: float

standard deviation (spread or “width”) of the normal distribution.

binomial_target: bool

if True, the target must be binomial with values {0, 1} and Binomial mixed model is used. If False, the target must be continuous and Linear mixed model is used. If None (the default), a heuristic is applied to estimate the target type.



Data Analysis Using Regression and Multilevel/Hierarchical Models, page 253, from


fit(X, y, **kwargs)

Fit encoder according to X and binary y.

fit_transform(X[, y])

Encoders that utilize the target must make sure that the training data are transformed with:


Returns the names of all transformed / added columns.


Get parameters for this estimator.


Set the parameters of this estimator.

transform(X[, y, override_return_df])

Perform the transformation to new categorical data.

fit(X, y, **kwargs)[source]

Fit encoder according to X and binary y.

Xarray-like, shape = [n_samples, n_features]

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

yarray-like, shape = [n_samples]

Binary target values.


Returns self.


Returns the names of all transformed / added columns.

feature_names: list

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

transform(X, y=None, override_return_df=False)[source]

Perform the transformation to new categorical data.

When the data are used for model training, it is important to also pass the target in order to apply leave one out.

Xarray-like, shape = [n_samples, n_features]
yarray-like, shape = [n_samples] when transform by leave one out

None, when transform without target information (such as transform test set)

parray, shape = [n_samples, n_numeric + N]

Transformed values with encoding applied.