对于这个通常的散点图,但不寻常的分布,哪个回归模型应该是最合适的?

问题描述 投票:0回答:1

我需要对以下数据拟合两种不同的回归模型 df 如:属于两个不同家族的曲线)。

因此,想想我所知道的回归类型,有 。

  • 线性回归(下图中绿色的那个,只是为了说明)
  • 多项式回归
  • 脊线回归
  • 拉索回归
  • ElasticNet回归

df 是超级简单的,只有两列 xy 各有450个条目。

散点图如下。

enter image description here

但是,当我通过traintest绘图过程时,我得到了以下一个..:

enter image description here

现在,很明显,对于这样的traintest分布,一个简单的线性模型是不够的。

但是,当我移动到调查MSE(均方误差),我得到一些有趣的东西。

Train Error: 0.06336815111266955
Test Error: 0.06359148208824823

我确信关于代码(我没有报告). 我检查了它与另一个玩具数据集和工作完美。

有人可以帮助我,好吗?

非常感谢,提前!

EDIT: 在模型拟合过程中,我应用了以下方法 MinMaxScaler() 范围内 [0,1] 功能

python python-3.x regression data-science curve
1个回答
0
投票

不是很清楚是哪里出了问题,可能是火车测试的拆分或者拟合错误的组合,下面我模拟了一些和你一样的数据,你可以看到它的工作原理。

import numpy as np
from sklearn import linear_model
import seaborn as sns
from sklearn.preprocessing import PolynomialFeatures

LR = linear_model.LinearRegression()

x = np.linspace(7, 15, num=100)
y = x + 2*x**2 + np.random.normal(0,5,size=100)
poly = PolynomialFeatures(2)

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3)

clf = linear_model.LinearRegression()
clf.fit(poly.fit_transform(X_train.reshape(-1,1)), y_train)
pred_train = clf.predict(poly.fit_transform(X_train.reshape(-1,1)))
pred_test = clf.predict(poly.fit_transform(X_test.reshape(-1,1)))

我们可以绘制火车的结果。

df = pd.DataFrame({'x':X_train,'y':y_train,'pred':pred_train})
sns.scatterplot(x='x',y='y',data=df)
sns.lineplot(x='x',y='pred',data=df,color="orange")

enter image description here

df = pd.DataFrame({'x':X_test,'y':y_test,'pred':pred_test})
sns.scatterplot(x='x',y='y',data=df)
sns.lineplot(x='x',y='pred',data=df,color="orange")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.