NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing Notes in Hindi & English : NumPy के मूल सिद्धांत: ndarrays, ब्रॉडकास्टिंग और वेक्टराइजेशन और 20 महत्वपूर्ण MCQ प्रश्न (India IT Exam)

Post a Comment

UGC-NET, GATE, NIELIT (O/A/B Level), KVS/NVS Computer Teacher, PGDCA, BCA, MCA, B-Tech व All India IT Exam विशेष नोट्स: इस अध्याय में हम NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing (NumPy के मूल सिद्धांत: ndarrays, ब्रॉडकास्टिंग और वेक्टराइजेशन) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और राष्ट्रीय स्तर की परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: NumPy (Numerical Python) is the foundational library for scientific computing in Python, centered around the homogeneous, fixed-type ndarray object. It achieves C-like performance through Vectorization (eliminating Python loops) and Broadcasting (operating on arrays of different shapes).

अध्याय सार: NumPy (न्यूमेरिकल Python) Python में वैज्ञानिक कंप्यूटिंग की आधारभूत लाइब्रेरी है, जो सजातीय ndarray ऑब्जेक्ट पर केंद्रित है। यह वेक्टराइजेशन (Python लूप को समाप्त करना) और ब्रॉडकास्टिंग (विभिन्न आकारों के ऐरे पर संचालन) के माध्यम से C-जैसा प्रदर्शन प्राप्त करता है।

NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing: Detailed Notes & Architecture (NumPy के मूल सिद्धांत: ndarrays, ब्रॉडकास्टिंग और वेक्टराइजेशन)

1. The ndarray Object and Attributes (ndarray ऑब्जेक्ट और एट्रिब्यूट्स)

[EN] Unlike Python lists that store pointers to heterogeneous objects, a NumPy ndarray stores elements of a single data type (dtype) in a contiguous block of memory. Key attributes include .shape (dimensions tuple), .ndim (number of axes), .size (total elements), and .dtype (e.g., int64, float32).

[HI] Python सूचियों के विपरीत, एक NumPy ndarray मेमोरी के एक सन्निहित ब्लॉक में एक ही डेटा प्रकार (dtype) के तत्वों को स्टोर करता है। मुख्य एट्रिब्यूट्स में .shape, .ndim, .size, और .dtype शामिल हैं।

2. Vectorization and Universal Functions (ufuncs) (वेक्टराइजेशन और यूनिवर्सल फ़ंक्शंस (ufuncs))

[EN] Vectorization delegates element-wise arithmetic (like a + b or np.sqrt(a)) to pre-compiled C/Fortran loops, avoiding Python's interpreter overhead. This makes NumPy operations 10x to 100x faster than standard Python for-loops. Functions that operate element-by-element on ndarrays are called Universal Functions (ufuncs).

[HI] वेक्टराइजेशन एलिमेंट-वाइज अंकगणित (जैसे a + b या np.sqrt(a)) को पूर्व-संकलित C/Fortran लूप को सौंपता है, जिससे यह मानक Python लूप की तुलना में 10x से 100x तेज़ हो जाता है।

3. Broadcasting Rules (ब्रॉडकास्टिंग नियम)

[EN] Broadcasting allows arithmetic between arrays of different shapes without copying data. NumPy compares shapes trailing-dimension first (right-to-left): two dimensions are compatible if they are equal, or if one of them is 1. For example, adding a (3, 4) matrix and a (4,) vector broadcasts the vector across all 3 rows.

[HI] ब्रॉडकास्टिंग डेटा कॉपी किए बिना विभिन्न आकारों के ऐरे के बीच अंकगणित की अनुमति देता है। NumPy दाएं-से-बाएं आकारों की तुलना करता है: दो आयाम संगत होते हैं यदि वे बराबर हैं, या उनमें से एक 1 है।

4. Indexing, Slicing (Views vs Copies), and Boolean Masking (इंडेक्सिंग, स्लाइसिंग (व्यू बनाम कॉपी), और बूलियन मास्किंग)

[EN] Basic slicing (e.g., arr[1:4, :2]) returns a View of the original array, meaning modifying the slice mutates the original data. In contrast, Fancy Indexing (arr[[0, 2]]) and Boolean Masking (arr[arr > 5]) return a new Copy. Understanding Views vs Copies is crucial for memory efficiency and avoiding unintended side effects.

