Python:如何使一行适合特定的数据间隔?
我正在尝试将一条线与我的数据集的9.0到10.0 um区域相匹配。以下是我的情节: 不幸的是,这是一个散点图,其中的Python:如何使一行适合特定的数据间隔?,python,scipy,curve-fitting,scipy-optimize,Python,Scipy,Curve Fitting,Scipy Optimize,我正在尝试将一条线与我的数据集的9.0到10.0 um区域相匹配。以下是我的情节: 不幸的是,这是一个散点图,其中的x值没有从小到大进行索引,因此我不能仅将optimize.curve\u fit函数应用于特定的索引范围,以获得x值中所需的范围 下面是我的曲线拟合步骤。我如何修改它,使其只适合9.0到10.0 umx-值范围(在我的例子中,是x_dist变量),该变量的点随机分布在索引中 def func(x,a,b): #
x
值没有从小到大进行索引,因此我不能仅将optimize.curve\u fit
函数应用于特定的索引范围,以获得x
值中所需的范围
下面是我的曲线拟合步骤。我如何修改它,使其只适合9.0到10.0 umx
-值范围(在我的例子中,是x_dist
变量),该变量的点随机分布在索引中
def func(x,a,b): # Define your fitting function
return a*x+b
initialguess = [-14.0, 0.05] # initial guess for the parameters of the function func
fit, covariance = optimize.curve_fit( # call to the fitting routine curve_fit. Returns optimal values of the fit parameters, and their estimated variance
func, # function to fit
x_dist, # data for independant variable
xdiff_norm, # data for dependant variable
initialguess, # initial guess of fit parameters
) # uncertainty in dependant variable
print("linear coefficient:",fit[0],"+-",np.sqrt(covariance[0][0])) #print value and one std deviation of first fit parameter
print("offset coefficient:",fit[1],"+-",np.sqrt(covariance[1][1])) #print value and one std deviation of second fit parameter
print(covariance)
您正确地识别出问题的产生是因为您的x值数据没有排序。你可以用不同的方式解决这个问题。一种方法是使用布尔掩码过滤掉不需要的值。我尽量接近你的例子:
from matplotlib import pyplot as plt
import numpy as np
from scipy import optimize
#fake data generation
np.random.seed(1234)
arr = np.linspace(0, 15, 100).reshape(2, 50)
arr[1, :] = np.random.random(50)
arr[1, 20:45] += 2 * arr[0, 20:45] -5
rng = np.random.default_rng()
rng.shuffle(arr, axis = 1)
x_dist = arr[0, :]
xdiff_norm = arr[1, :]
def func(x, a, b):
return a * x + b
initialguess = [5, 3]
mask = (x_dist>2.5) & (x_dist<6.6)
fit, covariance = optimize.curve_fit(
func,
x_dist[mask],
xdiff_norm[mask],
initialguess)
plt.scatter(x_dist, xdiff_norm, label="data")
x_fit = np.linspace(x_dist[mask].min(), x_dist[mask].max(), 100)
y_fit = func(x_fit, *fit)
plt.plot(x_fit, y_fit, c="red", label="fit")
plt.legend()
plt.show()
样本输出(毫不奇怪,差别不大):
您的输入是什么?numpy阵列?您是否知道要限制拟合的索引或值范围?如果是前者,那么
curve\u fit(func,x\u dist[start:stop],xdiff\u norm[start:stop],…
就足够了。@Mr.T这是一个numpy数组。我的问题是,如果我调用一个索引范围,它将与我的x值中的范围不对应。如果我选择[200:250],我调用的50个索引将在我的整个范围内随机分布xvalue,这没有多大帮助。我的一个想法是将索引从最小的xvalue排序到最大的xvalue,然后我可以调用特定的索引范围。
from matplotlib import pyplot as plt
import numpy as np
from scipy import optimize
#fake data generation
np.random.seed(1234)
arr = np.linspace(0, 15, 100).reshape(2, 50)
arr[1, :] = np.random.random(50)
arr[1, 20:45] += 2 * arr[0, 20:45] -5
rng = np.random.default_rng()
rng.shuffle(arr, axis = 1)
x_dist = arr[0, :]
xdiff_norm = arr[1, :]
def func(x, a, b):
return a * x + b
#find indexes of a sorted x_dist array, then sort both arrays based on this index
ind = x_dist.argsort()
x_dist = x_dist[ind]
xdiff_norm = xdiff_norm[ind]
#identify index where linear range starts for normal array indexing
start = np.argmax(x_dist>2.5)
stop = np.argmax(x_dist>6.6)
initialguess = [5, 3]
fit, covariance = optimize.curve_fit(
func,
x_dist[start:stop],
xdiff_norm[start:stop],
initialguess)
plt.plot(x_dist, xdiff_norm, label="data")
x_fit = np.linspace(x_dist[start], x_dist[stop], 100)
y_fit = func(x_fit, *fit)
plt.plot(x_fit, y_fit, c="red", ls="--", label="fit")
plt.legend()
plt.show()