简体   繁体   English

在python中实现梯度下降

[英]Implement gradient descent in python

I am trying to implement gradient descent in python.我正在尝试在 python 中实现梯度下降。 Though my code is returning result by I think results I am getting are completely wrong.虽然我的代码正在返回结果,但我认为我得到的结果是完全错误的。

Here is the code I have written:这是我写的代码:

import numpy as np
import pandas

dataset = pandas.read_csv('D:\ML Data\house-prices-advanced-regression-techniques\\train.csv')

X = np.empty((0, 1),int)
Y = np.empty((0, 1), int)

for i in range(dataset.shape[0]):
  X = np.append(X, dataset.at[i, 'LotArea'])
  Y = np.append(Y, dataset.at[i, 'SalePrice'])

X = np.c_[np.ones(len(X)), X]
Y = Y.reshape(len(Y), 1)

def gradient_descent(X, Y, theta, iterations=100, learningRate=0.000001):
  m = len(X)
  for i in range(iterations):
    prediction = np.dot(X, theta)
    theta = theta - (1/m) * learningRate * (X.T.dot(prediction - Y))

  return theta

  theta = np.random.randn(2,1)
  theta = gradient_descent(X, Y, theta)
  print('theta',theta)

The result I get after running this program is:我运行这个程序后得到的结果是:

theta [[-5.23237458e+228] [-1.04560188e+233]] θ [[-5.23237458e+228] [-1.04560188e+233]]

Which are very high values.这是非常高的值。 Can someone point out the mistake I have made in implementation.有人可以指出我在实施中犯的错误吗?

Also, 2nd problem is I have to set value of learning rate very low (in this case i have set to 0.000001) to work other wise program throws an error.另外,第二个问题是我必须将学习率的值设置得非常低(在这种情况下我设置为 0.000001)才能正常工作,否则程序会引发错误。

Please help me in diagnosis the problem.请帮助我诊断问题。

try to reduce the learning rate with iteration otherwise it wont be able to reach the optimal lowest.try this尝试通过迭代降低学习率,否则将无法达到最佳最低值。试试这个

import numpy as np
import pandas

dataset = pandas.read_csv('start.csv')

X = np.empty((0, 1),int)
Y = np.empty((0, 1), int)

for i in range(dataset.shape[0]):
  X = np.append(X, dataset.at[i, 'R&D Spend'])
  Y = np.append(Y, dataset.at[i, 'Profit'])

X = np.c_[np.ones(len(X)), X]
Y = Y.reshape(len(Y), 1)

def gradient_descent(X, Y, theta, iterations=50, learningRate=0.01):
  m = len(X)
  for i in range(iterations):
    prediction = np.dot(X, theta)
    theta = theta - (1/m) * learningRate * (X.T.dot(prediction - Y))
    learningRate/=10;

  return theta

theta = np.random.randn(2,1)
theta = gradient_descent(X, Y, theta)
print('theta',theta)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM