Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📊 Unit 3 – Data Handling और NumPy  ·  Lesson 13

NumPy Operations, Reshape और Statistics

Element-wise arithmetic

Same shape वाले दो arrays पर arithmetic operators element-दर-element चलते हैं — loop की ज़रूरत नहीं। एक number के साथ operation हर element पर लगता है (broadcasting)।

Python
import numpy as np
a = np.array([10, 20, 30])
b = np.array([1, 2, 3])
print(a + b, a - b, a * b)
print(a / b, a ** 2, a % 7)
[11 22 33] [ 9 18 27] [10 40 90] [10. 10. 10.] [100 400 900] [3 6 2]
[10 20 30]a+ [1 2 3]b[11 22 33]a + b
Position 0 से 0, 1 से 1, 2 से 2 जुड़ता है
Python
import numpy as np
print(np.array([1, 2, 3]) + np.array([1, 2]))
ValueError: operands could not be broadcast together with shapes (3,) (2,)

Matrix operations और transpose

Python
import numpy as np
m = np.array([[1, 2], [3, 4]])
n = np.array([[5, 6], [7, 8]])
print(m * n)        # element by element
print(m @ n)        # matrix multiplication
print(m.T)          # transpose
[[ 5 12] [21 32]] [[19 22] [43 50]] [[1 3] [2 4]]

* मिलते elements को गुणा करता है; @ (या np.dot) असली matrix multiplication करता है; .T rows और columns बदल देता है।

Sorting

Python
import numpy as np
s = np.array([42, 7, 19, 3, 25])
print(np.sort(s), s)        # np.sort returns a new sorted array
s.sort()                    # sorts in place
print(s)
[ 3 7 19 25 42] [42 7 19 3 25] [ 3 7 19 25 42]

Arrays जोड़ना

np.concatenate((a, b), axis=…) arrays जोड़ता है। axis=0 rows जोड़ता है (columns की संख्या same हो); axis=1 columns जोड़ता है (rows की संख्या same हो)।

Python
import numpy as np
x = np.array([[1, 2], [3, 4]])
print(np.concatenate((x, np.array([[5, 6]])), axis=0))     # add a row
print(np.concatenate((x, np.array([[9], [9]])), axis=1))   # add a column
[[1 2] [3 4] [5 6]] [[1 2 9] [3 4 9]]

Arrays का reshape

reshape(rows, cols) data बदले बिना shape बदलता है। कुल elements same रहने चाहिए (3 × 4 = 12)।

Python
import numpy as np
r = np.arange(1, 13)
print(r)
print(r.reshape(3, 4))
print(r.reshape(2, 6).shape)
[ 1 2 3 4 5 6 7 8 9 10 11 12] [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] (2, 6)
c0c1c2c3
r01234
r15678
r29101112
एक row के 12 numbers 3 rows × 4 columns बन गए

Arrays को split करना

Python
import numpy as np
print(np.split(np.arange(9), 3))          # 3 equal parts
print(np.split(np.arange(10), [3, 7]))    # split at index 3 and 7
[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])] [array([0, 1, 2]), array([3, 4, 5, 6]), array([7, 8, 9])]

Statistical operations और axis

sum, max, min, mean, std, var जैसे functions पूरे array पर, या किसी axis के साथ चलते हैं।

MonTueWedaxis=1 sum
Shop A58619
Shop B94720
Total141213← axis=0 sum
axis=0 हर column में नीचे; axis=1 हर row में across
Python
import numpy as np
sales = np.array([[5, 8, 6],      # shop A: Mon Tue Wed
                  [9, 4, 7]])     # shop B
print('Total sales        :', sales.sum())
print('Per day (axis=0)   :', sales.sum(axis=0))
print('Per shop (axis=1)  :', sales.sum(axis=1))
print('Best single day    :', sales.max())
print('Average            :', sales.mean())
print('Average per shop   :', sales.mean(axis=1))
print('Std deviation      :', round(sales.std(), 2))
Total sales : 39 Per day (axis=0) : [14 12 13] Per shop (axis=1) : [19 20] Best single day : 9 Average : 6.5 Average per shop : [6.33333333 6.66666667] Std deviation : 1.71
दो shops की daily sales051015MonTueWed586947Shop AShop B
वही 2-D array grouped bar chart के रूप में

Files से arrays load करना

Data अक्सर CSV (comma-separated values) files में रखा जाता है। np.loadtxt() साफ़ numeric file पढ़ता है; np.genfromtxt() missing values भी संभाल सकता है (उन्हें nan से भरता है)। np.savetxt() array को file में लिखता है।

marks.csv
roll,maths,ip
1,78,85
2,66,90
3,92,71
Python
import numpy as np
# create a small CSV file first
data = np.array([[1, 78, 85], [2, 66, 90], [3, 92, 71]])
np.savetxt('marks.csv', data, delimiter=',', fmt='%d', header='roll,maths,ip', comments='')
# now load it back
d = np.loadtxt('marks.csv', delimiter=',', skiprows=1, dtype=int)
print(d)
print('Maths average:', round(d[:, 1].mean(), 2))
print('Highest IP marks:', d[:, 2].max())
[[ 1 78 85] [ 2 66 90] [ 3 92 71]] Maths average: 78.67 Highest IP marks: 90

skiprows=1 header line छोड़ता है; delimiter=',' बताता है कि values commas से अलग हैं।

सारांश और practice

  • Arithmetic element-wise है; shapes match (या broadcast) होने चाहिए।
  • Matrix product के लिए @, transpose के लिए .T, sorting के लिए np.sort।
  • concatenate (axis 0/1), reshape (same कुल size), split।
  • axis=0 (columns) / axis=1 (rows) के साथ sum, mean, max, min, std।
  • loadtxt / genfromtxt CSV files पढ़ते हैं; savetxt लिखता है।
✅ Practice

1) 4 students के 3 subjects के marks का 4 × 3 array बनाकर हर student का total और हर subject का average print करें। 2) np.arange(20) को 4 × 5 में reshape करके आख़िरी row print करें। 3) np.arange(10).reshape(3, 3) से error क्यों आती है?

Answer 3: 10 elements 3 × 3 = 9 जगहों में नहीं भर सकते।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

NumPy में axis=0 का क्या मतलब है?
2-D array में axis=0 rows के नीचे की ओर काम करता है (column-wise result) और axis=1 columns के across (row-wise result)। sales.sum(axis=0) हर column का एक total देता है।
दो arrays कब जोड़े जा सकते हैं?
जब उनके shapes match हों (या broadcasting के लिए compatible हों, जैसे array और एक number)। Shapes (3,) और (2,) जोड़ने पर ValueError आती है।
reshape की शर्त क्या है?
कुल elements की संख्या same रहनी चाहिए। 12 elements को (3, 4), (2, 6), (4, 3) में reshape कर सकते हैं पर (5, 2) में नहीं।
← Class 11 Informatics Practices
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicDatabase Concepts और DBMS आपकी प्रगति: 13 / 20 (65%) · Lesson 14 / 20 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें