مكدس Python للتعلم الآلي: NumPy وPandas وMatplotlib
“مجموعة أدوات علوم البيانات — NumPy وPandas وMatplotlib وسير عمل Jupyter”
أتقن الأدوات التي يستخدمها كل مهندس تعلم آلي يومياً — عمليات NumPy المتجهة وPandas DataFrames وMatplotlib/Seaborn للتصور البصري. الأساس الذي يبني عليه كل شيء آخر.
المفاهيم المغطاة
∑الصيغ الرئيسية
المتوسط المتجّه
np.mean(X) — تحسب NumPy هذا بلغة C، أسرع بمراتب من حلقة Python
البث
تمدد NumPy المصفوفة الصغيرة على البُعد الناقص — تتجنب الحلقات الصريحة
ارتباط بيرسون
np.corrcoef(X,Y) — يقيس الاعتماد الخطي بين ميزتين
▶محاكاة تفاعلية
لماذا هذه الأدوات قبل كل شيء
كل إطار عمل للتعلم الآلي — scikit-learn وPyTorch وTensorFlow وJAX — يعتمد على مصفوفات NumPy. فهم كيفية عمل المصفوفات في الذاكرة هو الفرق بين كتابة حلقات Python بتعقيد O(n²) وعمليات NumPy المتجهة بتعقيد O(n) التي تعمل بسرعة لغة C. يمنحك Pandas إطارات البيانات للبيانات الفوضوية الحقيقية، وتتيح لك Matplotlib/Seaborn رؤية ما يحدث قبل النمذجة. النظام البيئي بأكمله يتحدث NumPy — إتقانه يعني إتقان اللغة المشتركة.
حلقة Python على 10 ملايين رقم تستغرق ~4 ثوانٍ. np.sum() تستغرق ~8ms — أسرع 500 مرة. هذا يهم عند حساب التدرجات في شبكة عصبية.
أساسيات NumPy — ما تحتاجه فعلاً
إنشاء المصفوفات: np.array(), np.zeros(), np.ones(), np.linspace(), np.arange(), np.random.randn()
معالجة الأشكال: .reshape(), .T (نقل)، np.concatenate()، np.stack()، np.squeeze()
العمليات الرياضية المتجهة: +، -، *، / تعمل عنصراً بعنصر؛ np.dot() / @ لضرب المصفوفات
الفهرسة: arr[2:5]، arr[arr > 0] (قناع منطقي)، arr[:, 0] (شريحة عمود)
التجميعات: .sum()، .mean()، .std()، .max()، .argmax() — تقبل جميعها معامل axis=
قاعدة البث: محاذاة الأشكال من اليمين، يجب تطابق الأبعاد أو أن تكون 1
NumPy, Pandas & Matplotlib — سير العمل الكامل
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # ── NumPy: arrays, broadcasting, vectorized ops ─────────────────────────────── X = np.random.randn(1000, 5) # 1000 samples, 5 features y = 2*X[:,0] - X[:,1] + 0.5*np.random.randn(1000) print(X.shape, X.dtype) # (1000, 5) float64 print(X.mean(axis=0).round(3)) # per-feature means ≈ 0 print(X.std(axis=0).round(3)) # per-feature stds ≈ 1 # Broadcasting: subtract mean and divide by std (manual StandardScaler) X_scaled = (X - X.mean(axis=0)) / X.std(axis=0) # Matrix multiply: X @ W where W is 5×2 W = np.random.randn(5, 2) Z = X_scaled @ W # shape (1000, 2) # Boolean indexing high_income = X[X[:,0] > 1.0] # rows where feature 0 > 1σ print(f"High income rows: {len(high_income)}") # ── Pandas: DataFrames, EDA ─────────────────────────────────────────────────── df = pd.DataFrame(X, columns=[f"feat_{i}" for i in range(5)]) df["target"] = y # Quick EDA print(df.describe().round(2)) # count, mean, std, quartiles print(df.isnull().sum()) # check for missing values print(df.dtypes) # Groupby example df["group"] = np.where(df["feat_0"] > 0, "high", "low") print(df.groupby("group")["target"].agg(["mean","std"]).round(3)) # Correlations corr = df.drop(columns="group").corr() print(corr["target"].sort_values(ascending=False).round(3)) # ── Matplotlib / Seaborn: visualization ────────────────────────────────────── fig, axes = plt.subplots(1, 3, figsize=(15, 4)) # 1. Distribution plot axes[0].hist(df["target"], bins=50, color="#6c63ff", alpha=0.8, edgecolor="white") axes[0].set_title("Target distribution") axes[0].set_xlabel("y") # 2. Scatter + regression line axes[1].scatter(df["feat_0"], df["target"], alpha=0.3, s=10, color="#06b6d4") m, b = np.polyfit(df["feat_0"], df["target"], 1) x_line = np.linspace(-3, 3, 100) axes[1].plot(x_line, m*x_line + b, color="#ff6b6b", lw=2, label=f"slope={m:.2f}") axes[1].set_title("Feature 0 vs Target") axes[1].legend() # 3. Correlation heatmap sns.heatmap(corr, annot=True, fmt=".2f", cmap="coolwarm", center=0, ax=axes[2], cbar=False) axes[2].set_title("Correlation matrix") plt.tight_layout() plt.show() # ── Jupyter tips ────────────────────────────────────────────────────────────── # %timeit np.dot(X, W) # benchmark any cell # %matplotlib inline # show plots in notebook # df.head() # preview first 5 rows # df.info() # dtypes + non-null counts # pd.set_option('display.max_columns', None) # show all columns
أكثر أخطاء NumPy شيوعاً
1) عدم تطابق الأشكال: (100,) ≠ (100,1). تحقق دائماً من .shape قبل عمليات المصفوفة. 2) القسمة الصحيحة: انتبه مع مصفوفات dtype=int. 3) النسخ مقابل العروض: arr[0:5] تُعيد عرضاً — تعديله يُعدّل الأصل. استخدم .copy() للأمان. 4) في المكان مقابل خارجه: X *= 2 تُعدّل X في مكانها؛ Y = X * 2 تُنشئ مصفوفة جديدة. 5) انتشار NaN: np.mean([1,2,np.nan]) = NaN. استخدم np.nanmean() للتجميعات الآمنة.
np.shares_memory(a, b) يخبرك إذا كانت مصفوفتان تشتركان في البيانات الأساسية — معلومة حيوية عند 'نسخ' الشرائح.
?اختبار المعرفة
يتم حفظ التقدم في متصفحك — لا حاجة لحساب.
Need a Data Scientist or AI Engineer?
I build custom ML models, RAG chatbots, data pipelines, and production APIs — from analysis to deployment.