This is my first time using BSpline, and I want to fit a curve to my data points. I've tried using Univariate Spline and attempted to use splev and splrep but I'd really like to learn how to do this using BSpline.
It looks like my fitting is really choppy and the line isn't even going through the points.
arraymagU = linspace(U_timeband.min(),U_timeband.max(),300) #array for my x data points
UfunctionBS = BSpline(U_timeband,U_magband,k=4,extrapolate=False)
arraymagU2 = UfunctionBS(arraymagU)
plt.plot(arraymagU,arraymagU2)
U_timeband is my x coordinates and U_magband is simply my y. k=4 I think indicates a cubic fit? I've played around with that value and it doesn't seem to make it better.
It produces this:
How can I make this better, consistent? I think I may have to define breakpoints, but I'm not sure how to do that either.
B-spline or basis spline is a curve approximation method based on given coefficients. B-spline requires the parameters such as knots, spline coefficients, and degree of a spline. The SciPy API provides BSpline class to implement the B-spline fitting for a given dataset.
The cubic B-spline class provided by boost allows fast and accurate interpolation of a function which is known at equally spaced points. The cubic B-spline interpolation is numerically stable as it uses compactly supported basis functions constructed via iterative convolution.
A B-spline of order is a piecewise polynomial function of degree in a variable . It is defined over locations , called knots or breakpoints, which must be in non-descending order .
We often have a dataset comprising of data following a general path, but each data has a standard deviation which makes them scattered across the line of best fit. We can get a single line using curve-fit () function. Scipy is the scientific computing module of Python providing in-built functions on a lot of well-known Mathematical functions.
Short answer: Long answer: scipy separates the steps involved in spline interpolation into two operations, most likely for computational efficiency. The coefficients describing the spline curve are computed, using splrep(). splrep returns an array of tuples containing the coefficients.
Scipy is the scientific computing module of Python providing in-built functions on a lot of well-known Mathematical functions. The scipy.optimize package equips us with multiple optimization procedures.
This Python tutorial will teach you how to use the “ Python Scipy Curve Fit ” method to fit data to various functions, including exponential and gaussian, and will go through the following topics. What is Curve Fit in Scipy? The curve_fit () method of module scipy.optimize that apply non-linear least squares to fit the data to a function.
splrep returns a tuple (t,c,k)
containing the vector of knots, the B-spline coefficients, and the degree of the spline. These can be fed to interpolate.BSpline
to create a BSpline object:
import numpy as np
import scipy.interpolate as interpolate
import matplotlib.pyplot as plt
x = np.array([ 0. , 1.2, 1.9, 3.2, 4. , 6.5])
y = np.array([ 0. , 2.3, 3. , 4.3, 2.9, 3.1])
t, c, k = interpolate.splrep(x, y, s=0, k=4)
print('''\
t: {}
c: {}
k: {}
'''.format(t, c, k))
N = 100
xmin, xmax = x.min(), x.max()
xx = np.linspace(xmin, xmax, N)
spline = interpolate.BSpline(t, c, k, extrapolate=False)
plt.plot(x, y, 'bo', label='Original points')
plt.plot(xx, spline(xx), 'r', label='BSpline')
plt.grid()
plt.legend(loc='best')
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With