[HI] बेसिक स्लाइसिंग (जैसे arr[1:4, :2]) मूल ऐरे का एक व्यू (View) लौटाती है, जिसका अर्थ है कि स्लाइस को संशोधित करने से मूल डेटा बदल जाता है। इसके विपरीत, फैंसी इंडेक्सिंग और बूलियन मास्किंग एक नई कॉपी (Copy) लौटाती है।

Syntax, Formula & Technical Example (NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing)

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])  # shape (2, 3)
b = np.array([10, 20, 30])            # shape (3,)
c = a + b                             # Broadcasting -> [[11, 22, 33], [14, 25, 36]]
mask = c[c > 20]                      # Boolean masking -> array([22, 33, 25, 36])
print(a.shape, a.dtype, mask)

NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing Quick Reference & Comparison Table

NumPy ConceptBehavior / OutputKey Exam Fact
ndarrayContiguous, homogeneous memoryFaster and smaller than Python lists
VectorizationC-level element-wise opsEliminates slow Python for-loops
BroadcastingAligns shapes right-to-leftDimensions match if equal or one is 1
Basic SlicingReturns a ViewModifying slice modifies original array

NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing Top 20 Bilingual MCQs with Answers

Q1. What is the core N-dimensional array object in NumPy called?

(NumPy में मुख्य N-आयामी ऐरे ऑब्जेक्ट को क्या कहा जाता है?)

(A) ndarray

(B) DataFrame

(C) TensorList

(D) PyArray

Correct Answer: (A)

[EN] ndarray stands for N-dimensional array.

[HI] ndarray का अर्थ N-आयामी ऐरे है।

Q2. Which attribute returns a tuple representing the dimensions of a NumPy array?

(कौन सा एट्रिब्यूट NumPy ऐरे के आयामों का प्रतिनिधित्व करने वाला टपल लौटाता है?)

(A) .ndim

(B) .dtype

(C) .size

(D) .shape

Correct Answer: (D)

[EN] For a 2x3 matrix, arr.shape returns (2, 3).

[HI] 2x3 मैट्रिक्स के लिए, arr.shape (2, 3) लौटाता है।

Q3. What does arr.ndim return?

(arr.ndim क्या लौटाता है?)

(A) Memory size in bytes

(B) The number of axes (dimensions) of the array

(C) Total number of elements

(D) Data type of elements

Correct Answer: (B)

[EN] A 2D matrix has ndim == 2.

[HI] एक 2D मैट्रिक्स में ndim == 2 होता है।

Q4. In NumPy, what does basic slicing (e.g., arr[1:3]) return?

(NumPy में, बेसिक स्लाइसिंग (जैसे arr[1:3]) क्या लौटाती है?)

(A) A deep Copy

(B) A Python list

(C) A View of the original array

(D) A tuple

Correct Answer: (C)

[EN] Modifying a View also modifies the original array.

[HI] व्यू को संशोधित करने से मूल ऐरे भी संशोधित हो जाता है।

Q5. Under NumPy Broadcasting rules, which two shapes are compatible?

(NumPy ब्रॉडकास्टिंग नियमों के तहत, कौन से दो आकार संगत हैं?)

(A) (4, 5) and (2, 5)

(B) (3, 4) and (3,)

(C) (3, 4) and (4,)

(D) (2, 3) and (3, 2)

Correct Answer: (C)

[EN] Comparing right-to-left: 4 == 4, and 3 aligns with implicit 1.

[HI] दाएं-से-बाएं तुलना करने पर: 4 == 4, और 3 अंतर्निहित 1 के साथ संरेखित होता है।

Q6. What is Vectorization in NumPy?

(NumPy में वेक्टराइजेशन क्या है?)

(A) Compressing arrays to ZIP

(B) Converting 2D arrays to 1D

(C) Executing element-wise operations in compiled C without Python loops

(D) Plotting vectors on a graph

Correct Answer: (C)

[EN] Vectorization is the key reason NumPy is orders of magnitude faster than Python lists.

[HI] वेक्टराइजेशन मुख्य कारण है कि NumPy Python सूचियों की तुलना में काफी तेज़ है।

Q7. What does Boolean Masking (e.g., arr[arr > 0]) return?

(बूलियन मास्किंग (जैसे arr[arr > 0]) क्या लौटाती है?)

(A) A Copy containing elements where the condition is True

(B) A View of the array

(C) A single boolean

(D) An index error

Correct Answer: (A)

