Python:使用插值函数时遇到问题 [英] Python : Having trouble with using the interpolate function
本文介绍了Python:使用插值函数时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
# Imported array of data from a text file. This code works with no problems.
q1, a1 = loadtxt("values.txt", unpack = True, skiprows = 1)
print q1
print a1
# Creating a while loop for this part of the code. This code works with no problems.
a = 3
b = -2
c = -9
q = 0.5
qt = 0.1
while q < 1.5:
print q, a
q += qt
a = a + b*qt
b = b + c*qt
# This interpolation does not work. I am unable to figure it out.
# TypeError: object of type 'float' has no len()
from scipy.interpolate import interp1d
f = interp1d(q,a,'cubic')
q1 = linspace(0.5,1.4,25)
a1 = f(q1)
plot(q1,a1, '-', q,a, 'o')
show()
谢谢。
Thanks.
推荐答案
问题是interp1d
需要数组(或类似数组的数组),以及q
和a
都是普通浮点数。要使功能正常工作,你还需要数组中至少两个元素。
我没有使用scipy,我不确定你的意图是什么,所以我不能提供一个精确的解决方案,但这就是你的类型错误的来源。
The issue is thatinterp1d
expects arrays (or array like values), and bothq
anda
are plain floats. For the function to work you also need at least two elements in the array.
I've not used scipy and I'm not sure what your intent is so I can't offer an exact solution, but that's where your type error is coming from.
这篇关于Python:使用插值函数时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文