← Back to Python Course | Chapter 13: Data Science & Web | Lesson 2 of 14

Python NumPy Arrays

NumPy arrays numbers की व्यवस्थित grids होती हैं जिन्हें आप slice, reshape और combine कर सकते हैं, जैसे फर्श पर tiles सजाना। आप हर tile पर एक साथ गणित कर सकते हैं।
Syntax
python
import numpy as np

array = np.array([[a, b], [c, d]])
array.shape
array[row, column]

Functions से Arrays बनाना

NumPy Python lists से हाथ से arrays बनाने की बजाय समर्पित constructor functions देती है: np.zeros(shape) और np.ones(shape) एक constant से पहले से भरे arrays बनाते हैं, जबकि np.arange() और np.linspace() समान अंतराल वाली numeric ranges सीधे arrays के रूप में generate करते हैं।

उदाहरण: Creating Arrays with Functions

python
import numpy as np
print(np.zeros(3))  # array pre-filled with zeros
print(np.arange(0, 10, 2))  # evenly spaced range as an array

Slicing और Indexing

NumPy array को index और slice करना Python list जैसे ही array[start:stop:step] syntax को follow करता है, लेकिन यह comma से अलग किए गए indices जैसे array[row, col] के साथ स्वाभाविक रूप से multiple dimensions तक फैल जाता है।

NumPy arrays के slices default रूप से original data पर *views* होते हैं, स्वतंत्र copies नहीं, इसलिए किसी slice को बदलने से source array भी बदल सकता है।

उदाहरण: Slicing and Indexing

python
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr[0, 1])  # row 0, column 1
print(arr[:, 0])  # every row, column 0

Reshaping और Resizing

.reshape() बदलता है कि array के मौजूदा data को dimensions में कैसे व्यवस्थित किया गया है -- जैसे किसी flat 12-element array को 3x4 grid में बदलना -- बिना underlying values को copy या बदले, जब तक नई shape की total element count original से मेल खाती है।

उदाहरण: Reshaping and Resizing

python
import numpy as np
arr = np.arange(12)
print(arr.reshape(3, 4))  # reorganizes the same 12 values into a 3x4 grid

Array Stacking और Splitting

np.concatenate() (या ज़्यादा specific np.vstack()/np.hstack()) कई arrays को किसी चुने axis पर जोड़ता है, जबकि np.split() एक array को कई छोटे arrays में बाँट देता है।

ये lists को जोड़ने या तोड़ने के array-equivalent हैं, बस पूरे numeric blocks पर एक साथ काम करते हैं।

उदाहरण: Array Stacking and Splitting

python
import numpy as np
a = np.array([1, 2])
b = np.array([3, 4])
print(np.concatenate([a, b]))  # joins the arrays into one

Conditional Selection

Boolean indexing आपको सीधे किसी condition का इस्तेमाल करके array elements चुनने देती है, जैसे array[array > 10], जो एक boolean mask बनाता है और सिर्फ उन elements को लौटाता है जहाँ वो mask True हो।

यह manual filtering loops की जगह एक ही expressive line लेती है, जिसे NumPy compiled speed पर execute करती है।

उदाहरण: Conditional Selection

python
import numpy as np
arr = np.array([5, 15, 25])
print(arr[arr > 10])  # boolean mask keeps only elements greater than 10
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. #}

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.