python 分段拟合 判别_numpy-如何在Python中应用分段线性拟合?

numpy-如何在Python中应用分段线性拟合?

我正在尝试为数据集拟合如图1所示的分段线性拟合

该图是通过设置在线获得的。 我试图使用代码来应用分段线性拟合:

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()

但这给了我无花果的形式

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值