malmgrek / gammy

:octopus: Generalized additive models in Python with a Bayesian twist
MIT License
77 stars 4 forks source link

How do you fit more than one feature? #4

Closed nickcorona closed 3 years ago

malmgrek commented 3 years ago

For example by defining the term types like so

a = gammy.ExpSquared1d( grid=np.arange(0, 1, 10), corrlen=1, sigma=1, energy=0.999 ) b = gammy.ExpSquared1d( grid=np.arange(0, 1, 10), corrlen=0.1, sigma=0.1, energy=0.999 )

and setting

formula = a(x[:, 0]) + b(x[:, 1])

and so on -- as in the readme examples. The above formulation assumes that the input data is (at least) of shape N x 2.

nickcorona commented 3 years ago

For example by defining the term types like so

a = gammy.ExpSquared1d( grid=np.arange(0, 1, 10), corrlen=1, sigma=1, energy=0.999 ) b = gammy.ExpSquared1d( grid=np.arange(0, 1, 10), corrlen=0.1, sigma=0.1, energy=0.999 )

and setting

formula = a(x[:, 0]) + b(x[:, 1])

and so on -- as in the readme examples. The above formulation assumes that the input data is (at least) of shape N x 2.

What if you have many features? Do you still have to define the formula in this way? Or is there a way to define the formula without explicitly indexing each feature in the matrix?

malmgrek commented 3 years ago

If I had to create an additive model for a larger (known) number of features, I would try either of the following:

Using list comprehension:

import gammy
import numpy as np

x = gammy.arraymapper.x

# simulate data
N = 10
input_data = np.random.rand(N, N)
y = np.random.rand(N)

f = gammy.ExpSquared1d(grid=np.arange(0, 1, 0.1), corrlen=1, sigma=1)
formula = gammy.Sum([f(x[:, n]) for n in range(N)])
model = gammy.BayesianGAM(formula).fit(input_data, y)
# model.predict(input_data)

Using reduce:

from functools import reduce
formula = reduce(lambda S, n: S + f(x[:, n]), range(1, N), f(x[:, 0]))
...

I quickly tested both of the approaches, and they seemed to work (at least technically).

Currently there is no shortcut or vectorized alternative method.

Did I try to answer the right question?

malmgrek commented 3 years ago

Closing the issue as there is no more discussion.