【问题标题】:Linear fit with scipy.optimize.curve_fit使用 scipy.optimize.curve_fit 进行线性拟合
【发布时间】:2016-04-22 22:01:57
【问题描述】:

我上周才开始编程,所以请温柔;)

我尝试使用 curve_fit 进行线性拟合,以确定对斜率的两个贡献。我试过了:

import os
from os import listdir
from os.path import isfile, join
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.axes as ax 
from scipy import asarray as ar,exp

freq_lw_tuple = [('10.0', 61.32542701946727), ('10.5', 39.367147015318501), ('11.0', 58.432147581817077), ('11.5', 68.819336676144317), ('12.0', 71.372078906193025), ('12.5', 73.113662907539336), ('13.0', 75.855316075062603), ('13.5', 76.798724771266322), ('14.0', 79.065657225891329), ('14.5', 81.637345805693897), ('15.0', 82.407248034320034)]
def func_lw(x_lw,alpha,g):
    return (alpha/g)*x_lw

#define constants
m_e = 9.109383*(10**(-31)) #mass of electron in [kg]
q_e = 1.602176*(10**(-19)) #value of electron charge
pi = 3.14159               #pi
#unzip the list of tuples
unzipped = list(zip(*freq_lw_tuple))

xval_list = []
yval_list = []
for k in range(0,len(unzipped[0])):
    x_value = (8/np.sqrt(3))*((2*pi*m_e*float(unzipped[0][k])*10**9)/q_e)   #calculate x values
    xval_list.append(x_value)
    y_value = unzipped[1][k]*10**(-4)*4*pi*10**(-7)    #transform unit of y values
    yval_list.append(y_value)


start_params2 = [0.01,2]
fitted_params, pcov = curve_fit(func_lw, xval_list, yval_list, start_params2)

它实际上给出了一些结果,但是当我想要时

print(func_lw(xval_list,*fitted_params))

我只是得到一个空列表,可能这就是我不能的原因

plt.plot(xval_list, func_lw(xval_list, *fitted_params))

(这会产生一个错误,例如:x 和 y 必须具有相同的第一维)

[编辑:为 freq_lw_tuple 和导入添加了一些数据]

【问题讨论】:

  • 你的“线性”函数有一个斜率(由两个参数组成!)但没有偏移
  • 在代码中写入你的导入和一些值,以便其他人可以复制粘贴代码并尝试运行它
  • 感谢您的回复!我知道,但它仍然是线性的,对吧?斜率的两个参数是我感兴趣的。如果这就是你的意思,我添加了一些数据。哦,还有进口……对不起

标签: python curve-fitting


【解决方案1】:

Python 不能将列表和标量相乘。 这就是您导入from scipy import asarray as ar 的原因。您也可以使用numpy。 所以当你调用func_lw 时,你应该给它一个数组,而不是一个列表。

func_lw(ar(xval_list), fitted_params[0], fitted_params[1] )
plt.plot(ar(xval_list), func_lw(ar(xval_list), *fitted_params))
plt.scatter(ar(xval_list), ar(yval_list))

关于优化函数,如果在公式中使用(alpha/g)*x_lw,则实际上只有一个参数(斜率)。我会改用

def func_lw(x_lw, slope, offset):
    return slope*x_lw + offset

编辑:顺便说一句,您应该使用“列表理解”,并执行类似的操作

xval_list = [(8/np.sqrt(3))*((2*pi*m_e*float(x)*10**9)/q_e) for x, y in freq_lw_tuple]
yval_list = [y*10**(-4)*4*pi*10**(-7) for x, y in freq_lw_tuple]

【讨论】:

  • 注意:仍有方法可以折射和压缩列表理解的两行,但我认为在你的学习曲线的这个阶段就可以了 :-)
  • 非常感谢您的帮助!
猜你喜欢
  • 2020-06-07
  • 2014-01-02
  • 1970-01-01
  • 2019-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 2018-11-22
相关资源
最近更新 更多