繁体   English   中英

线性拟合scipy.optimize.curve_fit

[英]Linear fit with scipy.optimize.curve_fit

我上周刚刚开始编程,所以请保持温和;)

我尝试做的是使用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不能乘以列表和标量。 这就是你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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM