-1

Can someone explain that code for me?

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
Neuron
  • 5,141
  • 5
  • 38
  • 59
BorkoP
  • 332
  • 3
  • 14
  • 2
    Actually, I would expect that to raise an exception `tuple index out of range` variety. – Paul Panzer Apr 16 '18 at 00:02
  • TIAS: `np.array([[1,2],[3,4]]).ravel()` gives you `array([1, 2, 3, 4])`, and it's shape is `(4,)`, so the index `1` of the shape tuple is out of range. Probably needs to be `0`. – fferri Apr 16 '18 at 00:08
  • `temp` is `np.matrix`, so the shape `[1]` is ok (though a bit of an oddball usage). – hpaulj Apr 16 '18 at 01:10
  • @hpaulj yuck, `matrix`. So this a convoluted way saying `theta.size`? – Paul Panzer Apr 16 '18 at 01:52

1 Answers1

0

Evaluate it step by step on a sample:

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

A np.matrix is always 2d, even when ravelled.

If we used an array, rather than 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

The loop requires that X, theta and temp all have the same 2nd dimension. I also think theta must be a (1,n) matrix to start with. Otherwise this ravel parameters would be too large. But in that case the ravel isn't needed in the first place.

hpaulj
  • 221,503
  • 14
  • 230
  • 353