2014-09-20 2 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

+0

괜찮습니다. 나는 그것을했다. 지금 볼 수 있습니다. – krazzy

답변

1

오류 메시지는 매우 명백합니다. float 인 개체를 전달했지만 길이가있는 것으로 예상됩니다. documentation을 확인하면 aq은 부동 소수점 수를 나타내지 만 interp1d은 배열과 같은 객체를 예상합니다.

+0

좋습니다. 너의 의도를 알 겠어. 이제, a와 q의 값을 가진 배열을 생성해야합니다. 권리? – krazzy

+1

오류 메시지는 다음과 같습니다. 그러나 무엇을하고 싶은지 모르겠으므로 "예"라고 대답 할 수 없습니다. ;-) – Achim

관련 문제