我试图分别使用最小二乘法和梯度下降法来解决拟合问题。但是两种方法给出的结果是完全不同的。有人帮助弄清楚代码是否错误?以及学习率是否正常?
<gradientDescent>
import pandas as pd
import numpy as np
def costFunc(X,Y,theta):
inner = np.power((X*theta.T)-Y,2)
return np.sum(inner)/(2*len(X))
def gradientDescent(X,Y,theta,alpha,iters):
temp = np.mat(np.zeros(theta.shape))
cost = np.zeros(iters)
thetaNums = int(theta.shape[1])
print(thetaNums)
for i in range(iters):
error = (X*theta.T-Y)
for j in range(thetaNums):
derivativeInner = np.multiply(error,X[:,j])
temp[0,j] = theta[0,j] - (alpha*np.sum(derivativeInner)/len(X))
theta = temp
cost[i] = costFunc(X,Y,theta)
return theta,cost
<gradient descent 1>
import pandas as pd
import numpy as np
from numpy import *
from pandas import *
from numpy import shape
import matplotlib.pyplot as plt
from pandas import DataFrame,Series
import gradientDescent as gd
datafile = u'H:/968.xls'
data = pd.read_excel(datafile)
examDf = DataFrame(data)
X = examDf.ix[:,1:]
Y = examDf.b
X.insert(0,'ones',1)
X = np.mat(X.values)
Y = np.mat(Y.values).T
theta_n = ((X.T*X).I*X.T*Y).T
print(theta_n)
# print(Y)
# print(shape(X),shape(Y),shape(theta_n))
theta = np.mat([0,0,0,0,0])
iters = 1000
alpha = 0.00000001
finalTheta,cost = gd.gradientDescent(X,Y,theta,alpha,iters)
print(finalTheta)
print(cost)