How to add interaction term in Python sklearn

后端 未结 3 1797
余生分开走
余生分开走 2021-02-01 05:03

If I have independent variables [x1, x2, x3] If I fit linear regression in sklearn it will give me something like this:

y = a*x1 + b*x2 + c*x3 + intercept
         


        
3条回答
  •  既然无缘
    2021-02-01 05:50

    Use patsy to construct a design matrix as follows:

    y, X = dmatrices('y ~ x1 + x2 + x3 + x1:x2', your_data)
    

    Where your_data is e.g. a DataFrame with response column y and input columns x1, x2 and x3.

    Then just call the fit method of your estimator, e.g. LinearRegression().fit(X,y).

提交回复
热议问题