NumPy Crash Course: Data Science Ki Neev

Lesson 6 of 26

NumPy (Numerical Python) Data Science ki neev hai. Pandas, scikit-learn aur Matplotlib — sab andar se NumPy arrays use karte hain. Is lesson me hum wo sab seekhenge jo aage chahiye.

NumPy ko aur detail me seekhna ho to hamara poora NumPy Tutorial course dekhiye.

List vs NumPy array

List pe math karne ke liye loop chahiye. NumPy array pe operation seedha poore array pe lagta hai — isko vectorization kehte hain, aur ye bahut fast hota hai:

Example

Python
import numpy as np

prices = [100, 200, 300]
print([p * 2 for p in prices])   # list: loop chahiye

arr = np.array(prices)
print(arr * 2)                   # array: seedha
print(arr + 50)

Output

Plain Text
[200, 400, 600]
[200 400 600]
[150 250 350]

Arrays banana

Example

Python
print(np.arange(0, 10, 2))       # 0 se 10 tak, step 2
print(np.linspace(0, 1, 5))      # 0 aur 1 ke beech 5 barabar points
print(np.zeros((2, 3)))          # 2x3 zeros
print(np.ones(3))

Output

Plain Text
[0 2 4 6 8]
[0.   0.25 0.5  0.75 1.  ]
[[0. 0. 0.]
 [0. 0. 0.]]
[1. 1. 1.]

2D array: shape, ndim, dtype

Data Science me data aksar table jaisa hota hai — rows aur columns. NumPy me ye 2D array hai:

Example

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

print("shape:", matrix.shape)   # (rows, columns)
print("ndim:", matrix.ndim)
print("dtype:", matrix.dtype)
print("size:", matrix.size)

Output

Plain Text
shape: (2, 3)
ndim: 2
dtype: int64
size: 6

Indexing aur slicing

Example

Python
print(matrix[0, 1])     # row 0, column 1
print(matrix[:, 1])     # saari rows, column 1
print(matrix[1, :])     # row 1 poori
print(np.arange(12).reshape(3, 4))

Output

Plain Text
2
[2 5]
[4 5 6]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

Boolean filtering — sabse kaam ki cheez

Condition lagao aur sirf wahi values nikaalo jo condition poori karti hain. Pandas me filtering bilkul isi tarah hoti hai:

Example

Python
marks = np.array([45, 78, 92, 33, 67, 88])

print(marks >= 60)
print(marks[marks >= 60])
print(np.where(marks >= 40, "Pass", "Fail"))

Output

Plain Text
[False  True  True False  True  True]
[78 92 67 88]
['Pass' 'Pass' 'Pass' 'Fail' 'Pass' 'Pass']

Aggregation aur axis

axis=0 matlab column-wise (upar se neeche), axis=1 matlab row-wise (baaye se daaye):

Example

Python
# 3 stores, 4 months ki sales
sales = np.array([[10, 12, 9, 14],
                  [20, 18, 22, 25],
                  [ 5,  7,  6,  8]])

print("Total:", sales.sum())
print("Har month ka total:", sales.sum(axis=0))
print("Har store ka total:", sales.sum(axis=1))
print("Average:", sales.mean().round(2))
print("Best store index:", sales.sum(axis=1).argmax())

Output

Plain Text
Total: 156
Har month ka total: [35 37 37 47]
Har store ka total: [45 85 26]
Average: 13.0
Best store index: 1

Broadcasting

Alag shape ke arrays pe bhi operation ho jaata hai — NumPy chhote array ko khud 'phaila' deta hai:

Example

Python
bonus = np.array([1, 2, 3, 4])   # har month ka bonus
print(sales + bonus)             # har row me add ho gaya

Output

Plain Text
[[11 14 12 18]
 [21 20 25 29]
 [ 6  9  9 12]]

Random numbers

Simulations aur sample data ke liye random numbers chahiye. default_rng(42) me 42 ek seed hai — isse har baar same random numbers aate hain, taaki result repeat ho sake:

Example

Python
rng = np.random.default_rng(42)

print(rng.integers(1, 7, size=10))          # 10 baar dice
print(rng.normal(50, 10, size=5).round(2))  # mean 50, std 10

Output

Plain Text
[1 5 4 3 3 6 1 5 2 1]
[36.98 51.28 46.84 49.83 41.47]

Summary

Kaam

Code

Array banana

np.array([...]), np.arange(), np.linspace()

Shape dekhna / badalna

arr.shape, arr.reshape(r, c)

Filter karna

arr[arr > 10]

Condition se value

np.where(cond, a, b)

Aggregation

arr.sum(axis=0), mean(), max(), argmax()

Random

np.random.default_rng(seed)