我需要对以下数据拟合两种不同的回归模型 df
如:属于两个不同家族的曲线)。
因此,想想我所知道的回归类型,有 。
该 df
是超级简单的,只有两列 x
和 y
各有450个条目。
散点图如下。
但是,当我通过traintest绘图过程时,我得到了以下一个..:
现在,很明显,对于这样的traintest分布,一个简单的线性模型是不够的。
但是,当我移动到调查MSE(均方误差),我得到一些有趣的东西。
Train Error: 0.06336815111266955
Test Error: 0.06359148208824823
我确信关于代码(我没有报告). 我检查了它与另一个玩具数据集和工作完美。
有人可以帮助我,好吗?
非常感谢,提前!
EDIT: 在模型拟合过程中,我应用了以下方法 MinMaxScaler()
范围内 [0,1]
功能
不是很清楚是哪里出了问题,可能是火车测试的拆分或者拟合错误的组合,下面我模拟了一些和你一样的数据,你可以看到它的工作原理。
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")
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")