xxanissrxx
xxanissrxx

Reputation: 129

Creating "inverse" interaction variables from SKLearn

I have used sklearn's preprocessing functions to create interaction variables very easily.

Here is the code:

poly=PolynomialFeatures(interaction_only = True, include_bias = False)
xtrain = poly.fit_transform(xtrain)

My data is between -1 and +1. That code has create interaction variables in the form x1 * x2. I am wondering if its possible to create x1 * (1-x2) with that code? I can't think of an easy way other than manually creating the interaction variables with a for loop.

Upvotes: 0

Views: 430

Answers (1)

FisheyJay
FisheyJay

Reputation: 450

Here's a well-written Medium article that explains how to do the inversion by defining your own log_transform function.

https://medium.com/@kylecaron/introduction-to-linear-regression-part-3-polynomial-regression-interaction-terms-and-feature-c70293d2038d

We can define a function to replicate this behavior, as well as perform the inverse function, eˣ, which returns a log transformed value back to its original value.

def log_transform(series, invert=False):
    if invert:
        return np.exp(series)-0.01
    return np.log(series+0.01)

Here’s an example with an artificial dataset with some heteroscedasticity. I will demonstrate regression on this dataset.

X = np.random.normal(5, 0.4, 200) 
y = 2*X + np.random.normal(0, 0.4, 200)
X = pd.Series(log_transform(X, invert=True), name='X')
y = pd.Series(log_transform(y, invert=True), name='y')

sns.regplot(X, y)

Upvotes: 1

Related Questions