What is the meaning of single quote(') in matlab, and how to change it to python
<pre><code>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); 
</code></pre>

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

<p><strong>Short answer:</strong></p>

<p>The <code>'</code> operator in MATLAB is the <em>matrix</em> (conjugate) transpose operator.  It flips the matrix around dimensions and takes the complex conjugate of the matrix (the second part being what trips people up)  The short answer is that the equivalent of <code>a'</code> in Python is <code>np.atleast_2d(a).T.conj()</code>.</p>

<p><strong>Slightly longer answer:</strong></p>

<p>Don't use <code>'</code> in MATLAB unless you <em>really</em> know what you are doing.  Use <code>.'</code>, which is the ordinary transpose.  It is the equivalent of <code>np.atleast_2d(a).T</code> in Python (no conjugate).  If you are sure that the <code>a.ndim &gt;= 2</code> in python, then you can just use <code>a.T</code>.  If you are sure that <code>a.ndim == 1</code> in Python, you can use <code>a[None].T</code>.  If you are sure that <code>a.ndim == 0</code> in Python then transposing is pointless so just do whatever you want.</p>

<p><strong>Very Long Answer:</strong></p>

<p>The basic idea about a transpose is that it flips an array or matrix around one dimension  So consider this:</p>

<pre><code>&gt;&gt; a=[1,2,3,4,5,6]

a =

    1     2     3     4     5     6

&gt;&gt; a'

ans =

    1
    2
    3
    4
    5
    6

&gt;&gt; b=[1,2,3;4,5,6]

b =

    1     2     3
    4     5     6

&gt;&gt; b'

ans =

    1     4
    2     5
    3     6
</code></pre>

<p>So it seems pretty clear, <code>'</code> does a transpose.  But that is deceiving:</p>

<pre><code>c=[1j,2j,3j,4j,5j,6j]

c =

Columns 1 through 3

0.000000000000000 + 1.000000000000000i  0.000000000000000 + 2.000000000000000i  0.000000000000000 + 3.000000000000000i

Columns 4 through 6

0.000000000000000 + 4.000000000000000i  0.000000000000000 + 5.000000000000000i  0.000000000000000 + 6.000000000000000i

&gt;&gt; c'

ans =

0.000000000000000 - 1.000000000000000i
0.000000000000000 - 2.000000000000000i
0.000000000000000 - 3.000000000000000i
0.000000000000000 - 4.000000000000000i
0.000000000000000 - 5.000000000000000i
0.000000000000000 - 6.000000000000000i
</code></pre>

<p>Where did all those negatives come from?  They weren't in the original array.  The reason for this is described in the <a href="https://www.mathworks.com/help/matlab/ref/ctranspose.html">documentation</a>.  The <code>'</code> operator in MATLAB isn't a normal transpose operator, the normal transpose operator is <code>.'</code>.  The <code>'</code> operator does a <em>complex conjugate</em> transpose.  It does the transpose of the matrix and does the complex conjugate of that matrix.  </p>

<p>The problem is that this is almost never what you actually want.  It will result in code that seems to work as expected, but silently changes your FFT data, for example.  So unless you are absolutely, positively sure your algorithm requires a complex conjugate transpose, use <code>.'</code>.</p>

<p>As for Python, the Python transpose operator is <code>.T</code>.  So you consider this:</p>

<pre><code>&gt;&gt;&gt; a = np.array([[1, 2, 3, 4, 5, 6]])
&gt;&gt;&gt; print(a)
[[1 2 3 4 5 6]]
&gt;&gt;&gt; print(a.T)
[[1]
 [2]
 [3]
 [4]
 [5]
 [6]]
&gt;&gt;&gt; b = np.array([[1j, 2j, 3j, 4j, 5j, 6j]])
[[ 0.+1.j  0.+2.j  0.+3.j  0.+4.j  0.+5.j  0.+6.j]]
&gt;&gt;&gt; (1j*np.ones((1,10))).T
[[ 0.+1.j]
 [ 0.+2.j]
 [ 0.+3.j]
 [ 0.+4.j]
 [ 0.+5.j]
 [ 0.+6.j]]
