[英]I get errors when I run my code for the interpolation function
As you can see below, I have typed my code and the first two blocks works fine. 正如您在下面看到的那样,我已经键入了代码,并且前两个块可以正常工作。 But, when I get to the interpolation, I start to have problems.
但是,当我进行插值时,我开始遇到问题。 The biggest error that shows up is at the end, which is : TypeError: object of type 'float' has no len()
出现的最大错误是在结尾处,即:TypeError:“ float”类型的对象没有len()
I am still learning this language, so please go easy if I have bad comments. 我仍在学习该语言,因此,如果我有不好的评论,请放轻松。
Here is my code: 这是我的代码:
# Imported array of data from a text file.
q1, a1 = loadtxt("values.txt", unpack = True, skiprows = 1)
print q1
print a1
# Creating a while loop for this part of the code.
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
# Interpolation
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()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-de5ecb6dbfd0> in <module>()
1 # Interpolation
2 from scipy.interpolate import interp1d
----> 3 f = interp1d(q,a,'cubic')
4 q1 = linspace(0.5,1.4,25)
5 a1 = f(q1)
C:\Users\krazzy\AppData\Local\Enthought\Canopy32\User\lib\site-packages\scipy\interpolate\interpolate.pyc in __init__(self, x, y, kind, axis, copy, bounds_error, fill_value, assume_sorted)
355 assume_sorted=False):
356 """ Initialize a 1D linear interpolation class."""
--> 357 _Interpolator1D.__init__(self, x, y, axis=axis)
358
359 self.copy = copy
C:\Users\krazzy\AppData\Local\Enthought\Canopy32\User\lib\site-packages\scipy\interpolate\polyint.pyc in __init__(self, xi, yi, axis)
58 self.dtype = None
59 if yi is not None:
---> 60 self._set_yi(yi, xi=xi, axis=axis)
61
62 def __call__(self, x):
C:\Users\krazzy\AppData\Local\Enthought\Canopy32\User\lib\site-packages\scipy\interpolate\polyint.pyc in _set_yi(self, yi, xi, axis)
122 if shape == ():
123 shape = (1,)
--> 124 if xi is not None and shape[axis] != len(xi):
125 raise ValueError("x and y arrays must be equal in length along "
126 "interpolation axis.")
TypeError: object of type 'float' has no len()
Thanks. 谢谢。
The error message is quite explicit: You pass in some object, which is a float
, but is expected to be something having a length. 错误消息非常明确:您传入某个对象,该对象是一个
float
,但是应该是长度一定的对象。 Check the documentation and you'll see, that a
and q
are floats, but interp1d
expects array like objects. 查看文档 ,您会看到
a
和q
都是浮点数,但是interp1d
需要像对象一样的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.