参数 = int(theta.ravel().shape[1]) 是什么意思?

博克P

有人可以为我解释该代码吗?

def gradientDescent(X, y, theta, alpha, iters):  
    temp = np.matrix(np.zeros(theta.shape))
    parameters = int(theta.ravel().shape[1])
    cost = np.zeros(iters)
    for i in range(iters):
    error = (X * theta.T) - y

    for j in range(parameters):
        term = np.multiply(error, X[:,j])
        temp[0,j] = theta[0,j] - ((alpha / len(X)) * np.sum(term))

    theta = temp
    cost[i] = computeCost(X, y, theta)

return theta, cost
保利

在样本上逐步评估:

In [13]: np.matrix(np.zeros((3,4)))
Out[13]: 
matrix([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])
In [14]: _.ravel()
Out[14]: matrix([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
In [15]: _.shape
Out[15]: (1, 12)
In [16]: _[1]
Out[16]: 12

Anp.matrix始终是 2d,即使是散乱的。

如果我们使用数组,而不是matrix

In [17]: np.zeros((3,4))
Out[17]: 
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
In [18]: _.ravel()
Out[18]: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In [19]: _.shape
Out[19]: (12,)
In [20]: _[0]
Out[20]: 12

循环需要Xtheta并且temp都具有相同的第二维。我也认为theta必须是一个 (1,n) 矩阵开始。否则这个 ravel 参数会太大。但在那种情况下,首先不需要 ravel。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章