Python NumPy Arrays
In this page:
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
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
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
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
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
import numpy as np
arr = np.array([5, 15, 25])
print(arr[arr > 10]) # boolean mask keeps only elements greater than 10
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB