2012-03-26 2 views
0

데이터 세트에 가장 적합한 지점을 찾는 방법으로 scipy.leastsq()를 사용하려고합니다. scipy 라이브러리에 익숙하지 않으면 leastsq 함수가 전체 데이터 집합에 대해 동시에 연산을 수행한다는 인상을받습니다. 동시에 일부 데이터 요소를 스칼라로 간주하기 때문에 문제가 발생합니다.SciPy LeastSq - 스칼라 문제 배열

목표는 결과를 일련의 원과의 최소 거리를 갖는 점 (x, y)의 집합으로 얻는 것입니다.이 값은 또한 x의 형태로 leastsq 함수에 제공됩니다 , y, 반경). leastsq 함수의 전반부에있는 수학은 추측에 가장 가까운 각 원 위에 점을 찾은 다음 추측에서 해당 점까지의 거리를 구합니다.

leastsq 기능 (XI, 이순신, 반경이 이미 배열에 값로드)

#Now that we have the center, we can do least squares 
#generate point guess starting at avg of circles 
ptGuess = np.array([avgX,avgY])   
point, cov,info,mesg, ier = optimize.leastsq(calcResiduals, ptGuess, args = (xi,yi,radii)) 

그리고 calcResiduals()에

전화 :

def calcResiduals(ptGuess, xi, yi, radii): 
#extract x and y from guess point 
xg = ptGuess[0] 
yg = ptGuess[1] 
#slope of the line from (xi,yi) to guess (xg,yg) 
m = (yg - yi)/(xg - xi) 
#Go along the line for the distance of c to get coordinates 
deltax = radii/math.sqrt(1+m**2) 
if (xi > xg): 
    xii = xi + deltax 
else: 
    xii = xi - deltax 
yii = m*(xii-xi) + yi 
#residuals is distance from (xii,yii) to (xg, yg) 
return (xii-xg)**2 + (yii-yg)**2  

내가 오류가 암시하는 것 같다 곱셈을위한 스칼라 값으로 배열을 변환하는 문제로,하지만 왜 그 선이 아직 작동하지 않을지 모르겠다.

오류 : 코드에서 XI와 이순신 포인트의 배열 인 경우

File "listener.py", line 62, in calcAPLocation 
point, cov,info,mesg, ier = optimize.leastsq(calcResiduals, ptGuess, args = (xi,yi,radii)) 
File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 276, in leastsq 
m = _check_func('leastsq', 'func', func, x0, args, n)[0] 
File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 13, in _check_func 
res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) 
File "listener.py", line 76, in calcResiduals 
deltax = radii/math.sqrt(1+m**2) 
TypeError: only length-1 arrays can be converted to Python scalars 

답변

2

하면, 다음 m은 (XI)를 렌하기 동일한 길이로 배열해야한다. 함수 math.sqrt는 길이가 1 인 배열이나 스칼라가 필요합니다.

이전 라인 : 같은 길이의 배열로 분할하고 있기 때문에

m = (yg - yi)/(xg - xi) 

작품.

이 줄은 실패 m 많은 항목이 배열이며, 파이썬 수학 라이브러리가 그것을 처리하는 방법을 알고하지 않습니다

deltax = radii/math.sqrt(1+m**2) 

때문이다. math.sqrt를 numpy.sqrt로 변경하여 m에 각각 항목의 제곱근을 취할 수 있습니다. 나는 이것이 당신이 한 일이라고 생각합니다. 위의 행을

deltaX = radii/np.sqrt(1 + m**2) 
으로 변경하십시오.