Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tensor of categoricals #26

Open
tommyod opened this issue Nov 30, 2023 · 0 comments
Open

Tensor of categoricals #26

tommyod opened this issue Nov 30, 2023 · 0 comments

Comments

@tommyod
Copy link
Owner

tommyod commented Nov 30, 2023

I want fit and transform over a Tensor of categoricals to behave like fitting each categorical.

import matplotlib.pyplot as plt
import numpy as np

from generalized_additive_models import GAM, Categorical, Spline, Tensor
from generalized_additive_models.datasets import load_powerlifters

# Load data and filter it
df = load_powerlifters().rename(columns=lambda s:s.removeprefix("best3").removesuffix("kg"))

# Melt dataframe so each exercise (squat, bench, deadlift) ends up in a row
df = df.melt(id_vars=["sex", "age", "bodyweight"], value_vars=["squat", "bench", "deadlift"], value_name="lifted", var_name="exercise")

# Predict total weight lifted, given age, bodyweight and sex
target = df["lifted"]
age = Spline("age", penalty=1e3, num_splines=8)
bodyweight = Spline("bodyweightkg", penalty=1e3, num_splines=8)
sex = Categorical("sex", penalty=0)
sex.fit_transform(df)
array([[1., 0.],
       [1., 0.],
       [0., 1.],
       ...,
       [1., 0.],
       [1., 0.],
       [1., 0.]])

However:

sex_ex_exercise = Tensor([Categorical(feature='sex', penalty=0), Categorical(feature='exercise', penalty=0)])

sex_ex_exercise.fit_transform(df)
Out[149]: 
array([[-1.51653018e-01, -1.51653018e-01,  1.33333333e+00,
        -5.15013649e-01, -5.15013649e-01,  4.18554080e-14],
       [-1.51653018e-01, -1.51653018e-01,  1.33333333e+00,
        -5.15013649e-01, -5.15013649e-01,  4.18554080e-14],
       [-4.84986351e-01, -4.84986351e-01,  2.50910404e-14,
        -1.81680315e-01, -1.81680315e-01,  1.33333333e+00],
       ...,
       [-1.51653018e-01,  1.33333333e+00, -1.51653018e-01,
        -5.15013649e-01, -1.36557432e-14, -5.15013649e-01],
       [-1.51653018e-01,  1.33333333e+00, -1.51653018e-01,
        -5.15013649e-01, -1.36557432e-14, -5.15013649e-01],
       [-1.51653018e-01,  1.33333333e+00, -1.51653018e-01,
        -5.15013649e-01, -1.36557432e-14, -5.15013649e-01]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant