SaitoL - 6 months ago 48

Python Question

`grad = (1/m * (h-y)' * X) + lambda * [0;theta(2:end)]'/m;`

cost = 1/(m) * sum(-y .* log(h) - (1-y) .* log(1-h)) + lambda/m/2*sum(theta(2:end).^2);

How to change this two lines to python? I tried to use the zip to do the same job as '. But it shows the error.

Answer

The " **'** " in Matlab is 'transpose' of a matrix. The `numpy`

package is the fundamental package for scientific computing in python. `numpy.transpose`

could be used to carry the same task out.

```
import numpy as np
matrix = np.arange(6).reshape((2,3))
```

This going to create a matrix with two rows and three columns as follows :

```
>>> array([[0, 1, 2],[3, 4, 5]])
```

Then the transpose is given as:

```
np.transpose (matrix)
>>> array([[0, 3],[1, 4],[2, 5]])
```

I hope it helps