梯度下降实现 – 绝对误差问题

我正在尝试用Python实现梯度下降算法。当我绘制成本函数的历史记录时,它似乎在收敛,但我的实现得到的平均绝对误差远比从sklearn的linear_model得到的差。我无法找出我的实现有什么问题。

import matplotlib.pyplot as pltimport numpy as npimport pandas as pdfrom sklearn import linear_modelfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import mean_absolute_errordef gradient_descent(x, y, theta, alpha, num_iters):    m = len(y)    cost_history = np.zeros(num_iters)    for iter in range(num_iters):        h = np.dot(x, theta)        for i in range(len(theta)):            theta[i] = theta[i] - (alpha/m) * np.sum((h - y) * x[:,i])        #保存每次迭代的成本        cost_history[iter] = np.sum(np.square((h - y))) / (2 * m)    return theta, cost_historyattributes = [...]class_field = [...]x_df = pd.read_csv('train.csv', usecols = attributes)y_df = pd.read_csv('train.csv', usecols = class_field)#标准化x_df = (x_df - x_df.mean()) / x_df.std()#梯度下降alpha = 0.01num_iters = 1000err = 0i = 10for i in range(i):    x_train, x_test, y_train, y_test = train_test_split(x_df, y_df, test_size=0.2)    x_train = np.array(x_train)    y_train = np.array(y_train).flatten()    theta = np.random.sample(len(x_df.columns))    theta, cost_history = gradient_descent(x_train, y_train, theta, alpha, num_iters)    err = err + mean_absolute_error(y_test, np.dot(x_test, theta))    print(np.dot(x_test, theta))    #plt.plot(cost_history)    #plt.show()print(err/i)regr = linear_model.LinearRegression()regr.fit(x_train, y_train)y_pred = regr.predict(x_test)print(mean_absolute_error(y_test, y_pred))

回答:

看起来你遗漏了偏置/截距列和系数。

线性函数的假设应该如下所示:

H = theta_0 + theta_1 * x

在你的实现中,它看起来像这样:

H = theta_1 * x

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

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