Numpy Array Broadcasting In Python Explained

Поділитися
Вставка
  • Опубліковано 1 жов 2024

КОМЕНТАРІ • 30

  • @amaarquadri
    @amaarquadri 3 роки тому +12

    Thanks for making it intuitive! Maybe I won't have to mess around in a python console to figure stuff out every time this comes up now!

  • @ZeroSleap
    @ZeroSleap 3 роки тому +6

    Huh i've been coding my own Math library in C# and i've tackled Matrix(array) operations,i defined the regular Matrix addition that requires same Matrix dimensions,but i could i add this too,thanks!

    • @mCoding
      @mCoding  3 роки тому

      Great to hear!

  • @ruroruro
    @ruroruro 3 роки тому +18

    11:36 Disclaimer: Actually, you should avoid using reshape in most cases. Instead, try to always use
    1) indexing with np.newaxis/None to add new "1" dimensions
    2) x.squeeze(axis=N) to remove a "1" dimension in Nth position
    3) x.transpose(...) or x.swapaxes(...) to change the order of axes
    4) x.flat/flatten when you want to "stretch" your N-dim array into a flat 1D vector.
    You should only really use reshape when you really actually want to change the shape, while also rearranging the order of the elements.
    The reason for this recommendation is that it's very easy to make a mistake with reshape and still end up with "valid" code.
    For example, np.ones(2, 3).reshape(3, 2) might look like it transposes the dimensions, but actually it "reinterprets" the shape of the array.
    Additionally, reshape **sometimes** produces views of the original arrays and **sometimes** produces copies.

    • @mCoding
      @mCoding  3 роки тому +5

      I definitely agree that user should be weary of copies and reshaping is not always what you want, users should read the docs! Using the methods you mentioned are often better for expressing intent. If you really do want a reshape but want an error to be raised when data is copied, assigning to shape is what you want instead of reshape. E.g. c = b.view(); c.shape = (2,3) will error because a copy is initiated. Note: flatten will always copy.

  • @rolininthemud
    @rolininthemud 3 роки тому +11

    My answer for the exercise at the end...
    The array [[[[2]], [[3]], [[4]]]] which has shape (1, 3, 1, 1)

    • @I_am_DD
      @I_am_DD 3 роки тому +8

      my anser:
      np.array([2,3,4]).reshape(3,1,1)

    • @winstonsurviving
      @winstonsurviving 7 місяців тому

      import numpy as np
      np.reshape([2,3,4], (3,1,1))

  • @siddhantkandi2412
    @siddhantkandi2412 2 роки тому +2

    Thank you for making it interesting and easy to learn.

  • @dingusagar
    @dingusagar 10 днів тому

    the forloop intuition was good. thanks!

  • @vadivelan4228
    @vadivelan4228 2 роки тому +1

    Thank you, it really helped..

  • @LouisDuran
    @LouisDuran 10 місяців тому

    @mCoding Have you done any work in the area of Machine Learning and Deep Neural Networks?

  • @LouisDuran
    @LouisDuran 10 місяців тому

    Thanks for explanation. I have been learning ML and neural networks on Coursera and they did not explain broadcasting as well as you did.
    The example of iteratively doing the adds on "incompatible" matrices and comparing to the answer given by numpy really helped a lot in my undersanding of broadcasting.

  • @vishalmishra3046
    @vishalmishra3046 3 роки тому +1

    Great video. Thanks.

  • @mariabarbarasalinasluna4808
    @mariabarbarasalinasluna4808 2 роки тому

    Excellent video, you explain it perfectly, thanks a lot ;)

  • @lei07fun
    @lei07fun Рік тому

    A very clear explanation. Nice job

  • @ziweihuang2860
    @ziweihuang2860 10 місяців тому

    For example 3, the shape of y should be (1, 1, 1, 1, 17) rather than (1, 7, 1, 1, 17)

    • @ImbaFerkelchen
      @ImbaFerkelchen 2 місяці тому

      this is not correct, as the 7 cannot get squished into a 1, but the 1 dimension can be extended to 7. Otherwise the Example at the End (1,3) + (3,1) would not work and result in a (1,1) output :D

  • @nafisachowdhury3687
    @nafisachowdhury3687 8 днів тому

    Why does "y" need to be reshaped here 7:08? Before reshaping it's (1, 3) or (3,) which is still compatible with x and results in (3, 3) shape. The result of addition is different but why use the reshaped dimensions and not the original?

    • @mCoding
      @mCoding  7 днів тому

      It didnt need to be reshaped, I just wanted to show an example with that particular shape.

    • @nafisachowdhury3687
      @nafisachowdhury3687 7 днів тому

      @@mCoding gotcha! Thank you so much. the lesson was very thorough

  • @MinhBui-h4x
    @MinhBui-h4x Рік тому

    excellent video. btw, could you tell what the text font being used in this video?

    • @mCoding
      @mCoding  Рік тому

      Thanks! I'm using the RISE presentation plugin for Jupyter notebooks, which in turn uses the reveal.js library with the "simple" theme. You can find the definition of that theme here: github.com/hakimel/reveal.js/blob/master/css/theme/source/simple.scss, from which it seems the font you are looking for is either "Lato" or "News Cycle".

  • @Banefane
    @Banefane Рік тому

    Thank you very much!

  • @AJ-et3vf
    @AJ-et3vf 2 роки тому

    Awesome video! Thank you!

  • @danbrown6698
    @danbrown6698 2 роки тому

    You explained this tech quite clear👍

  • @JoaoVitorBRgomes
    @JoaoVitorBRgomes 3 роки тому

    At 7:19 isn't y shape == (1, 3) ? And not (3, 1) ?

    • @mCoding
      @mCoding  3 роки тому

      The shape of y there is (3, 1) because of the explicit ".reshape(3, 1)". Also, if the reshape call was not there, the shape would have been (3,) instead of (1, 3).