[SciPy-User] B-spline basis functions?

Charles R Harris charlesr.harris at gmail.com
Tue Jul 31 11:48:06 EDT 2012


On Tue, Jul 31, 2012 at 9:46 AM, Charles R Harris <charlesr.harris at gmail.com
> wrote:

>
>
> On Tue, Jul 31, 2012 at 9:37 AM, Nathaniel Smith <njs at pobox.com> wrote:
>
>> Hi all,
>>
>> I'd like to be able to do spline regression in patsy[1], which means
>> that I need to be able to compute b-spline basis functions. I am not
>> an initiate into the mysteries of practical spline computations, but I
>> *think* the stuff in scipy.signal is not quite usable as is, because
>> it's focused on doing interpolation directly rather than exposing the
>> basis functions themselves?
>>
>> Specifically, to achieve feature parity with R [2], I need to be able to
>> take
>>   - an arbitrary order
>>   - an arbitrary collection of knot positions (which may be irregularly
>> spaced)
>>   - a vector x of points at which to evaluate the basis functions
>> and spit out the value of each spline basis function evaluated at each
>> point in the x vector.
>>
>> It looks like scipy.signal.bspline *might* be useful, but I can't
>> quite tell? Or alternatively someone might have some code lying around
>> to do this already?
>>
>> Basically I have a copy of Schumaker here and I'm hoping someone will
>> save me from having to read it :-).
>>
>>
> I have this floating around
>
> def splvander(x, deg, knots):
>     """Vandermonde type matrix for splines.
>
>     Returns a matrix whose columns are the values of the b-splines of deg
>     `deg` associated with the knot sequence `knots` evaluated at the points
>     `x`.
>
>     Parameters
>     ----------
>     x : array_like
>         Points at which to evaluate the b-splines.
>     deg : int
>         Degree of the splines.
>     knots : array_like
>         List of knots. The convention here is that the interior knots have
>         been extended at both ends by ``deg + 1`` extra knots.
>
>     Returns
>     -------
>     vander : ndarray
>         Vandermonde like matrix of shape (m,n), where ``m = len(x)`` and
>         ``m = len(knots) - deg - 1``
>
>     Notes
>     -----
>     The knots exending the interior points are usually taken to be the same
>     as the endpoints of the interval on which the spline will be evaluated.
>
>     """
>     m = len(knots) - deg - 1
>     v = np.zeros((m, len(x)))
>     d = np.eye(m, len(knots))
>     for i in range(m):
>         v[i] = spl.splev(x, (knots, d[i], deg))
>     return v.T
>
>
With this import

from scipy.interpolate import fitpack as spl

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20120731/365d31c5/attachment.html>


More information about the SciPy-User mailing list