我試圖適應分段線性擬合,如圖1所示爲一個數據組如何在Python中應用分段線性擬合?
由該位線設定獲得這個數字。我試圖使用代碼應用分段線性擬合:
from scipy import optimize
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11, 12, 13, 14, 15])
y = np.array([5, 7, 9, 11, 13, 15, 28.92, 42.81, 56.7, 70.59, 84.47, 98.36, 112.25, 126.14, 140.03])
def linear_fit(x, a, b):
return a * x + b
fit_a, fit_b = optimize.curve_fit(linear_fit, x[0:5], y[0:5])[0]
y_fit = fit_a * x[0:7] + fit_b
fit_a, fit_b = optimize.curve_fit(linear_fit, x[6:14], y[6:14])[0]
y_fit = np.append(y_fit, fit_a * x[6:14] + fit_b)
figure = plt.figure(figsize=(5.15, 5.15))
figure.clf()
plot = plt.subplot(111)
ax1 = plt.gca()
plot.plot(x, y, linestyle = '', linewidth = 0.25, markeredgecolor='none', marker = 'o', label = r'\textit{y_a}')
plot.plot(x, y_fit, linestyle = ':', linewidth = 0.25, markeredgecolor='none', marker = '', label = r'\textit{y_b}')
plot.set_ylabel('Y', labelpad = 6)
plot.set_xlabel('X', labelpad = 6)
figure.savefig('test.pdf', box_inches='tight')
plt.close()
但是,這給了我適合圖。 2,我試圖玩弄價值觀,但沒有改變,我無法得到適合的上限。對我來說最重要的要求是如何讓Python獲得漸變點。本質上,我希望Python能夠識別並在適當的範圍內適合兩種線性擬合。這怎麼可以在Python中完成?
重複http://stackoverflow.com/questions/35579419/fitting-a-curve-with-a-pivot-point-python – 2017-03-27 21:26:11