本文整理汇总了Python中scipy.interpolate.LSQUnivariateSpline方法的典型用法代码示例。如果您正苦于以下问题:Python interpolate.LSQUnivariateSpline方法的具体用法?Python interpolate.LSQUnivariateSpline怎么用?Python interpolate.LSQUnivariateSpline使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.interpolate
的用法示例。
在下文中一共展示了interpolate.LSQUnivariateSpline方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: spline_filter
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LSQUnivariateSpline [as 别名]
def spline_filter(data, nsegs):
"""Detrend a possibly periodic timeseries by fitting a coarse piecewise
smooth cubic spline
Parameters
----------
data : ndarray
list of observed values
nsegs : number
number of spline segments
Returns
-------
filtered : ndarray
"""
index = np.arange(len(data))
nknots = max(2, nsegs + 1)
knots = np.linspace(index[0], index[-1], nknots + 2)[1:-2]
return LSQUnivariateSpline(index, data, knots)(index)
示例2: update_normal_vector
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LSQUnivariateSpline [as 别名]
def update_normal_vector(self):
"""
update the constraint by approximating the
loglikelihood hypersurface as a spline in
each dimension.
This is an approximation which
improves as the algorithm proceeds
"""
n = self.ensemble[0].dimension
tracers_array = np.zeros((len(self.ensemble),n))
for i,samp in enumerate(self.ensemble):
tracers_array[i,:] = samp.values
V_vals = np.atleast_1d([p.logL for p in self.ensemble])
self.normal = []
for i,x in enumerate(tracers_array.T):
# sort the values
# self.normal.append(lambda x: -x)
idx = x.argsort()
xs = x[idx]
Vs = V_vals[idx]
# remove potential duplicate entries
xs, ids = np.unique(xs, return_index = True)
Vs = Vs[ids]
# pick only finite values
idx = np.isfinite(Vs)
Vs = Vs[idx]
xs = xs[idx]
# filter to within the 90% range of the Pvals
Vl,Vh = np.percentile(Vs,[5,95])
(idx,) = np.where(np.logical_and(Vs > Vl,Vs < Vh))
Vs = Vs[idx]
xs = xs[idx]
# Pick knots for this parameters: Choose 5 knots between
# the 1st and 99th percentiles (heuristic tuning WDP)
knots = np.percentile(xs,np.linspace(1,99,5))
# Guesstimate the length scale for numerical derivatives
dimwidth = knots[-1]-knots[0]
delta = 0.1 * dimwidth / len(idx)
# Apply a Savtzky-Golay filter to the likelihoods (low-pass filter)
window_length = len(idx)//2+1 # Window for Savtzky-Golay filter
if window_length%2 == 0: window_length += 1
f = savgol_filter(Vs, window_length,
5, # Order of polynominal filter
deriv=1, # Take first derivative
delta=delta, # delta for numerical deriv
mode='mirror' # Reflective boundary conds.
)
# construct a LSQ spline interpolant
self.normal.append(LSQUnivariateSpline(xs, f, knots, ext = 3, k = 3))
if self.DEBUG: np.savetxt('dlogL_spline_%d.txt'%i,np.column_stack((xs,Vs,self.normal[-1](xs),f)))