수색…


소개

선 (또는 다른 함수)을 데이터 요소 집합에 적용합니다.

np.polyfit 사용

직선 $ f (x) = mx + c $를 사용하여 데이터 세트를 만듭니다.

npoints = 20
slope = 2
offset = 3
x = np.arange(npoints)
y = slope * x + offset + np.random.normal(size=npoints)
p = np.polyfit(x,y,1)           # Last argument is degree of polynomial

우리가 한 일을보기 위해서 :

import matplotlib.pyplot as plt
f = np.poly1d(p)                # So we can call f(x)
fig = plt.figure()
ax  = fig.add_subplot(111)
plt.plot(x, y, 'bo', label="Data")
plt.plot(x,f(x), 'b-',label="Polyfit")
plt.show()

참고 :이 예는 https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html 의 numpy 문서를 매우 자세하게 설명합니다.

np.linalg.lstsq 사용

polyfit과 같은 데이터 세트를 사용합니다.

npoints = 20
slope = 2
offset = 3
x = np.arange(npoints)
y = slope * x + offset + np.random.normal(size=npoints)

이제 우리는 | cA b | ** 2를 최소화함으로써 선형 방정식의 시스템을 최소화함으로써 해를 구하려고 노력합니다

import matplotlib.pyplot as plt # So we can plot the resulting fit
A = np.vstack([x,np.ones(npoints)]).T
m, c = np.linalg.lstsq(A, y)[0] # Don't care about residuals right now
fig = plt.figure()
ax  = fig.add_subplot(111)
plt.plot(x, y, 'bo', label="Data")
plt.plot(x, m*x+c, 'r--',label="Least Squares")
plt.show()

참고 :이 예는 https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html 의 numpy 문서를 매우 자세하게 설명합니다.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow