本文整理汇总了Python中scipy.optimize.least_squares函数的典型用法代码示例。如果您正苦于以下问题:Python least_squares函数的具体用法?Python least_squares怎么用?Python least_squares使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了least_squares函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: multi_curveFitting_2
def multi_curveFitting_2(least_func, avg, seed, min_range=5):
cost = []
#param1 = np.ones((n_param, 300))
#param2 = np.ones((n_param, 300))
x_range = np.linspace(1, 300, 300)
for n in range( int(300/min_range) - 1) : # iteration for all data
# print("iteration ", n)
x1 = x_range[:min_range*(n+1)]
x2 = x_range[min_range*(n+1):]
#print('\n\n - x1:', x1)
#print(' - x2:', x2)
y1 = avg[:min_range*(n+1)]
y2 = avg[min_range*(n+1):]
lsq1 = least_squares(least_func, seed, args=(x1, y1))
lsq2 = least_squares(least_func, seed, args=(x2, y2))
#param1[:, n] = lsq1.x
#param2[:, n] = lsq2.x
cost.append(lsq1.cost+lsq2.cost)
idx = np.argmin(cost)
return min_range*(idx+1)#, param1[:, idx], param2[:, idx]
示例2: curve_Fitting
def curve_Fitting(least_func, curve_func, x, y, seed, file_path, clt_num):
fig, ax = plt.subplots(1, 1, figsize=(6,4))
# popt, pcov = curve_fit(func, x, y, maxfev = 1000000)
'''
upper_bound = []
lower_bound = []
for i in range(len(pcov)):
upper_bound.append(popt[i] + pcov[i,i])
lower_bound.append(popt[i] - pcov[i,i])
'''
x_fit = np.linspace(0, 16, 100)
'''
if seed == 1:
lsq = least_squares(least_func, seed, args=(x, y))
y_mean = curve_func(x_fit, lsq.x)
cost = lsq.cost
'''
if len(seed) == 1:
lsq = least_squares(least_func, seed, args=(x, y))
y_mean = curve_func(x_fit, lsq.x)
cost = lsq.cost
if len(seed) == 2:
lsq = least_squares(least_func, seed, args=(x, y))
y_mean = curve_func(x_fit, lsq.x[0], lsq.x[1])
cost = lsq.cost
elif len(seed) == 3:
lsq = least_squares(least_func, seed, args=(x, y))
y_mean = curve_func(x_fit, lsq.x[0], lsq.x[1], lsq.x[2])
cost = lsq.cost
elif len(seed) == 4:
lsq = least_squares(least_func, seed, args=(x, y))
y_mean = curve_func(x_fit, lsq.x[0], lsq.x[1], lsq.x[2], lsq.x[3])
cost = lsq.cost
print(" - Curve Fitting Parameters: {0}".format(lsq.x))
print(" - Curve Fitting Cost: {0}\n".format(cost))
ax.plot(x, y, 'rx', label="average score")
ax.plot(x_fit, y_mean, 'b-', label="curve fitting")
'''
for i in range(len(x_fit)):
if i == 0:
ax.plot([x_fit[i], x_fit[i]], [y_lower[i], y_upper[i]], 'b-', label="variance")
else:
ax.plot([x_fit[i], x_fit[i]], [y_lower[i], y_upper[i]], 'b-')
'''
ax.set_ylim([0, max(y)+0.2])
ax.legend(fontsize=14)
ax.set_title("Cluster {0} (Cost {1})".format(clt_num, round(cost, 2)))
# ax.text(0.77, 0.03, "cost: {0}".format(round(cost, 2)), horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes, fontsize=15)
fig.savefig(file_path, dpi=100)
return lsq.x, cost
示例3: test_solver_selection
def test_solver_selection(self):
sparse = BroydenTridiagonal(mode='sparse')
dense = BroydenTridiagonal(mode='dense')
res_sparse = least_squares(sparse.fun, sparse.x0, jac=sparse.jac,
method=self.method)
res_dense = least_squares(dense.fun, dense.x0, jac=dense.jac,
method=self.method)
assert_allclose(res_sparse.cost, 0, atol=1e-20)
assert_allclose(res_dense.cost, 0, atol=1e-20)
assert_(issparse(res_sparse.jac))
assert_(isinstance(res_dense.jac, np.ndarray))
示例4: test_numerical_jac
def test_numerical_jac(self):
p = BroydenTridiagonal()
for jac in ['2-point', '3-point', 'cs']:
res_dense = least_squares(p.fun, p.x0, jac, method=self.method)
res_sparse = least_squares(
p.fun, p.x0, jac,method=self.method,
jac_sparsity=p.sparsity)
assert_equal(res_dense.nfev, res_sparse.nfev)
assert_allclose(res_dense.x, res_sparse.x, atol=1e-20)
assert_allclose(res_dense.cost, 0, atol=1e-20)
assert_allclose(res_sparse.cost, 0, atol=1e-20)
示例5: test_in_bounds
def test_in_bounds(self):
for jac in ['2-point', '3-point', 'cs', jac_trivial]:
res = least_squares(fun_trivial, 2.0, jac=jac,
bounds=(-1.0, 3.0), method=self.method)
assert_allclose(res.x, 0.0, atol=1e-4)
assert_equal(res.active_mask, [0])
assert_(-1 <= res.x <= 3)
res = least_squares(fun_trivial, 2.0, jac=jac,
bounds=(0.5, 3.0), method=self.method)
assert_allclose(res.x, 0.5, atol=1e-4)
assert_equal(res.active_mask, [-1])
assert_(0.5 <= res.x <= 3)
示例6: test_numerical_jac
def test_numerical_jac(self):
p = BroydenTridiagonal()
with warnings.catch_warnings():
warnings.simplefilter('ignore', UserWarning)
for jac in ['2-point', '3-point', 'cs']:
res_dense = least_squares(p.fun, p.x0, jac, method=self.method)
res_sparse = least_squares(
p.fun, p.x0, jac,method=self.method,
jac_sparsity=p.sparsity)
assert_equal(res_dense.nfev, res_sparse.nfev)
assert_allclose(res_dense.x, res_sparse.x, atol=1e-20)
assert_allclose(res_dense.cost, 0, atol=1e-20)
assert_allclose(res_sparse.cost, 0, atol=1e-20)
示例7: test_equivalence
def test_equivalence(self):
sparse = BroydenTridiagonal(mode='sparse')
dense = BroydenTridiagonal(mode='dense')
res_sparse = least_squares(
sparse.fun, sparse.x0, jac=sparse.jac,
method=self.method)
res_dense = least_squares(
dense.fun, dense.x0, jac=sparse.jac,
method=self.method)
assert_equal(res_sparse.nfev, res_dense.nfev)
assert_allclose(res_sparse.x, res_dense.x, atol=1e-20)
assert_allclose(res_sparse.cost, 0, atol=1e-20)
assert_allclose(res_dense.cost, 0, atol=1e-20)
示例8: test_equivalence
def test_equivalence(self):
sparse = BroydenTridiagonal(mode='sparse')
dense = BroydenTridiagonal(mode='dense')
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
res_sparse = least_squares(
sparse.fun, sparse.x0, jac=sparse.jac,
method=self.method)
res_dense = least_squares(
dense.fun, dense.x0, jac=sparse.jac,
method=self.method)
assert_equal(res_sparse.nfev, res_dense.nfev)
assert_allclose(res_sparse.x, res_dense.x, atol=1e-20)
assert_allclose(res_sparse.cost, 0, atol=1e-20)
assert_allclose(res_dense.cost, 0, atol=1e-20)
示例9: test_bounds_shape
def test_bounds_shape(self):
for jac in ['2-point', '3-point', 'cs', jac_2d_trivial]:
x0 = [1.0, 1.0]
res = least_squares(fun_2d_trivial, x0, jac=jac)
assert_allclose(res.x, [0.0, 0.0])
res = least_squares(fun_2d_trivial, x0, jac=jac,
bounds=(0.5, [2.0, 2.0]), method=self.method)
assert_allclose(res.x, [0.5, 0.5])
res = least_squares(fun_2d_trivial, x0, jac=jac,
bounds=([0.3, 0.2], 3.0), method=self.method)
assert_allclose(res.x, [0.3, 0.2])
res = least_squares(
fun_2d_trivial, x0, jac=jac, bounds=([-1, 0.5], [1.0, 3.0]),
method=self.method)
assert_allclose(res.x, [0.0, 0.5], atol=1e-5)
示例10: test_diff_step
def test_diff_step(self):
# res1 and res2 should be equivalent.
# res2 and res3 should be different.
res1 = least_squares(fun_trivial, 2.0, diff_step=1e-1,
method=self.method)
res2 = least_squares(fun_trivial, 2.0, diff_step=-1e-1,
method=self.method)
res3 = least_squares(fun_trivial, 2.0,
diff_step=None, method=self.method)
assert_allclose(res1.x, 0, atol=1e-4)
assert_allclose(res2.x, 0, atol=1e-4)
assert_allclose(res3.x, 0, atol=1e-4)
assert_equal(res1.x, res2.x)
assert_equal(res1.nfev, res2.nfev)
assert_(res2.nfev != res3.nfev)
示例11: robust_l2
def robust_l2(obs_phase, freqs, solve_cs=True):
'''Solve the tec and cs for multiple datasets.
`obs_phase` : `numpy.ndarray`
the measured phase with shape (num_freqs, )
`freqs` : `numpy.ndarray`
the frequencies at the datapoints (num_freqs,)
`solve_cs` : (optional) bool
Whether to solve cs (True)
'''
obs_phase = phase_unwrapp1d(obs_phase)
if solve_cs:
def residuals(m, freqs, obs_phase):
tec,cs = m[0],m[1]
return calc_phase(tec,freqs,cs=cs) - obs_phase
else:
def residuals(m, freqs, obs_phase):
tec,cs = m[0],m[1]
return calc_phase(tec,freqs,cs=0.) - obs_phase
nan_mask = np.bitwise_not(np.isnan(obs_phase))
obs_phase_ = obs_phase[nan_mask]
freqs_ = freqs[nan_mask]
m0 = [0.0, 0.]
m = least_squares(residuals,m0,loss='soft_l1',f_scale=90.*np.pi/180.,args=(freqs_,obs_phase_))
if solve_cs:
return m.x[0], m.x[1]
else:
return m.x[0], 0.
示例12: solve_van_genuchten_1986
def solve_van_genuchten_1986(self, D=0.01, R=0.01, ftol=1e-10,
max_nfev=1000, **kwargs):
"""
Scipy optimize method to solve least squares
for van genuchten 1986. Miscable displacement.
Parameters:
----------
:param float D: Diffusivity initial guess. Cannot be 0
:param float R: Retardation initial guess. Cannot be 0
:param float ftol: scipy function tolerance for solution
:param int max_nfev: maximum number of function iterations
:param **kwargs: scipy least squares kwargs
Returns:
-------
:return: scipy least squares dictionary. Answer in dict['x']
"""
from scipy.optimize import least_squares
l = self.ylen * self.resolution
v = self.uy
t = self.bt['nts'].as_matrix() * self.timestep
bt = self.bt['ncpr'].as_matrix() / self.ncol
x0 = np.array([D, R])
return least_squares(self.__van_genuchten_residuals, x0,
args=(l, v, t, bt),
ftol=ftol, max_nfev=max_nfev,
**kwargs)
示例13: inference_cEWRGt
def inference_cEWRGt(W, thresh):
k = (W>0).sum(axis=0) # degrees
s = W.sum(axis=0) # strength
#from scipy.optimize import root
from scipy.optimize import least_squares
x0=np.concatenate([k,s])*1E-4 # initial solution
# Initialize least squares from previous solution
sollm = least_squares(lambda v: eq(v,thresh,k,s),
x0=x0,
bounds= (0,np.inf),
method='trf',
ftol=1E-8,
xtol=1E-8,
verbose=1)
sollm = root(lambda z: eq(z,thresh,k,s),
x0=x0,
method='lm',
options={'xtol':1E-30,'gtol':1E-30,'ftol':1E-30},
tol=1E-6)
#print('Final cost', sollm['cost'])
sollm = sollm['x']
n2 = int(len(sollm)//2)
x,y = sollm[0:n2],sollm[n2:]
return x, y
示例14: test_fun
def test_fun(self):
# Test that res.fun is actual residuals, and not modified by loss
# function stuff.
for loss in LOSSES:
res = least_squares(fun_trivial, 2.0, loss=loss,
method=self.method)
assert_equal(res.fun, fun_trivial(res.x))
示例15: optimize_diffusion_simp_parameters_with_bounds
def optimize_diffusion_simp_parameters_with_bounds(
params_guess_dict, params_bounds_dict, const_params_dict,
exp_time_array, exp_x_array, exp_temp_array,
x_array, num_steps, time_step, finite_step_method, sim_fpath,
lsq_fn=_lsq_func_simp, figsize=FIG_SIZE,
):
params_guess = np.array([v for k, v in sorted(params_guess_dict.items())])
if params_bounds_dict is None:
bounds = None
else:
pgi = sorted(params_bounds_dict.items())
lower_bounds = np.array([v[0] for k, v in pgi])
upper_bounds = np.array([v[1] for k, v in pgi])
bounds = (lower_bounds, upper_bounds)
iter_fn = count()
if figsize is not None:
fig = plt.figure(figsize=figsize)
ax = fig.add_subplot(111)
fit_lines = list()
return least_squares(
fun=lsq_fn, x0=params_guess, bounds=bounds, verbose=2,
args=(
const_params_dict, params_guess_dict.keys(),
exp_time_array, exp_x_array, exp_temp_array,
x_array, num_steps, time_step,
finite_step_method, sim_fpath, iter_fn, fig, ax, fit_lines
),
)