Pet*_*ter 0 python matlab numpy
我正在尝试实现一个渐变下降算法,这个算法之前用python编写的matlab中有numpy,但是我得到了一组相似但不同的结果.
这是matlab代码
function [theta] = gradientDescentMulti(X, y, theta, alpha, num_iters)
m = length(y);
num_features = size(X,2);
for iter = 1:num_iters;
temp_theta = theta;
for i = 1:num_features
temp_theta(i) = theta(i)-((alpha/m)*(X * theta - y)'*X(:,i));
end
theta = temp_theta;
end
end
Run Code Online (Sandbox Code Playgroud)
和我的python版本
def gradient_descent(X,y, alpha, trials):
m = X.shape[0]
n = X.shape[1]
theta = np.zeros((n, 1))
for i in range(trials):
temp_theta = theta
for p in range(n):
thetaX = np.dot(X, theta)
tMinY = thetaX-y
temp_theta[p] = temp_theta[p]-(alpha/m)*np.dot(tMinY.T, X[:,p:p+1])
theta = temp_theta
return theta
Run Code Online (Sandbox Code Playgroud)
在matlab中测试用例和结果
X = [1 2 1 3; 1 7 1 9; 1 1 8 1; 1 3 7 4]
y = [2 ; 5 ; 5 ; 6];
[theta] = gradientDescentMulti(X, y, zeros(4,1), 0.01, 1);
theta =
0.0450
0.1550
0.2225
0.2000
Run Code Online (Sandbox Code Playgroud)
测试用例和结果在python中
test_X = np.array([[1,2,1,3],[1,7,1,9],[1,1,8,1],[1,3,7,4]])
test_y = np.array([[2], [5], [5], [6]])
theta, cost = gradient_descent(test_X, test_y, 0.01, 1)
print theta
>>[[ 0.045 ]
[ 0.1535375 ]
[ 0.20600144]
[ 0.14189214]]
Run Code Online (Sandbox Code Playgroud)
Python中的这一行:
temp_theta = theta
Run Code Online (Sandbox Code Playgroud)
不会做你认为它做的事情.它没有复制theta
并"分配"它到"变量" temp_theta
- 它只是说" temp_theta
现在是当前命名的对象的新名称theta
".
所以当你temp_theta
在这里修改时:
temp_theta[p] = temp_theta[p]-(alpha/m)*np.dot(tMinY.T, X[:,p:p+1])
Run Code Online (Sandbox Code Playgroud)
你实际上正在修改theta
- 因为只有一个数组,现在有两个名字.
如果你反而写
temp_theta = theta.copy()
Run Code Online (Sandbox Code Playgroud)
你会得到类似的东西
(3.5) dsm@notebook:~/coding$ python peter.py
[[ 0.045 ]
[ 0.155 ]
[ 0.2225]
[ 0.2 ]]
Run Code Online (Sandbox Code Playgroud)
这与您的Matlab结果相符.