</code></pre>

<p>Notice the lack of any negatives for the imaginary part.  If you want to get the complex conjugate transpose, you need to use <code>np.conj(a)</code> or <code>a.conj()</code> to get the complex conjugate (either before or after doing the transpose).  However, numpy has its own transpose pitfall:</p>

<pre><code>&gt;&gt;&gt; c = np.array([1, 2, 3, 4, 5, 6])
&gt;&gt;&gt; print(c)
[1 2 3 4 5 6]
&gt;&gt;&gt; print(c.T)
[1 2 3 4 5 6]
</code></pre>

<p>Huh?  It didn't do anything.  The reason is that <code>np.array([1, 2, 3, 4, 5, 6])</code> creates a 1D array.  A transpose is flipping the array along a particular dimension.  That is meaningless when there is only one dimension, so the transpose doesn't do anything.  </p>

<p>"But," you might object, "didn't a transpose of the 1D MATLAB matrix work?" The reason is more fundamental to how MATLAB and numpy store data.  Consider Python:</p>

<pre><code>&gt;&gt;&gt; np.array([[1, 2, 3], [4, 5, 6]]).ndim
2
&gt;&gt;&gt; np.array([1, 2, 3, 4, 5, 6]).ndim
1
&gt;&gt;&gt; np.array(1).ndim
0
</code></pre>

<p>That seems reasonable.  A 2D array has two dimensions, a 1D array has one dimension, and a scalar has zero dimensions.  But try the same thing in MATLAB:</p>

<pre><code>&gt;&gt; ndims([1,2,3;4,5,6])

ans =

     2

&gt;&gt; ndims([1,2,3,4,5,6])

ans =

     2

&gt;&gt; ndims(1)

ans =

     2
</code></pre>

<p>Everything has 2 dimensions! MATLAB has no 1D or 0D data structures, everything in MATLAB <em>must</em> have at least 2 dimensions (although it may be possible to create your own effectively 1D or 0D class in MATLAB).  So taking the transpose of your "1D" data structure in MATLAB worked becaused it wasn't actually 1D.</p>

<p>Both the conjugate transpose and the 1D transpose issues come down to the basic data type MATLB and numpy use.  MATLAB uses matrices, which inherently are at least 2D.  nump, on the other hand, uses arrays, which can have any number of dimensions.  MATLAB matrices use matrix mathematics as their normal operations (so <code>a * b</code> in MATLAB is a matrix product) while Python arrays use element-by-element mathematics as their normal operators (so <code>a * b</code> is an element-by-element product, equivalent of <code>a .* b</code> in MATLAB).  MATLAB has element-by-element operators, and numpy arrays have matrix operators (although no matrix transpose yet, though adding one is being considered), so this mostly applies to the default operations.</p>

<p>To avoid this issue in Python, there are several ways to get around it.  Indexing with <code>None</code> in Python inserts additional dimensions.  So for a 1D array <code>a</code>, <code>a[None]</code> will be a 2D array where the first dimension has a length of <code>1</code>.  If you don't know ahead of time what the dimensionality of your array is, you can use <code>np.atleast_2d(a)</code>, which will make sure <code>a</code> has at least two dimensions.  So 0D becomes 2D, 1D becomes 2D, 2D stays 2D, 3D stays 3D, 4D stays 4D, etc.</p>

<p>That being said, numpy has a matrix class that works the same as MATLAB's in all these regards (it even has a conjugate transpose operator, <code>.H</code>).  <strong>Don't use it</strong>. The python community has standardized around arrays, since in practice that is almost always what you want.  That means that most Python tools expect arrays, and many will either malfunction if given matrices or will convert them to arrays.  So just use arrays.</p>

python matlab matrix transpose