Introduction to NumPy ndarray in Python – पूरी जानकारी हिंदी में
Table of Contents
What is NumPy? – NumPy क्या है
Python में जब भी बात numerical computing या mathematical operations की आती है, तो सबसे पहला नाम आता है NumPy का। NumPy का पूरा नाम है Numerical Python, और यह एक ऐसी library है जो large arrays और matrices के साथ काम करना बहुत आसान बना देती है।
NumPy असल में एक open-source Python library है जिसे खासतौर पर numerical और scientific calculations के लिए बनाया गया है।
यह multi-dimensional arrays को support करती है, यानी हम 1D, 2D या इससे भी ज्यादा dimensions वाले data structures बना सकते हैं।
NumPy के अंदर बहुत सारे mathematical functions पहले से ready मिलते हैं, जैसे addition, multiplication, statistics, linear algebra वगैरह — इन्हें बार-बार खुद से लिखने की जरूरत नहीं पड़ती।
Data Science, Machine Learning और Deep Learning की लगभग हर library — जैसे Pandas, Scikit-learn, TensorFlow — किसी न किसी तरीके से NumPy के ऊपर ही बनी हैं।
Normal Python list की तुलना में NumPy की calculations बहुत तेज होती हैं, क्योंकि इसका core हिस्सा C language में लिखा गया है।
NumPy को इस्तेमाल करने के लिए पहले उसे install करना पड़ता है, और फिर उसे import करके code में use किया जाता है।
import numpy as np
data = np.array([10, 20, 30, 40])
print(data)
print(type(data))
ऊपर के code में np.array() function एक normal Python list को NumPy के special data structure में बदल देता है, जिसे हम ndarray कहते हैं। यही ndarray पूरे NumPy library की सबसे बड़ी और सबसे important चीज है, इसलिए अब इसे detail में समझते हैं।
ndarray Object in Python
ndarray का मतलब है N-dimensional array। यह NumPy का core data structure है, जिस पर पूरी library टिकी हुई है।
ndarray एक ऐसा object है जिसमें same data type के elements एक साथ, contiguous यानी लगातार memory locations में store होते हैं।
Normal Python list में अलग-अलग type के elements रखे जा सकते हैं, लेकिन ndarray में सभी elements का data type एक जैसा (homogeneous) होना जरूरी होता है।
ndarray एक साथ कई dimensions को support करता है — जैसे 1D array (vector), 2D array (matrix) या उससे भी ज्यादा dimensions वाला array।
हर ndarray के कुछ important attributes होते हैं जो उसकी structure बताते हैं —
shape(dimensions की size),ndim(कितने dimensions हैं),dtype(elements का data type) औरsize(total elements की संख्या)।
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.shape) # (2, 3) -> 2 rows, 3 columns
print(matrix.ndim) # 2 -> 2 dimensions
print(matrix.dtype) # int64
print(matrix.size) # 6 -> total elements
यहाँ matrix एक 2-dimensional ndarray है। shape attribute बताता है कि इसमें 2 rows और 3 columns हैं, जबकि ndim बताता है कि यह 2D array है। यही properties ndarray को इतना powerful बनाती हैं क्योंकि इनकी मदद से हम बड़े datasets की structure को तुरंत समझ सकते हैं।
यह diagram एक 2D ndarray की internal structure दिखा रहा है — 2 rows और 3 columns में arrange किए गए same data type के elements, साथ में उसके shape, ndim और size attributes।
Advantages of ndarray
ndarray को इतना popular होने की एक नहीं, बल्कि कई वजहें हैं। नीचे इसके सबसे important advantages दिए गए हैं:
Fast Performance: ndarray की internal implementation C language में होने की वजह से इसकी operations Python की normal loops से कहीं ज्यादा तेज होती हैं।
Less Memory Usage: चूंकि ndarray में सभी elements का data type same होता है, इसलिए memory बहुत ही efficient तरीके से use होती है।
Vectorized Operations: ndarray पर हम बिना loop लिखे सीधे mathematical operations कर सकते हैं, जैसे पूरे array को एक साथ multiply या add करना।
Multi-dimensional Support: ndarray सिर्फ 1D तक सीमित नहीं है, बल्कि 2D, 3D या उससे ज्यादा dimensions वाले data को भी आसानी से handle कर लेता है।
Broadcasting: अलग-अलग shape वाले arrays के बीच भी operations करना possible होता है, जिसे broadcasting कहा जाता है।
Rich Function Library: ndarray के साथ NumPy के built-in functions जैसे
sum(),mean(),reshape()आदि सीधे इस्तेमाल किए जा सकते हैं।
import numpy as np
numbers = np.array([1, 2, 3, 4, 5])
# vectorized operation, koi loop nahi likha
squared = numbers ** 2
print(squared) # [ 1 4 9 16 25]
print(numbers.sum()) # 15
print(numbers.mean()) # 3.0
यहाँ ध्यान देने वाली बात यह है कि numbers ** 2 लिखते ही पूरे array के हर element का square अपने आप calculate हो गया, बिना किसी explicit loop के। यही vectorization ndarray को इतना fast और convenient बनाता है।
Difference Between Python List and NumPy ndarray
अक्सर beginners को यह confusion होती है कि जब Python में पहले से list मौजूद है, तो फिर ndarray की जरूरत ही क्यों पड़ती है। इसका जवाब समझने के लिए दोनों का फर्क clearly देखना जरूरी है।
Data Type: Python list में अलग-अलग data types के elements एक साथ रखे जा सकते हैं, जबकि ndarray में सभी elements का data type same होना चाहिए।
Speed: Numerical calculations में ndarray, list की तुलना में काफी तेज होता है क्योंकि इसका core implementation C में है।
Memory: ndarray list की तुलना में कम memory लेता है क्योंकि elements compact और fixed-type तरीके से store होते हैं।
Operations: List पर mathematical operations करने के लिए loop लिखना पड़ता है, जबकि ndarray पर सीधे operations perform किए जा सकते हैं।
Dimensions: Python list सिर्फ nested lists के जरिए multi-dimensional data को represent कर पाती है, जबकि ndarray के पास built-in multi-dimensional support होता है।
import numpy as np
# Python list ke saath addition
list_a = [1, 2, 3]
list_b = [4, 5, 6]
print(list_a + list_b) # [1, 2, 3, 4, 5, 6] -> concatenation
# NumPy ndarray ke saath addition
array_a = np.array([1, 2, 3])
array_b = np.array([4, 5, 6])
print(array_a + array_b) # [5 7 9] -> element-wise addition
यह example बहुत अच्छे से दिखाता है कि दोनों data structures का behavior कितना अलग है। Python list में + operator दोनों lists को जोड़ (concatenate) देता है, जबकि ndarray में + operator element-wise addition करता है। यही फर्क समझ लेने के बाद ही दोनों का सही जगह पर इस्तेमाल किया जा सकता है।
यह diagram Python List और NumPy ndarray के बीच के मुख्य फर्क को side-by-side दिखा रहा है, खासतौर पर data type, speed और memory usage के मामले में।
Applications of NumPy in Python in Hindi
NumPy का इस्तेमाल सिर्फ academic examples तक सीमित नहीं है, बल्कि real-world में भी इसे बहुत सारी जगहों पर use किया जाता है।
Data Science: Data cleaning, transformation और numerical analysis में NumPy की arrays बहुत बड़े पैमाने पर इस्तेमाल होती हैं।
Machine Learning: Scikit-learn जैसी popular ML libraries अपने अंदर data को represent करने के लिए NumPy ndarray का ही इस्तेमाल करती हैं।
Deep Learning: TensorFlow और PyTorch जैसी libraries के tensors की concept भी काफी हद तक NumPy ndarray से inspired है।
Image Processing: कोई भी image basically pixel values का एक multi-dimensional array ही होती है, इसलिए image processing में NumPy बहुत काम आता है।
Scientific Computing: Physics, statistics और engineering से जुड़ी complex mathematical calculations में NumPy एक standard tool माना जाता है।
Financial Analysis: Stock prices, returns और अलग-अलग financial metrics को calculate करने में भी NumPy की arrays का इस्तेमाल होता है।
import numpy as np
# ek chhota sa example: monthly sales ka average nikalna
monthly_sales = np.array([12000, 15000, 9000, 18000, 20000])
average_sales = monthly_sales.mean()
max_sales = monthly_sales.max()
print("Average Sales:", average_sales)
print("Highest Sales:", max_sales)
ऊपर के example में सिर्फ दो लाइनों में हमने पूरे sales data का average और maximum value निकाल लिया, बिना किसी manual loop के। यही efficiency और simplicity है जिसकी वजह से NumPy आज हर Python developer और data professional के toolbox का जरूरी हिस्सा बन चुका है।
Exam की नजर से देखा जाए, तो यह समझना काफी है कि NumPy का core object ndarray है, जो fast, memory-efficient और multi-dimensional data को handle करने के लिए बना है, और यही properties इसे normal Python list से अलग और ज्यादा powerful बनाती हैं।