[EN] Boolean and fancy indexing always return a new copy.

[HI] बूलियन और फैंसी इंडेक्सिंग हमेशा एक नई कॉपी लौटाती है।

Q8. Which function creates an array of evenly spaced values within a given interval (like Python's range)?

(कौन सा फ़ंक्शन दिए गए अंतराल के भीतर समान दूरी वाले मानों का ऐरे बनाता है?)

(A) np.zeros()

(B) np.eye()

(C) np.arange()

(D) np.ones()

Correct Answer: (C)

[EN] np.arange(0, 10, 2) produces array([0, 2, 4, 6, 8]).

[HI] np.arange(0, 10, 2) array([0, 2, 4, 6, 8]) उत्पन्न करता है।

Q9. Which function creates an identity matrix in NumPy?

(NumPy में कौन सा फ़ंक्शन आइडेंटिटी मैट्रिक्स बनाता है?)

(A) np.diag_empty()

(B) np.eye()

(C) np.linspace()

(D) np.zeros()

Correct Answer: (B)

[EN] np.eye(3) creates a 3x3 identity matrix with 1s on the main diagonal.

[HI] np.eye(3) मुख्य विकर्ण पर 1 के साथ 3x3 आइडेंटिटी मैट्रिक्स बनाता है।

Q10. What is the difference between a * b and a @ b for two 2D NumPy arrays?

(दो 2D NumPy ऐरे के लिए a * b और a @ b में क्या अंतर है?)

(A) a * b is matrix multiplication; a @ b is dot product

(B) Both are identical

(C) a * b is element-wise multiplication; a @ b is matrix multiplication

(D) a @ b raises a syntax error

Correct Answer: (C)

[EN] The @ operator performs matrix multiplication (equivalent to np.matmul).

[HI] @ ऑपरेटर मैट्रिक्स गुणन (np.matmul के समतुल्य) करता है।

Q11. Which method changes the shape of an array without changing its data?

(कौन सा मेथड डेटा को बदले बिना ऐरे का आकार बदलता है?)

(A) .transpose_all()

(B) .reshape()

(C) .flatten_copy()

(D) .resize()

Correct Answer: (B)

[EN] arr.reshape(2, 3) returns a reshaped view when possible.

[HI] arr.reshape(2, 3) संभव होने पर एक रीशेप्ड व्यू लौटाता है।

Q12. What happens if you mix an integer and a float in np.array([1, 2.5, 3])?

(यदि आप np.array([1, 2.5, 3]) में एक पूर्णांक और एक फ्लोट मिलाते हैं तो क्या होता है?)

(A) It creates a Python list

(B) All elements are upcast to float64

(C) It raises a TypeError

(D) 2.5 is truncated to 2

Correct Answer: (B)

[EN] ndarrays are homogeneous, so NumPy upcasts to the most general type (float64).

[HI] ndarrays सजातीय होते हैं, इसलिए NumPy सबसे सामान्य प्रकार (float64) में अपकास्ट करता है।

Q13. Which function generates N evenly spaced numbers between a start and stop value (inclusive)?

(कौन सा फ़ंक्शन स्टार्ट और स्टॉप मान (समावेशी) के बीच N समान दूरी वाली संख्याएं उत्पन्न करता है?)

(A) np.arange()

(B) np.ones()

(C) np.random.rand()

(D) np.linspace()

Correct Answer: (D)

[EN] np.linspace(0, 1, 5) returns 5 values: [0., 0.25, 0.5, 0.75, 1.].

[HI] np.linspace(0, 1, 5) 5 मान लौटाता है।

Q14. How do you compute the sum of each column in a 2D NumPy array arr?

(आप 2D NumPy ऐरे arr में प्रत्येक कॉलम का योग कैसे निकालते हैं?)

(A) sum(arr)

(B) arr.sum(axis=0)

(C) arr.sum(axis=-1)

(D) arr.sum(axis=1)

Correct Answer: (B)

[EN] axis=0 collapses rows (summing down each column); axis=1 sums across columns.

[HI] axis=0 पंक्तियों को कोलैप्स करता है (प्रत्येक कॉलम का योग करता है)।

Q15. Which method creates an explicit independent copy of a NumPy array?

(कौन सा मेथड NumPy ऐरे की एक स्पष्ट स्वतंत्र कॉपी बनाता है?)

(A) .copy()

(B) .clone()

(C) .view()

(D) .slice()

Correct Answer: (A)

[EN] arr.copy() allocates new memory so changes do not affect the original.

[HI] arr.copy() नई मेमोरी आवंटित करता है ताकि परिवर्तन मूल को प्रभावित न करें।

Q16. What does arr.size return for a 3x4x2 array?

(3x4x2 ऐरे के लिए arr.size क्या लौटाता है?)

(A) 4

(B) 3

(C) 9

(D) 24

Correct Answer: (D)

[EN] size is the total number of elements: 3 * 4 * 2 = 24.

[HI] size तत्वों की कुल संख्या है: 3 * 4 * 2 = 24।

Q17. Within NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing, which numpy concept directly maps to: "Contiguous, homogeneous memory"?

(NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing में कौन सा NumPy Concept सीधे "Contiguous, homogeneous memory" से संबंधित है?)

(A) Basic Slicing

(B) Broadcasting

(C) Vectorization

(D) ndarray

Correct Answer: (D)

[EN] In NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing, 'ndarray' handles 'Contiguous, homogeneous memory' (Faster and smaller than Python lists).

[HI] NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing में 'ndarray' का कार्य/विवरण 'Contiguous, homogeneous memory' (Faster and smaller than Python lists) है।

Q18. Which option accurately describes 'Vectorization' in NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing?

(NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing के संदर्भ में 'Vectorization' की सही विशेषता कौन सी है?)

(A) Contiguous, homogeneous memory

(B) Aligns shapes right-to-left

(C) Returns a View

(D) C-level element-wise ops

Correct Answer: (D)

[EN] 'Vectorization' provides 'C-level element-wise ops' — key point: Eliminates slow Python for-loops.

[HI] 'Vectorization' का मुख्य विवरण 'C-level element-wise ops' है (Eliminates slow Python for-loops)।

Q19. In NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing, identify the correct parameter for the specification: "Aligns shapes right-to-left".

(NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing में "Aligns shapes right-to-left" विवरण किस विकल्प के लिए सही है?)

(A) ndarray

(B) Broadcasting

(C) Vectorization

(D) Basic Slicing

Correct Answer: (B)

[EN] 'Broadcasting' corresponds to 'Aligns shapes right-to-left' (Dimensions match if equal or one is 1).

[HI] 'Broadcasting' का संबंध 'Aligns shapes right-to-left' (Dimensions match if equal or one is 1) से है।

Q20. Considering NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing, what key behavior defines 'Basic Slicing'?

(NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing में 'Basic Slicing' की मुख्य भूमिका या मान क्या है?)

(A) Aligns shapes right-to-left

(B) Returns a View

(C) C-level element-wise ops

(D) Contiguous, homogeneous memory

Correct Answer: (B)

[EN] 'Basic Slicing' represents 'Returns a View' (Modifying slice modifies original array).

[HI] 'Basic Slicing' का अर्थ/मान 'Returns a View' है (Modifying slice modifies original array)।

NumPy Fundamentals: ndarrays, Broadcasting, Vectorization & Indexing — Exam FAQs

Q1: Why is NumPy faster than Python lists?

NumPy, Python सूचियों से तेज़ क्यों है?

[EN] NumPy stores homogeneous data in contiguous memory and runs vectorized operations in compiled C, avoiding Python's per-element type checking.

[HI] NumPy सन्निहित मेमोरी में सजातीय डेटा स्टोर करता है और संकलित C में वेक्टराइज्ड संचालन चलाता है।

Q2: What is the difference between a View and a Copy in NumPy?

NumPy में व्यू और कॉपी में क्या अंतर है?

[EN] A View shares the same underlying memory buffer as the original array, while a Copy has its own independent memory.

[HI] एक व्यू मूल ऐरे के साथ समान मेमोरी बफर साझा करता है, जबकि एक कॉपी की अपनी स्वतंत्र मेमोरी होती है।

Q3: When does Broadcasting fail in NumPy?

NumPy में ब्रॉडकास्टिंग कब विफल होती है?

[EN] Broadcasting raises a ValueError when trailing dimensions are neither equal nor 1 (e.g., shapes (3, 4) and (3, 5)).

[HI] जब ट्रेलिंग आयाम न तो बराबर होते हैं और न ही 1 (जैसे (3, 4) और (3, 5)), तो ब्रॉडकास्टिंग ValueError देती है।

Newest Older

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)