NUMPY
NumPy — 0-dan Praktik Bələdçi
Addım → Çıxış formatında, sıfırdan başlamaq üçün hazırlanıb. Eyni CSV üzərində müxtəlif baxışlar, izahlar və Interview Q&A ayrıca bölmədə.
ndarray · shape · dtype
indexing & masks
reshape / concatenate
stats & linalg
CSV → müxtəlif çıxışlar
Qeyd: Hər kod blokunun altında Output ayrıca göstərilir və yanında hansı əməliyyatın nəticəsi olduğu yazılıb.
Addım-addım
Terminal — Setup
# 1) Virtual env (opsional, tövsiyə olunur)
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS/Linux
source .venv/bin/activate
# 2) Paketlər
pip install --upgrade pip
pip install numpy pandas
# 3) Test versiyalar
python -c "import numpy as np, pandas as pd; print(np.__version__, pd.__version__)"
Output
2.1.3 2.2.3 # np, pandas versiyaları (səndə fərqli ola bilər)
Problem? ModuleNotFoundError görürsənsə, virtual env aktiv deyil və ya paket quraşdırılmayıb.
Python — Arrays
import numpy as np
import pandas as pd
# 1D massiv
a1 = np.array([1, 2, 3])
print(a1.shape) # → Output A1
# 2D massiv
a2 = np.array([[1, 2.0, 3.3],
[4, 5, 6.5]])
print(a2) # → Output A2 (bütöv)
print(a2[1]) # → Output A3 (2-ci sətir)
# 3D massiv
a3 = np.array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]])
print(a3.ndim) # → Output A4 (ölçü)
print(a3.dtype) # → Output A5 (tip)
print(a3.size) # → Output A6 (say)
print(type(a3)) # → Output A7 (obyekt tipi)
Output
(3,) # A1: print(a1.shape)
[[1. 2. 3.3]
[4. 5. 6.5]] # A2: print(a2)
[4. 5. 6.5] # A3: print(a2[1])
3 # A4: print(a3.ndim)
int64 # A5: print(a3.dtype) (səndə fərqli ola bilər)
18 # A6: print(a3.size)
<class 'numpy.ndarray'> # A7: print(type(a3))
Python — Creation
np.random.seed(1)
simple_array = np.array([1, 2, 3])
print(simple_array) # → C1
zeros = np.zeros((2, 3))
print(zeros) # → C2
ones = np.ones((1, 3))
print(ones) # → C3
random_arange = np.arange(2, 10, 1)
print(random_arange) # → C4
random_array = np.random.randint(6, 20, size=(2, 5))
print(random_array, random_array.shape, random_array.size) # → C5
new_random = np.random.random((5, 3))
print(new_random) # → C6
random_data = np.random.randint(10, size=(5, 3))
print(random_data) # → C7
unique_array = np.unique(random_data)
print(unique_array) # → C8
a4 = np.random.randint(10, size=(2, 3, 4, 5))
print(a4.shape) # → C9
Output
[1 2 3] # C1
[[0. 0. 0.]
[0. 0. 0.]] # C2
[[1. 1. 1.]] # C3
[2 3 4 5 6 7 8 9] # C4
[[ 7 9 15 12 11]
[ 6 19 6 6 16]] (2, 5) 10 # C5
[[0.417022 0.72032449 0.00011437]
[0.30233257 0.14675589 0.09233859]
[0.18626021 0.34556073 0.39676747]
[0.53881673 0.41919451 0.6852195 ]
[0.20445225 0.87811744 0.02738759]] # C6
[[2 9 0]
[4 9 5]
[9 3 5]
[1 2 4]
[1 7 7]] # C7
[0 1 2 3 4 5 7 9] # C8
(2, 3, 4, 5) # C9
Python — Indexing
A = np.arange(1, 13).reshape(3, 4)
print(A) # → I1
print(A[0, 0]) # → I2
print(A[1]) # → I3
print(A[:, 2]) # → I4
print(A[:2, :2]) # → I5
mask = A % 2 == 0
print(mask) # → I6
print(A[mask]) # → I7
B = A.copy()
B[B < 5] = 0
print(B) # → I8
Output
[[ 1 2 3 4] # I1
[ 5 6 7 8]
[ 9 10 11 12]]
1 # I2
[5 6 7 8] # I3
[ 3 7 11] # I4
[[1 2] # I5
[5 6]]
[[False True False True]
[False True False True]
[False True False True]] # I6
[ 2 4 6 8 10 12] # I7
[[0 0 0 0] # I8
[0 6 7 8]
[9 10 11 12]]
Python — Reshape/Join
B = np.arange(12)
print(B.reshape(3, 4)) # → R1
print(B.reshape(-1, 6)) # → R2
print(B.reshape(3, 4).T) # → R3
X = np.arange(1, 7).reshape(2, 3)
Y = np.arange(7, 13).reshape(2, 3)
print(np.concatenate([X, Y], axis=0)) # → R4
print(np.concatenate([X, Y], axis=1)) # → R5
print(np.stack([X, Y], axis=0).shape) # → R6
Output
[[ 0 1 2 3] # R1
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3 4 5] # R2
[ 6 7 8 9 10 11]]
[[ 0 4 8] # R3
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
[[ 1 2 3] # R4
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[ 1 2 3 7 8 9] # R5
[ 4 5 6 10 11 12]]
(2, 2, 3) # R6
Python — Stats & Broadcasting
M = np.array([[1, 2, 3],
[4, 5, 6]])
print(M.sum()) # → S1
print(M.sum(axis=0)) # → S2
print(M.sum(axis=1)) # → S3
print(M.mean(), M.std(), M.min(), M.max()) # → S4
print(np.percentile(M, [25, 50, 75])) # → S5
v = np.array([1, 0, -1])
print(M + v) # → S6 (broadcasting)
Output
21 # S1
[5 7 9] # S2
[ 6 15] # S3
3.5 1.707825127659933 1 6 # S4
[1.5 3.5 5.5] # S5
[[2 2 2] # S6
[5 5 5]]
Python — Linear Algebra
A = np.array([[3, 1],
[0, 2]])
b = np.array([9, 8])
print(A @ A) # → L1
print(np.linalg.det(A)) # → L2
print(np.linalg.inv(A)) # → L3
vals, vecs = np.linalg.eig(A)
print(vals) # → L4
print(np.linalg.solve(A, b)) # → L5
Output
[[9 5]
[0 4]] # L1
6.0 # L2
[[ 0.33333333 -0.16666667]
[ 0. 0.5 ]] # L3
[3. 2.] # L4
[2.66666667 4.] # L5
sales.csv (mini)
File — sales.csv
order_id,city,product,qty,price
1,Warsaw,Keyboard,2,120
2,Warsaw,Mouse,1,50
3,Gdansk,Keyboard,1,125
4,Warsaw,Monitor,1,900
5,Gdansk,Mouse,3,45
Python — CSV Views
import pandas as pd
import numpy as np
df = pd.read_csv('sales.csv')
arr = df.values
print(df.head()) # → C1
print(arr.shape, arr.dtype) # → C2
qty = df['qty'].to_numpy()
price = df['price'].to_numpy()
revenue = qty * price
print(qty) # → C3
print(price) # → C4
print(revenue, revenue.sum()) # → C5
print(df[df['city']=='Warsaw'].to_numpy()) # → C6
print(np.unique(df['product'])) # → C7
pivot = df.pivot_table(index='city', columns='product', values='qty', aggfunc='sum', fill_value=0)
print(pivot) # → C8
print(pivot.to_numpy()) # → C9
Output
order_id city product qty price # C1
0 1 Warsaw Keyboard 2 120
1 2 Warsaw Mouse 1 50
2 3 Gdansk Keyboard 1 125
3 4 Warsaw Monitor 1 900
4 5 Gdansk Mouse 3 45
(5, 5) object # C2
[2 1 1 1 3] # C3
[120 50 125 900 45] # C4
[240 50 125 900 135] 1450 # C5
[[1 'Warsaw' 'Keyboard' 2 120]
[2 'Warsaw' 'Mouse' 1 50]
[4 'Warsaw' 'Monitor' 1 900]] # C6
['Keyboard' 'Monitor' 'Mouse'] # C7
product Keyboard Monitor Mouse # C8
city
Gdansk 1 0 3
Warsaw 2 1 1
[[1 0 3]
[2 1 1]] # C9
- T1:
np.arange(1, 37)→reshape(3,3,4)— gözlənilən:(3, 3, 4). - T2: Pivotda ən böyük
qtyhüceyrəsinin (city, product) etiketini tap. - T3:
revenueüzrə top‑2 sifariş indeksləri (azalan). - T4: 10,000 təsadüfi
float32vəfloat64üçünmean/stdfərqlərini müqayisə et.
- list vs ndarray? —
ndarraysabit tipli, vektorlaşdırılmış və sürətlidir;listheterogendir, döngü tələb edir. - Broadcasting nədir? — Formanın avtomatik genişlənməsi ilə element‑wise əməliyyat.
- View vs Copy? — Dilim çox vaxt view; müstəqil işləmək üçün
copy(). - reshape vs resize? —
reshapeforma;resizeölçünü dəyişib doldura bilər. - np.vectorize sürət verir? — Yox, əsasən rahatlıqdır; sürət vektorlaşdırmada.
- np.dot vs @? — 2D-də eyni; ND hallarda
@daha aydındır. - np.nan müqayisəsi? —
np.isnan;nan==nanFalse. - dtype seçimi? — Yaddaş+sürət+dəqiqlik balansı (ML:
float32, elm:float64). - np.unique nə qaytarır? — Unikal dəyərlər; istəyə görə saylar (
return_counts=True). - Maskalı indeksləmə? —
x[x>0]kimi boolean filtr. - np.argsort nə edir? — Sıralama indeksləri.
- C vs F‑order? — Sətir‑əsaslı vs sütun‑əsaslı yaddaş düzümü.
- Böyük fayllar? —
np.memmap, parçalı oxu,dtypeoptimizasiyası. - einsum? — Çoxölçülü hasil/toplamaları qısa və tez.
- Performans? — Vektorlaşdırma, lazımsız kopyadan qaç, doğru
dtype, lazım olsa Numba/Cython.