Skip to content

Commit

Permalink
Correctyl expand penalties when cat_missing_method=convert
Browse files Browse the repository at this point in the history
  • Loading branch information
stanmart committed Jan 25, 2024
1 parent 056bf68 commit 389cb70
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/glum/_glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2717,7 +2717,9 @@ def _set_up_and_check_fit_args(

if any(X.dtypes == "category"):

def _expand_categorical_penalties(penalty, X, drop_first):
def _expand_categorical_penalties(
penalty, X, drop_first, has_missing_category
):
"""
If P1 or P2 has the same shape as X before expanding the
categoricals, we assume that the penalty at the location of
Expand All @@ -2741,19 +2743,29 @@ def _expand_categorical_penalties(penalty, X, drop_first):
chain.from_iterable(
[
elmt
for _ in dtype.categories[int(drop_first) :]
for _ in range(
len(dtype.categories)
+ has_missing_category[col]
- drop_first
)
]
if pd.api.types.is_categorical_dtype(dtype)
else [elmt]
for elmt, dtype in zip(penalty, X.dtypes)
for elmt, (col, dtype) in zip(
penalty, X.dtypes.items()
)
)
)
)
else:
return penalty

P1 = _expand_categorical_penalties(self.P1, X, self.drop_first)
P2 = _expand_categorical_penalties(self.P2, X, self.drop_first)
P1 = _expand_categorical_penalties(
self.P1, X, self.drop_first, self.has_missing_category_
)
P2 = _expand_categorical_penalties(
self.P2, X, self.drop_first, self.has_missing_category_
)

X = tm.from_pandas(
X,
Expand Down

0 comments on commit 389cb70

Please sign in to comment.