Python 增加线性回归的成本

Python 增加线性回归的成本,python,machine-learning,linear-regression,Python,Machine Learning,Linear Regression,出于培训目的,我用python实现了一个线性回归。问题是成本不是在减少而是在增加。对于数据,我使用翼型自噪声数据集。可以找到数据 我按如下方式导入数据: import pandas as pd def features(): features = pd.read_csv("data/airfoil_self_noise/airfoil_self_noise.dat.txt", sep="\t", header=None) X = features.iloc[:, 0:5]

出于培训目的,我用python实现了一个线性回归。问题是成本不是在减少而是在增加。对于数据,我使用翼型自噪声数据集。可以找到数据

我按如下方式导入数据:

import pandas as pd

def features():

    features = pd.read_csv("data/airfoil_self_noise/airfoil_self_noise.dat.txt", sep="\t", header=None)

    X = features.iloc[:, 0:5]
    Y = features.iloc[:, 5]

    return X.values, Y.values.reshape(Y.shape[0], 1)
X, Y = features()
model = linearRegression()
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)
model.fit(X_train, y_train)
我的线性回归代码如下所示:

import numpy as np
import random

class linearRegression():

    def __init__(self, learning_rate=0.01, max_iter=20):
        """
        Initialize the hyperparameters of the linear regression.

        :param learning_rate: the learning rate
        :param max_iter: the max numer of iteration to perform
        """

        self.lr = learning_rate
        self.max_iter = max_iter
        self.m = None
        self.weights = None
        self.bias = None

    def fit(self, X, Y):
        """
        Run gradient descent algorithm

        :param X: the inputs
        :param Y: the outputs
        :return:
        """

        self.m = X.shape[0]
        self.weights = np.random.normal(0, 0.1, (X.shape[1], 1))
        self.bias = random.normalvariate(0, 0.1)

        for iter in range(0, self.max_iter):

            A = self.__forward(X)
            dw, db = self.__backward(A, X, Y)

            J = (1/(2 * self.m)) * np.sum(np.power((A - Y), 2))

            print("at iteration %s cost is %s" % (iter, J))

            self.weights = self.weights - self.lr * dw
            self.bias = self.bias - self.lr * db

    def predict(self, X):
        """
        Make prediction on the inputs

        :param X: the inputs
        :return:
        """

        Y_pred = self.__forward(X)

        return Y_pred

    def __forward(self, X):
        """
        Compute the linear function on the inputs

        :param X: the inputs
        :return:
            A: the activation
        """

        A = np.dot(X, self.weights) + self.bias

        return A

    def __backward(self, A, X, Y):
        """

        :param A: the activation
        :param X: the inputs
        :param Y: the outputs
        :return:
            dw: the gradient for the weights
            db: the gradient for the bias
        """

        dw = (1 / self.m) * np.dot(X.T, (A - Y))
        db = (1 / self.m) * np.sum(A - Y)

        return dw, db
然后我实例化linearRegression类,如下所示:

import pandas as pd

def features():

    features = pd.read_csv("data/airfoil_self_noise/airfoil_self_noise.dat.txt", sep="\t", header=None)

    X = features.iloc[:, 0:5]
    Y = features.iloc[:, 5]

    return X.values, Y.values.reshape(Y.shape[0], 1)
X, Y = features()
model = linearRegression()
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)
model.fit(X_train, y_train)

我试图找出成本增加的原因,但到目前为止我还没有找到原因。如果有人能给我指出正确的方向,我将不胜感激。

通常,如果你选择了较高的学习率,你可能会遇到类似的问题。我已尝试检查您的代码,我的观察结果如下:

  • 你的成本函数J看起来不错
  • 但在你的反向函数中,你似乎从你的猜测中减去了你的实际结果。通过这样做,你可能会得到负权重,因为你是从权重和梯度中减去你的学习率和学习率的乘积,你最终会得到增加的成本函数结果

    • 你的学习率太高了。当我运行你的代码时,除了学习率为1e-7而不是0.01之外,没有修改,我可以可靠地降低成本。

      通常,成本增加时,学习率太高。

      在创建类实例和调用函数的地方发布完整代码。允许他人重现错误/问题我编辑了我的帖子当你在数据上使用其他软件包时,你会得到什么样的结果?手工进行几次迭代会得到什么?可能是您的数据。当我将
      features()
      中的行更改为
      X=features.iloc[:,1:2]
      (而不是使用前四列)时,您的成本开始下降。即使我使用sklearn,用原始数据也无法得到比.6更好的分数。试着构建一个人工数据集,你知道它将与线性回归很好地结合在一起——看看你会得到什么样的结果