← Back to NumPy Course | Chapter 6: Math Operations | Lesson 2 of 7

Broadcasting के Basics

Broadcasting NumPy को एक छोटा array stretch करने देता है ताकि data copy किए बिना इसे एक बड़े के साथ combine किया जा सके।

In this page:

  1. Broadcasting Basics
Syntax
python
# shapes are compared from the last axis backwards
result = arr_shape_3_by_1 + arr_shape_1_by_4    # -> shape (3, 4)

Broadcasting Basics

जब shapes अलग होते हैं, NumPy इन्हें last axis से backwards compare करता है। Dimensions compatible हैं अगर वे equal हों या इनमें से एक 1 हो, और size-1 axis stretch होता है। यह आपको एक expression में एक matrix की हर row में एक row add करने देता है।

Note: अगर broadcasting fail हो आपको operands could not be broadcast together mention करता एक ValueError दिखेगा।

उदाहरण: Broadcasting basics

python
import numpy as np

m = np.array([[1, 2, 3], [4, 5, 6]])
row = np.array([10, 20, 30])
col = np.array([[100], [200]])
print(m + row)
print(m + col)

# Output:
# [[11 22 33]
#  [14 25 36]]
# [[101 102 103]
#  [204 205 206]]
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि कोई भी दो shapes combine हो सकती हैं
  2. कौन-सी axis stretch हो रही है mix up करना
  3. यह भूल जाना कि एक 1-D array last axis के साथ align होता है
चैप्टर सारांश
  • Shapes right से compare करें
  • Axes equal या 1 होनी चाहिए
  • Size-1 axes stretch होती हैं
  • Explicit loops और copies avoid करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.