Running your code and then plotting
plt.plot(x, lasso_predict)
plt.plot(x, y)
shows a pretty good correspondence (r^2 = .97)!
I think the problem is that Xpoly does not contain raw polynomials as you
use for prediction.
If you do
p_lasso = a[0] + a[1] * Xpoly[:, 1] + a[2] * Xpoly[:, 2] + a[3] * Xpoly[:,
3]
then it works. The polynomials in Xpoly are actually not pure monomials,
and they are scaled with a certain rule in order not to explode.
On Sun, Jun 29, 2014 at 6:53 AM, Fernando Paolo <fpa...@ucsd.edu> wrote:
> Hello,
>
> I must be missing something obvious because I can't find the "actual"
> coefficients of the polynomial fitted using LassoCV. That is, for a 3rd
> degree polynomial
>
> p = a0 + a1 * x + a2 * x^2 + a3 * x^3
>
> I want the a0, a1, a2 and a3 coefficients (as those returned by
> numpy.polyfit()). Here is an example code of what I'm after
>
> import numpy as np
> import matplotlib.pyplot as plt
> from pandas import *
> from math import *
> from patsy import dmatrix
> from sklearn.linear_model import LassoCV
>
> sin_data = DataFrame({'x' : np.linspace(0, 1, 101)})
> sin_data['y'] = np.sin(2 * pi * sin_data['x']) + np.random.normal(0, 0.1,
> 101)
> x = sin_data['x']
> y = sin_data['y']
> Xpoly = dmatrix('C(x, Poly)')
>
> n = 3
> lasso_model = LassoCV(cv=15, copy_X=True, normalize=True)
> lasso_fit = lasso_model.fit(Xpoly[:,1:n+1], y)
> lasso_predict = lasso_model.predict(Xpoly[:,1:n+1])
>
> a = np.r_[lasso_fit.intercept_, lasso_fit.coef_]
>
> b = np.polyfit(x, y, n)[::-1]
>
> p_lasso = a[0] + a[1] * x + a[2] * x**2 + a[3] * x**3
> p_polyfit = b[0] + b[1] * x + b[2] * x**2 + b[3] * x**3
>
> print 'coef. lasso:', a
> print 'coef. polyfit:', b
>
>
> The returned coefficients 'a' and 'b' are completely different, and while
> 'p_polyfit' is indeed the fitted polynomial of degree 3, 'p_lasso' makes no
> sense (plot to see). Unless 'b' is something else... If so, what actually
> are the coefficients returned by fit()? And how can I get the coefficients
> that reconstruct the fitted polynomial?
>
> Thank you,
> -fernando
>
> PS: If this is mentioned in any documentation, I couldn't find it.
>
>
>
> --
> Fernando Paolo
> Institute of Geophysics & Planetary Physics
> Scripps Institution of Oceanography
> University of California, San Diego
> 9500 Gilman Drive
> La Jolla, CA 92093-0225
>
>
> ------------------------------------------------------------------------------
> Open source business process management suite built on Java and Eclipse
> Turn processes into business applications with Bonita BPM Community Edition
> Quickly connect people, data, and systems into organized workflows
> Winner of BOSSIE, CODIE, OW2 and Gartner awards
> http://p.sf.net/sfu/Bonitasoft
> _______________________________________________
> Scikit-learn-general mailing list
> Scikit-learn-general@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/scikit-learn-general
>
>
------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Scikit-learn-general mailing list
Scikit-learn-general@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/scikit-learn-general