# Vectorization in Numpy

**URL:** <https://community.deeplearning.ai/t/vectorization-in-numpy/520062>\
**Category:** Neural Networks and Deep Learning\
**Tags:** coursera-platform\
**Created:** [December 16, 2023, 1:41pm UTC](https://community.deeplearning.ai/t/vectorization-in-numpy/520062 "2023-12-16T13:41:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![khushal\_vanani](https://avatars.discourse-cdn.com/v4/letter/k/e0b2c6/32.png) [@khushal\_vanani](https://community.deeplearning.ai/u/khushal_vanani)\
**Post date:** [December 16, 2023, 1:41pm UTC](https://community.deeplearning.ai/t/vectorization-in-numpy/520062/1 "2023-12-16T13:41:11Z")

</div>

Dr. Ng said that we can calculate wTx by np.dot(wT,x).  
Dot product for two vectors is this right - x.y = xTy  
Shouldn’t this be np.dot(w,x) - this would be the correct notation I guess. Can someone clarify on this?

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [December 16, 2023, 1:53pm UTC](https://community.deeplearning.ai/t/vectorization-in-numpy/520062/2 "2023-12-16T13:53:59Z")

</div>

Please see [this](https://numpy.org/doc/stable/reference/generated/numpy.dot.html#numpy.dot) keeping array shapes in mind.

---

<div class="post-metadata">

**Author:** ![khushal\_vanani](https://avatars.discourse-cdn.com/v4/letter/k/e0b2c6/32.png) [@khushal\_vanani](https://community.deeplearning.ai/u/khushal_vanani)\
**Post date:** [December 16, 2023, 2:08pm UTC](https://community.deeplearning.ai/t/vectorization-in-numpy/520062/3 "2023-12-16T14:08:48Z")

</div>

I looked at it, I do know programming and MATLAB as well, but my question is a different thing and this website about dot function is different as well. I did not get what were you trying to me look at. can you please elaborate on that or answer my question straightaway.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 16, 2023, 4:00pm UTC](https://community.deeplearning.ai/t/vectorization-in-numpy/520062/4 "2023-12-16T16:00:06Z")

</div>

Python is not as nice as MATLAB at handling vector operations. In MATLAB, all that is native to the language and the way that the polymorphism works is really beautiful. But in python, they had to build a separate library numpy to provide vector functions. In numpy you have two functions you can use for real dot product style matrix multiply: numpy `dot` or `matmul`. With 2D objects, the two are equivalent. Numpy also provides “elementwise” matrix multiply as numpy `multiply`. You also have “over-ride” operators that you can use: \* is equivalent to `np.multiply` and @ is equivalent to `np.matmul`.

In the particular case of logistic regression, we have w as a column vector of dimension n\_x x 1 and X (the input “samples” matrix) has dimension n\_x x m, where n\_x is the number of features and m is the number of samples. So if we need the dot product of w and X, it will require that we transpose w first, right? That’s exactly the same in MATLAB. It’s just the underlying math. The math expression is w^T \cdot X and you have several way to express that in python, all of which are equivalent:

```auto
np.dot(w.T, X)
np.matmul(w.T, X)
w.T @ X
np.transpose(w) @ X
np.dot(np.transpose(w), X)

```

Of those choices, Prof Ng will usually choose the first one. If you are just getting started here, it is also helpful to be cognizant of the notation conventions Prof Ng uses, which are discussed on [this thread](https://community.deeplearning.ai/t/week-2-how-to-decide-to-use-or-np-dot-when-calculating-formula/17467).
