import numpy as np
import pandas as pd
import numpy as np
from matplotlib import pyplot as pt
def computeCost(X,y,theta):
m=len(y)
predictions= X*theta-y
sqrerror=np.power(predictions,2)
return 1/(2*m)*np.sum(sqrerror)
def gradientDescent(X, y, theta, alpha, num_iters):
m = len(y)
jhistory = np.zeros((num_iters,1))
for i in range(num_iters):
h = X * theta
s = h - y
theta = theta - (alpha / m) * (s.T*X).T
jhistory_iter = computeCost(X, y, theta)
return theta,jhistory_iter
data = open(r'C:\Users\Coding\Desktop\machine-learning-ex1\ex1\ex1data1.txt')
data1=np.array(pd.read_csv(r'C:\Users\Coding\Desktop\machine-learning-ex1\ex1\ex1data1.txt',header=None))
y =np.array(data1[:,1])
m=len(y)
y=np.asmatrix(y.reshape(m,1))
X = np.array([data1[:,0]]).reshape(m,1)
X = np.asmatrix(np.insert(X,0,1,axis=1))
theta=np.zeros((2,1))
iterations = 1500
alpha = 0.01;
print('Testing the cost function ...')
J = computeCost(X, y, theta)
print('With theta = [0 , 0]\nCost computed = ', J)
print('Expected cost value (approx) 32.07')
theta=np.asmatrix([[-1,0],[1,2]])
J = computeCost(X, y, theta)
print('With theta = [-1 , 2]\nCost computed =', J)
print('Expected cost value (approx) 54.24')
theta,JJ = gradientDescent(X, y, theta, alpha, iterations)
print('Theta found by gradient descent:')
print(theta)
print('Expected theta values (approx)')
print(' -3.6303\n 1.1664\n')
predict1 = [1, 3.5] *theta
print(predict1*10000)
结果:
测试成本函数...
theta = [0,0]
计算的费用= 32.072733877455676
预期成本值(大约)32.07
theta = [-1,2]
计算的费用= 69.84811062494227
预期成本值(大约)54.24
通过梯度下降发现的θ:
[[-3.70304726 -3.64357517]
[1.17367146 1.16769684]]
预期theta值(大约)
-3.6303
1.1664
[[4048.02858742 4433.63790186]]
有两个问题,第一个计算的成本是正确的,但是第二个是错误的。而且我的梯度下降中有4个元素(假设是2个)
答案 0 :(得分:0)
当您提及“ With theta = [-1,2]”
然后输入
theta=np.asmatrix([[-1,0],[1,2]])
我认为这是不正确的。假设您具有单一功能,并添加了1列,则您尝试进行简单的线性回归
正确的方法应该是
np.array([-1,2])
也在哪里
predictions= X*theta-y
如果您这样做
np.dot(X,theta)-y
乘以时,它不是在做同一件事。