3
votes

I'm using Python 2.7 and Scikit-learn to fit a dataset using multiplicate linear regression, where the different terms are multiplied together instead of added together like in sklearn.linear_models.Ridge.

So instead of

y = c1 * X1 + c2 * X2 + c3 * X3 + ...

we need

y = c1 * X1 * c2 * X2 * c3 * X3...

Can we enable Python and Sklearn to fit and predict such a multiplicative/hedonic regression model?

2
hmmmm I think I may have done something like this recently. Can you post more information on how you are applying this and what the data is like? - Ryan Saxe

2 Answers

4
votes

I think you should be able to do this with regular linear regression by manipulating your input data set (data matrix).

The regression y ~ c1 * X1 * c2 * X2 *... is equivalent to y ~ k * (X1 * X2 *...) where k is some constant

So if you multiply all of the values in your design matrix together, then regress on that, I think you should be able to do this.

i.e. if your data matrix, X, is 4 x 1000 with features X1, X2, X3, and X4, use a pre-processing step to create a new matrix X_new, that is 1 x 1000 where the single column equals X1 * X2 * X3 * X4, then fit y ~ X_new (clf = LinearRegression(), clf.fit(X_new,y))

0
votes

Here's what you need. X is a matrix of all X values.

Y is a matrix or a vector for all Y values.

degree is the highest degree you allow for the formula. Like X^2 has a degree of two, while X1^2 * X2^3 has a degree of 5. You'll need to decide this yourself.

from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model

poly = PolynomialFeatures(degree=degree)
X_ = poly.fit_transform(X)
model = linear_model.LinearRegression(fit_intercept=True)
model.fit(X_, Y)