Python NumPy क्या है? Array, Indexing, Slicing & Operations in Hindi | RBSE Class 12

इस लेख में: NumPy क्या है, NumPy library को install और import कैसे करते हैं, NumPy Array, ndarray, 1D और 2D Array, Indexing, Slicing, Shape, Size, Dimension तथा Basic Array Operations को आसान Hindi में examples के साथ समझेंगे।

NumPy क्या है?

NumPy का पूरा नाम Numerical Python है। यह Python की एक popular library है जिसका उपयोग numerical computing, arrays और mathematical operations के लिए किया जाता है।

NumPy का मुख्य data structure ndarray है, जिसे multi-dimensional array के लिए उपयोग किया जाता है।

Important: NumPy Python की standard library का हिस्सा नहीं है। इसे अलग से install किया जा सकता है।

NumPy का उपयोग क्यों किया जाता है?

  • Numerical calculations के लिए।
  • Large arrays के साथ काम करने के लिए।
  • Mathematical operations के लिए।
  • Data science और scientific computing में।
  • Arrays को efficiently process करने के लिए।

NumPy Install कैसे करें?

यदि आपके computer में NumPy installed नहीं है, तो pip का उपयोग करके इसे install किया जा सकता है।

pip install numpy

Installation के बाद Python program में NumPy को import किया जा सकता है।

NumPy Import कैसे करें?

import numpy as np

यहाँ np, NumPy का commonly used alias है।

NumPy Array क्या है?

NumPy में array बनाने के लिए numpy.array() function का उपयोग किया जा सकता है।

Example

import numpy as np

numbers = np.array([10, 20, 30, 40])

print(numbers)

Output:

[10 20 30 40]

Python List और NumPy Array

Python में सामान्य list और NumPy array दोनों में multiple values रखी जा सकती हैं, लेकिन NumPy arrays numerical और scientific operations के लिए विशेष रूप से उपयोगी हैं।

import numpy as np

numbers = np.array([10, 20, 30])

print(numbers)

1D Array

एक-dimensional array में values एक single dimension में होती हैं।

import numpy as np

arr = np.array([10, 20, 30, 40, 50])

print(arr)

2D Array

Two-dimensional array को rows और columns के रूप में समझा जा सकता है।

import numpy as np

arr = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(arr)

Output:

[[1 2 3]
 [4 5 6]]

3D Array

NumPy में तीन या उससे अधिक dimensions वाले arrays भी बनाए जा सकते हैं।

import numpy as np

arr = np.array([
    [
        [1, 2],
        [3, 4]
    ]
])

print(arr)

ndim — Number of Dimensions

ndim attribute array की dimensions की संख्या बताता है।

import numpy as np

arr = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(arr.ndim)

Output:

2

shape Attribute

shape array के प्रत्येक dimension में elements की संख्या बताता है।

import numpy as np

arr = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(arr.shape)

Output:

(2, 3)

इसका अर्थ है कि array में 2 rows और 3 columns हैं।

size Attribute

size array में मौजूद total elements की संख्या बताता है।

import numpy as np

arr = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(arr.size)

Output:

6

dtype Attribute

dtype array में stored elements का data type बताता है।

import numpy as np

arr = np.array([10, 20, 30])

print(arr.dtype)

NumPy Array Indexing

NumPy array में indexing सामान्यतः 0 से शुरू होती है।

import numpy as np

arr = np.array([10, 20, 30, 40])

print(arr[0])
print(arr[2])

Output:

10
30

Negative Indexing

Python की तरह NumPy arrays में भी negative indexing का उपयोग किया जा सकता है।

import numpy as np

arr = np.array([10, 20, 30, 40])

print(arr[-1])
print(arr[-2])

Output:

40
30

2D Array Indexing

2D array में row और column index का उपयोग किया जा सकता है।

import numpy as np

arr = np.array([
    [10, 20, 30],
    [40, 50, 60]
])

print(arr[0, 1])
print(arr[1, 2])

Output:

20
60

NumPy Array Slicing

Array के किसी portion को प्राप्त करने के लिए slicing का उपयोग किया जा सकता है।

import numpy as np

arr = np.array([10, 20, 30, 40, 50])

print(arr[1:4])

Output:

[20 30 40]

Start से Slicing

print(arr[:3])

Output:

[10 20 30]

End तक Slicing

print(arr[2:])

Output:

[30 40 50]

Step के साथ Slicing

print(arr[::2])

Output:

[10 30 50]

NumPy Array Data Types

NumPy arrays में data type specify भी किया जा सकता है।

import numpy as np

arr = np.array([1, 2, 3], dtype=float)

print(arr)
print(arr.dtype)

Output:

[1. 2. 3.]
float64

Zeros Array

np.zeros() का उपयोग zeros से भरा array बनाने के लिए किया जा सकता है।

import numpy as np

arr = np.zeros(5)

print(arr)

Ones Array

import numpy as np

arr = np.ones(5)

print(arr)

arange() Function

np.arange() दिए गए range के अनुसार evenly spaced integer values का array बनाने के लिए उपयोगी है।

import numpy as np

arr = np.arange(1, 10)

print(arr)

Output:

