Scipy의 보간 : y를 생성하는 x를 찾습니다
-
06-07-2019 - |
문제
어느 것을 찾는 더 좋은 방법이 있습니까? 엑스 나에게 줘 와이 나는 Scipy를 찾고 있습니까? 방금 Scipy를 사용하기 시작했고 각 기능에 너무 익숙하지 않습니다.
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
x = [70, 80, 90, 100, 110]
y = [49.7, 80.6, 122.5, 153.8, 163.0]
tck = interpolate.splrep(x,y,s=0)
xnew = np.arange(70,111,1)
ynew = interpolate.splev(xnew,tck,der=0)
plt.plot(x,y,'x',xnew,ynew)
plt.show()
t,c,k=tck
yToFind = 140
print interpolate.sproot((t,c-yToFind,k)) #Lowers the spline at the abscissa
해결책
Scipy의 Univariatespline 클래스는 스플라인을 훨씬 더 피스닉으로 만듭니다.
x = [70, 80, 90, 100, 110]
y = [49.7, 80.6, 122.5, 153.8, 163.0]
f = interpolate.UnivariateSpline(x, y, s=0)
xnew = np.arange(70,111,1)
plt.plot(x,y,'x',xnew,f(xnew))
y에서 x를 찾으려면 다음을 수행하십시오.
yToFind = 140
yreduced = np.array(y) - yToFind
freduced = interpolate.UnivariateSpline(x, yreduced, s=0)
freduced.roots()
Y의 관점에서 X를 보간하는 것이 효과가 있다고 생각했지만 다소 다른 경로가 필요합니다. 더 많은 포인트가 더 가까울 수 있습니다.
다른 팁
필요한 것은 선형 보간이라면 Interp Numpy에서 기능.
당신의 질문을 오해했을 수도 있습니다. 그래서 죄송합니다. 나는 당신이 scipy를 사용할 필요가 없다고 생각합니다. Numpy는 최소 제곱 기능이 있습니다.
#!/usr/bin/env python
from numpy.linalg.linalg import lstsq
def find_coefficients(data, exponents):
X = tuple((tuple((pow(x,p) for p in exponents)) for (x,y) in data))
y = tuple(((y) for (x,y) in data))
x, resids, rank, s = lstsq(X,y)
return x
if __name__ == "__main__":
data = tuple((
(1.47, 52.21),
(1.50, 53.12),
(1.52, 54.48),
(1.55, 55.84),
(1.57, 57.20),
(1.60, 58.57),
(1.63, 59.93),
(1.65, 61.29),
(1.68, 63.11),
(1.70, 64.47),
(1.73, 66.28),
(1.75, 68.10),
(1.78, 69.92),
(1.80, 72.19),
(1.83, 74.46)
))
print find_coefficients(data, range(3))
이것은 [128.81280358 -143.16202286 61.96032544] 반환됩니다.
>>> x=1.47 # the first of the input data
>>> 128.81280358 + -143.16202286*x + 61.96032544*(x**2)
52.254697219095988
0.04 아웃, 나쁘지 않습니다
제휴하지 않습니다 StackOverflow