NumPy Operations, Reshape और Statistics
Element-wise arithmetic
Same shape वाले दो arrays पर arithmetic operators element-दर-element चलते हैं — loop की ज़रूरत नहीं। एक number के साथ operation हर element पर लगता है (broadcasting)।
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)
import numpy as np print(np.array([1, 2, 3]) + np.array([1, 2]))
Matrix operations और transpose
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
* मिलते elements को गुणा करता है; @ (या np.dot) असली matrix multiplication करता है; .T rows और columns बदल देता है।
Sorting
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)
Arrays जोड़ना
np.concatenate((a, b), axis=…) arrays जोड़ता है। axis=0 rows जोड़ता है (columns की संख्या same हो); axis=1 columns जोड़ता है (rows की संख्या same हो)।
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
Arrays का reshape
reshape(rows, cols) data बदले बिना shape बदलता है। कुल elements same रहने चाहिए (3 × 4 = 12)।
import numpy as np r = np.arange(1, 13) print(r) print(r.reshape(3, 4)) print(r.reshape(2, 6).shape)
| c0 | c1 | c2 | c3 | |
|---|---|---|---|---|
| r0 | 1 | 2 | 3 | 4 |
| r1 | 5 | 6 | 7 | 8 |
| r2 | 9 | 10 | 11 | 12 |
Arrays को split करना
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
Statistical operations और axis
sum, max, min, mean, std, var जैसे functions पूरे array पर, या किसी axis के साथ चलते हैं।
| Mon | Tue | Wed | axis=1 sum | |
|---|---|---|---|---|
| Shop A | 5 | 8 | 6 | 19 |
| Shop B | 9 | 4 | 7 | 20 |
| Total | 14 | 12 | 13 | ← axis=0 sum |
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))Files से arrays load करना
Data अक्सर CSV (comma-separated values) files में रखा जाता है। np.loadtxt() साफ़ numeric file पढ़ता है; np.genfromtxt() missing values भी संभाल सकता है (उन्हें nan से भरता है)। np.savetxt() array को file में लिखता है।
roll,maths,ip 1,78,85 2,66,90 3,92,71
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())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 लिखता है।
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 जगहों में नहीं भर सकते।