[1 2 3 4 5 6 7 8 9]

Array में Addition

import numpy as np

a = np.array([10, 20, 30])
b = np.array([1, 2, 3])

print(a + b)

Output:

[11 22 33]

Array में Subtraction

print(a - b)

Output:

[ 9 18 27]

Array में Multiplication

print(a * b)

Output:

[10 40 90]

Array में Division

print(a / b)

Output:

[10. 10. 10.]

Array को Scalar से Multiply करना

import numpy as np

arr = np.array([1, 2, 3, 4])

print(arr * 2)

Output:

[2 4 6 8]

sum() Function

import numpy as np

arr = np.array([10, 20, 30])

print(np.sum(arr))

Output:

60

mean() Function

import numpy as np

arr = np.array([10, 20, 30])

print(np.mean(arr))

Output:

20.0

max() और min()

import numpy as np

arr = np.array([10, 50, 20, 40])

print(np.max(arr))
print(np.min(arr))

Output:

50
10

Reshape() Function

reshape() array के elements की संख्या बदले बिना उसके shape को बदलने के लिए उपयोग किया जा सकता है, यदि नया shape compatible हो।

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

new_arr = arr.reshape(2, 3)

print(new_arr)

Output:

[[1 2 3]
 [4 5 6]]

Flattening Array

flatten() का उपयोग multi-dimensional array की copy को one-dimensional array में बदलने के लिए किया जा सकता है।

import numpy as np

arr = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

flat = arr.flatten()

print(flat)

Output:

[1 2 3 4 5 6]

NumPy Array और List में Basic Difference

Python List NumPy Array
General-purpose collection है। Numerical और scientific computing के लिए designed है।
Different data types रख सकती है। Array में सामान्यतः एक compatible dtype होता है।
Mathematical vector operations के लिए direct array semantics नहीं। Element-wise numerical operations आसानी से किए जा सकते हैं।
Python language का built-in feature है। NumPy library से मिलता है।

NumPy के Important Functions

Function / Attribute उपयोग
np.array() Array बनाने के लिए
ndim Dimensions की संख्या
shape Array का shape
size Total elements
dtype Data type
np.zeros() Zeros वाला array
np.ones() Ones वाला array
np.arange() Range-based array
np.sum() Total sum
np.mean() Mean
np.max() Maximum value
np.min() Minimum value
reshape() Shape बदलना
flatten() Array को 1D में बदलना

Practical Example — Student Marks

import numpy as np

marks = np.array([75, 82, 68, 91, 88])

print("Marks:", marks)
print("Total:", np.sum(marks))
print("Average:", np.mean(marks))
print("Highest:", np.max(marks))
print("Lowest:", np.min(marks))

यह example students के marks पर NumPy के basic numerical functions का उपयोग दिखाता है।

RBSE Class 12 Important Questions

  1. NumPy क्या है?
  2. NumPy का पूरा नाम क्या है?
  3. NumPy का उपयोग क्यों किया जाता है?
  4. NumPy को Python program में कैसे import करते हैं?
  5. NumPy Array क्या है?
  6. ndarray क्या है?
  7. 1D और 2D Array में अंतर लिखिए।
  8. ndim attribute क्या बताता है?
  9. shape attribute क्या बताता है?
  10. size attribute क्या बताता है?
  11. dtype क्या है?
  12. NumPy array में indexing कैसे होती है?
  13. NumPy array में slicing क्या है?
  14. np.zeros() का उपयोग क्या है?
  15. np.ones() का उपयोग क्या है?
  16. np.arange() क्या करता है?
  17. reshape() function क्या है?
  18. flatten() function क्या करता है?
  19. NumPy array में arithmetic operations कैसे करते हैं?
  20. Python List और NumPy Array में अंतर लिखिए।

Frequently Asked Questions (FAQs)

NumPy क्या है?

NumPy Python की एक library है जिसका उपयोग numerical computing और multi-dimensional arrays के साथ काम करने के लिए किया जाता है।

NumPy को Python में कैसे import करते हैं?

आमतौर पर import numpy as np का उपयोग किया जाता है।

NumPy Array क्या है?

NumPy Array numerical data को one-dimensional या multi-dimensional structure में store और process करने के लिए उपयोग किया जाता है।

ndim क्या बताता है?

ndim array में dimensions की संख्या बताता है।

shape क्या बताता है?

shape प्रत्येक dimension में elements की संख्या बताता है।

NumPy में indexing किससे शुरू होती है?

NumPy array की indexing सामान्यतः zero यानी 0 से शुरू होती है।

reshape() का उपयोग क्या है?

reshape() array के elements की संख्या को बनाए रखते हुए उसका shape बदलने के लिए उपयोग किया जाता है, जब नया shape compatible हो।

अब अगला Topic पढ़ें

RBSE Python Study Tip: NumPy में सबसे पहले array creation, indexing, slicing, ndim, shape और size को अच्छी तरह समझें। इसके बाद arithmetic operations और reshape() की practice करें। Practical questions में छोटे array programs लिखने का अभ्यास करें।
Official Reference:

NumPy की विस्तृत जानकारी के लिए NumPy की official documentation देखें।

NumPy Documentation