【问题标题】:Is there a function for finding the furthest data point from a linear line of best fit scatter graph是否有从最佳拟合散点图的线性线中找到最远数据点的功能
【发布时间】:2020-03-04 09:42:44
【问题描述】:

尝试从最佳拟合的线性线中找到最远的数据点,以便可以用作初始猜测以进行额外的高斯拟合。需要最远的数据点,然后可以将其用作优化的粗略平均猜测。需要数据点的 x 值。 def fit_func(x,a,mu,sig,m,c): #用于高斯和线性拟合的函数 gaus = a*sp.exp(-(x-mu)2/(2*sig2)) 线 = m*x+c 返回高斯+线 定义最大化(x,y,m,c): 距离=(sp.sqrt(x2+y2)-sp.sqrt(x**2+((m*x+c)**2))) max_xval=[sp.argmax(距离)] 返回 max_xval 对于 A 中的 f: 以 open(f,'r') 作为文件: line1=file.readline() line1_split=line1.split(',') if 'Instrument Response: Good ' in line1_split[3]: #filtering good data from bad 打印(f) 打印(line1_split[3]) #good_file_data.append(f) J,Z=sp.loadtxt(f,delimiter=',', skiprows=2,unpack=True) #读取好的数据 plt.plot(J,Z) 最大化(J,Z,(Z[999]-Z[0])/(J[999]-J[0]),50) initial_guess=[30,最大化,1,(Z[999]-Z[0])/(J[999]-J[0]),50] po,po_cov=sp.optimize.curve_fit(fit_func,J,Z,initial_guess, maxfev=2000000000) #绘制最佳拟合线和散点图 plt.plot(J,fit_func(J,po[0],po[1],po[2],po[3],po[4])) plt.plot(sp.unique(J), sp.poly1d(sp.polyfit(J, Z, 1))(sp.unique(J))) K=[J,Z] plt.xlabel('波长/nm') plt.ylabel('强度/AU') plt.show() 取回一个 float 类型错误,并且函数作为 Maximize 没有返回值。

【问题讨论】:

  • 使用vector projection,您可以使用线性代数将所有点投影到最佳拟合线上(这在python实现中应该很快,因为它只是矩阵/向量操作。然后您可以定义每个点的距离作为减去一个点的两个向量时得到的向量的大小(前/后投影)。请参阅我的链接中a_2 的定义
  • 已完成但尚未格式化为代码,抱歉。对 SO 很陌生

标签: python curve-fitting scatter-plot


【解决方案1】:

我将假设 xvals、yvals 和 yfit 都是数组。假设我在这里没有遗漏什么,你可以尝试这样的事情:

import numpy as np

# create an array of delta values
delta_fit = abs(yvals - yfit)

# find the index where the maximum delta point is located
max_idx = np.argmax(delta_fit)

# get the xvals value at max_idx
max_xval = xvals[max_idx]

这假设您的数组具有相同的形状,并且您有兴趣确定与给定索引位置的拟合的最大距离。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2021-02-27
    • 2014-04-09
    • 1970-01-01
    相关资源
    最近更新 更多