我可以在skLearning上进行对数回归吗? [英] Can I make a logarithmic regression on sklearn?

查看:43
本文介绍了我可以在skLearning上进行对数回归吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道"对数回归"是否是正确的术语,我需要在我的数据上拟合一条曲线,就像一条多项式曲线,但末尾是平坦的。

这是一张图片,蓝色的曲线就是我所拥有的(二阶多项式回归),洋红色的曲线就是我需要的。

我搜索了很多,但没有找到,只有线性回归,多项式回归,但没有在skLearning上的对数回归。我需要绘制曲线,然后使用该回归进行预测。

编辑

以下是我发布的绘图图像的数据:

x,y
670,75
707,46
565,47
342,77
433,73
472,46
569,52
611,60
616,63
493,67
572,11
745,12
483,75
637,75
218,251
444,72
305,75
746,64
444,98
342,117
272,85
128,275
500,75
654,65
241,150
217,150
426,131
155,153
841,66
737,70
722,70
754,60
664,60
688,60
796,55
799,62
229,150
232,95
116,480
340,49
501,65

推荐答案

如果我理解正确,您希望用y=a*exp(-b*(x-c))+d这样的函数拟合数据。

我不确定skLearning是否能做到这一点。但是您可以使用scipy.Optimize.curvefit()将数据与您定义的任何函数进行拟合。(scipy):

对于您的情况,我使用您的数据进行了试验,结果如下:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

my_data = np.genfromtxt('yourdata.csv', delimiter=',')
my_data = my_data[my_data[:,0].argsort()]
xdata = my_data[:,0].transpose()
ydata = my_data[:,1].transpose()

# define a function for fitting
def func(x, a, b, c, d):
    return a * np.exp(-b * (x - c)) + d

init_vals = [50, 0, 90, 63]
# fit your data and getting fit parameters
popt, pcov = curve_fit(func, xdata, ydata, p0=init_vals, bounds=([0, 0, 90, 0], [1000, 0.1, 200, 200]))
# predict new data based on your fit
y_pred = func(200, *popt)
print(y_pred)

plt.plot(xdata, ydata, 'bo', label='data')
plt.plot(xdata, func(xdata, *popt), '-', label='fit')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

我发现b的初始值对于拟合至关重要。我估计了一个很小的范围,然后对数据进行了拟合。

如果您对xy之间的关系没有先验知识,您可以使用SkLearning提供的回归方法,如线性回归、核岭回归(KRR)、最近邻回归、高斯过程回归等来拟合非线性数据。Find the documentation here

这篇关于我可以在skLearning上进行对数回归吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