You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
linear restriction, overparameterized models and patsy #2359
Patsy currently does not provide an factor/categorical encoding that does not drop columns that are already linear dependent available in "earlier" factor columns pydata/patsy#60 pydata/patsy#42
(discussed with Nathaniel at pycon 2015)
Alternatively, if you really want an over-parameterized model, then if you scroll down further on the docs page you linked to, it tells you how to define arbitrary custom encoding schemes.
import numpy as np
from patsy import ContrastMatrix
class FullRankOneHot(object):
def __init__(self, reference=0):
self.reference = reference
# Called to generate a full-rank encoding
def code_with_intercept(self, levels):
return ContrastMatrix(np.eye(len(levels)),
["[My.%s]" % (level,) for level in levels])
# Called to generate a non-full-rank encoding. But we don't care,
# we do what we want, and return a full-rank encoding anyway.
# Take that, patsy.
def code_without_intercept(self, levels):
return self.code_with_intercept(levels)
Then you can use it like: y ~ 1 + C(a, FullRankOneHot).
Activity
josef-pkt commentedon Jun 22, 2022
Nathaniel showed a way to use custom encoding
https://stackoverflow.com/questions/46832637/one-hot-encoding-in-patsy/46870306#46870306
need to try this out for fit_constrained
stanmart commentedon Aug 7, 2023
AFAIK
formulaicprovides support for this with theensure_full_rank=Falseandspans_intercept=Falseparameters.