因此,我的目标是将自己的数据点拟合为黑体曲线,但是遇到了困难。
我正在做的概要是
http://python4esac.github.io/fitting/example_blackbody.html
但他们使用随机数据,我正在尝试使用自己的CSV数据。
此数据为:
Wavelength
0.7,
0.865,
1.24,
1.61,
3.7,
4.05,
Radiance
0,
0.106718,
0.227031,
0.373527,
0.240927,
0.293215,
有没有让Python进入文件并使用这两列呢?到目前为止,我所尝试的一切都失败了。
我的代码如下
import csv
with open('PythonCode1.csv', 'rb') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
from scipy.optimize import curve_fit
import pylab as plt
from pylab import plotfile, show, gca
fid=open('PythonCode1.csv','r')
import numpy as np
import matplotlib.cbook as cbook
def blackbody_lam(lam, T):
""" Blackbody as a function of wavelength (um) and temperature (K).
"""
from scipy.constants import h,k,c
lam = 1e-6 * lam # convert to metres
return 2*h*c**2 / (lam**5 * (np.exp(h*c / (lam*k*T)) - 1))
wa = np.linspace(0.1, 6, 100) # wavelengths in um
T1 = 1000.
T2 = 2500.
y1 = blackbody_lam(wa, T1)
y2 = blackbody_lam(wa, T2)
ytot = y1 + y2
sigma = np.ones(len(wa)) * 1 * np.median(ytot)
ydata = ytot + csv.row[1].randn(len(wa)) * sigma
and returns
%run "d:\temp\k1339544\tmpskczzu.py"
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
R:\Canpy103.001\Canopy32\App\appdata\canopy-1.0.3.1262.win-x86\lib\site- packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
174 else:
175 filename = fname
--> 176 exec compile(scripttext, filename, 'exec') in glob, loc
177 else:
178 def execfile(fname, *where):
d:\temp\k1339544\tmpskczzu.py in <module>()
27
28 sigma = np.ones(len(wa)) * 1 * np.median(ytot)
---> 29 ydata = ytot + csv.rows[1].randn(len(wa)) * sigma
30
31 # plot the input model and synthetic data
AttributeError: 'module' object has no attribute 'rows'
答案 0 :(得分:1)
您可以提供自己的数据,而不是像设置示例中那样将wa
和ydata
设置为人工数据。
如果您将数据格式化为两列:
Wavelength,Radiance
0.7,0
0.865,0.106718
1.24,0.227031
1.61,0.373527
3.7,0.240927
4.05,0.293215
然后你可以用
来摄取它data = np.loadtxt("PythonCode1.csv", delimiter=",", skiprows=1)
wa = data[:,0]
ydata = data[:,1]
这不能保证该功能可以适合您的数据(它不能,黑体功能缺少可调节的比例参数)或者适合会收敛,但这是最简单的方法来获得数据。