Search code examples
pythonspline

How to you find the basis expansion matrix in python


I'm doing nonparametric regression, and need a function to expand the design matrix X into the basis matrix. Is there a package that can do this?

For example, if X is 200*10 (200 obs and 10 features), using a B-spline basis expansion with 5 bases will yield a 200*50 basis matrix.

I tried scipy.interpolate.BSpline, but it seems it does not return the basis matrix.


Solution

  • The pasty library in python is useful.

    from pasty import dmatrix
    
    transformed_x = dmatrix(
                        "bs(x, df=df, degree=degree, include_intercept=False)",
                        {"train": x}, return_type='matrix')
    

    This will return the basis expansion for a vector x. If you have a data matrix, do this for each column.