Sklearn线性回归损失函数与手动代码不匹配

我试图使用手动代码复制Sklearn线性回归库中的成本结果。两者之间存在巨大差异,我无法找出原因。这是Sklearn的代码:

SkLearn实现:

X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size=0.30)classifier = sklearn.linear_model.LinearRegression()classifier.fit(X_train,Y_train)cost = np.sqrt(np.sum((np.dot(X_train,classifier.coef_.reshape(9,1)) + classifier.intercept_ - Y_train.reshape(478,1))**2))print(cost)cost = 4.236441942240197

我尝试复制的代码:

a = X_train_rev.shapeassert(X_train_rev.shape == (478,10)) # 断言X_train_rev的形状Y_train = Y_train.reshape(478,1)alpha = 0.0005 # 学习率coefficient = np.random.randn(1,10) # 初始化系数,包括截距# 循环迭代for i in range(100000):    cost = np.sqrt(np.sum((np.dot(X_train_rev,coefficient.T) - Y_train)**2)) # 成本结果    if i % 10000 == 0: print(cost)    grad = np.dot((np.dot(X_train_rev,coefficient.T) - Y_train).T, X_train_rev) # 计算梯度    coefficient = coefficient - (alpha * grad) # 调整系数,包括截距迭代后的成本:45.2304286497357910.42840191696328510.42840191696328510.42840191696328510.42840191696328510.42840191696328510.42840191696328510.42840191696328510.42840191696328510.428401916963285

根据我的手动代码,成本未能进一步降低,并且与Sklearn的成本相差甚远。我尝试调整alpha变量,但任何增加alpha都会导致成本趋向于正无穷大。

请注意,我的手动代码中使用的X_train_rev数据有10列/特征,而不是Sklearn训练集中9个特征,因为我在训练集中添加了一列’ones’来表示截距。同样,系数向量也包括截距。


回答:

我已经尝试复制您的问题

from sklearn.datasets import make_regressionfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LinearRegressionfrom sklearn.metrics import mean_squared_errorX, Y = make_regression(n_samples=500, n_features=9, bias=0, random_state=1)X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.30, random_state=1)classifier = LinearRegression(fit_intercept=False)classifier.fit(X_train,Y_train)cost = np.sqrt(np.sum((np.dot(X_train,classifier.coef_.reshape(9,1)) + classifier.intercept_ - Y_train.reshape(-1,1))**2))print(cost)print('Manual regression')Y_train = Y_train.reshape(-1,1)alpha = 0.0005 # 学习率coefficient = np.random.randn(1,9) # 初始化系数,包括截距# 循环迭代for i in range(100000):    cost = np.sqrt(np.sum((np.dot(X_train,coefficient.T) - Y_train)**2)) # 成本结果    if i % 10000 == 0: print(cost)    grad = np.dot((np.dot(X_train,coefficient.T) - Y_train).T, X_train) # 计算梯度    coefficient = coefficient - (alpha * grad) # 调整系数,包括截距

做了一些小的调整以使代码完全可重现。我没有遇到任何问题。MSE之间存在微小差异,但两者的得分都小于1e-11,所以这是一个数值问题。

enter image description here

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注