2014-09-20 348 views
0

正如您在下面看到的,我輸入了我的代碼,前兩個模塊正常工作。但是,當我進入插值時,我開始出現問題。顯示出來的最大錯誤是:TypeError:'float'類型的對象沒有len()當我運行插補函數的代碼時,出現錯誤

我還在學習這種語言,所以如果我有不好的評論,請容易。

這裏是我的代碼:

# 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() 

感謝。

+1

請始終發佈完整的回溯。 – wwii 2014-09-20 16:04:51

+0

沒關係。我做的。您現在可以查看它。 – krazzy 2014-09-20 16:30:49

回答

1

錯誤消息是非常明確的:您傳遞一些對象,這是一個float,但預計會有一些長度的東西。檢查documentation,你會看到,aq是浮動的,但interp1d期望數組像對象。

+0

好的。我明白你的意思。現在,我需要生成一個數組a和q。對? – krazzy 2014-09-20 17:13:14

+1

錯誤消息看起來像這樣,但因爲我不知道你想要做什麼,所以我不能用「是」回答。 ;-) – Achim 2014-09-20 19:15:56

相關問題