【问题标题】:Univariate Linear Regression outputting NaN单变量线性回归输出 NaN
【发布时间】:2017-07-26 21:35:03
【问题描述】:

我目前正在 python 上编写单变量线性回归的实现:

# implementation of univariate linear regression
import numpy as np


def cost_function(hypothesis, y, m):
  return (1 / (2 * m)) * ((hypothesis - y) ** 2).sum()


def hypothesis(X, theta):
  return X.dot(theta)


def gradient_descent(X, y, theta, m, alpha):
  for i in range(1500):
    temp1 = theta[0][0] - alpha * (1 / m) * (hypothesis(X, theta) - y).sum()
    temp2 = theta[1][0] - alpha * (1 / m) * ((hypothesis(X, theta) - y) * X[:, 1]).sum()
    theta[0][0] = temp1
    theta[1][0] = temp2

  return theta

if __name__ == '__main__':
  data = np.loadtxt('data.txt', delimiter=',')

  y = data[:, 1]
  m = y.size
  X = np.ones(shape=(m, 2))
  X[:, 1] = data[:, 0]
  theta = np.zeros(shape=(2, 1))
  alpha = 0.01

  print(gradient_descent(X, y, theta, m, alpha))

这段代码将在无穷大之后输出 theta 的 NaN - 我不知道出了什么问题,但这肯定与我在梯度下降函数中更改 theta 有关。

我使用的数据是我上网的一个简单的线性回归对数据集 - 并且加载正确。

谁能指出我正确的方向?

【问题讨论】:

    标签: python machine-learning linear-regression


    【解决方案1】:

    您看到的问题是,当您执行 X[:,1] 或 data[:,1] 时,您会得到形状为 (m,) 的对象。当您将形状为 (m,) 的对象与形状为 (m,1) 的矩阵相乘时,您将得到一个大小为 (m,m) 的矩阵

    a = np.array([1,2,3])
    b = np.array([[4],[5],[6]])
    (a*b).shape #prints (3,3)
    

    如果你这样做 y=y.reshape((m,1)) 在你的 if __name__ 块和你的 gradient_descent 函数中

    X_1 = X[:,1].reshape((m,1))
    

    应该解决问题。现在发生的事情是当你这样做时

    ((hypothesis(X, theta) - y) * X[:, 1])
    

    你得到一个 100 x 100 的矩阵,这不是你想要的。

    我用于测试的完整代码是:

    # implementation of univariate linear regression
    import numpy as np
    
    
    def cost_function(hypothesis, y, m):
      return (1 / (2 * m)) * ((hypothesis - y) ** 2).sum()
    
    
    def hypothesis(X, theta):
      return X.dot(theta)
    
    
    def gradient_descent(X, y, theta, m, alpha):
      X_1 = X[:,1]
      X_1 = X_1.reshape((m,1))
      for i in range(1500):
        temp1 = theta[0][0] - alpha * (1 / m) * (hypothesis(X, theta) - y).sum()
        temp2 = theta[1][0] - alpha * (1 / m) * ((hypothesis(X, theta) - y) * X_1).sum()
        theta[0][0] = temp1
        theta[1][0] = temp2
    
      return theta
    
    if __name__ == '__main__':
      data= np.random.normal(size=(100,2))
    
      y = 30*data[:,0] + data[:, 1]
      m = y.size
      X = np.ones(shape=(m, 2))
      y = y.reshape((m,1))
      X[:, 1] = data[:, 0]
      theta = np.zeros(shape=(2, 1))
      alpha = 0.01
    
      print(gradient_descent(X, y, theta, m, alpha))
    

    【讨论】:

    • 不幸的是,这似乎无法解决问题 - 输出与以前相同。
    • 还是没有运气!和之前一样的输出。我在 if name 块中的 m 声明之后直接插入了 y = y.reshape((m, 1)) ,并且 x1 = X[:, 1].reshape((m, 1)) 在 gradient_descent 的顶部。
    • 你使用了什么数据?
    • 随机生成我自己的,所以我会知道真正的斜率和截距
    • college.cengage.com/mathematics/brase/understandable_statistics/… 我在这里使用标价与最佳价格数据集无济于事。我得到 RuntimeWarning: invalid value 在我分配给 temp2 的行上的 double_scalars 中遇到,可能是由于 NaN
    猜你喜欢
    • 2019-04-12
    • 1970-01-01
    • 2017-12-16
    • 2020-06-10
    • 2021-02-20
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 2019-12-31
    相关资源
    最近更新 更多