كل المقررات

الرئيسية / مقرر Machine Learning / الفصل 8 الجزء 1

الفصل 8 (الجزء 1): Dimensionality Reduction

The curse of dimensionality and PCA

الفصل ده عن تقليل عدد الـ features مع الحفاظ على المعلومة. في الجزء ده هنفهم لعنة الأبعاد (curse of dimensionality) والـ PCA.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

أمثلة Python

أمثلة صغيرة بالـ scikit-learn

الكود والنتيجة معروضين هنا كنص ثابت (النتائج اتجرّبت فعلاً). لتشغيل الكود بنفسك انسخه في Google Colab المجاني. تقدر كمان تفتح الدفتر الرسمي للكتاب (ترخيص Apache-2.0) من الرابط تحت.

المثال ١: PCA على iris

Sample output (press Run to produce your own):
variance kept by each component: [0.925 0.053]
total kept with 2 components    : 0.978
first flower in 2D: [[-2.68  0.32]]

المثال ٢: كام بُعد نحتاج؟

Sample output (press Run to produce your own):
original dimensions: 64
components for 95% variance: 29
components for 99% variance: 41

المثال ٣: ضغط وفك الضغط

Sample output (press Run to produce your own):
shape before: (1797, 64) -> after: (1797, 29)
average reconstruction error: 0.849

افتح الدفتر الرسمي في Colab

اختبار قصير

اختبر فهمك

1. The curse of dimensionality means:

2. PCA picks the first direction where the data:

3. PCA(n_components=0.95) keeps:

4. Why scale the data before PCA?

5. inverse_transform in PCA:

تمارين برمجة

جرّب بإيدك

التمرين 1

على داتا wine اعمل StandardScaler ثم PCA بـ 0.90 واطبع عدد الـ components اللي اتختارت.

On the wine data apply StandardScaler then PCA with 0.90 and print how many components were chosen.

(output appears here)

التمرين 2

درّب KNN على digits الأصلية وعلى digits بعد PCA(0.95) وقارن الدقة.

Train KNN on the original digits and on the digits after PCA(0.95) and compare the accuracy.

(output appears here)

مشروع صغير في آخر المحاضرة

اضغط الداتا من ٦٤ بُعد لأقل من النص

الداتا: Optical Recognition of Handwritten Digits: 1,797 images, 64 pixels each. Source: UCI.

License / source: Ships with scikit-learn; open data from the UCI repository. Dataset page

الخطوات

  1. حمّل digits وقسّمه ٨٠/٢٠.
  2. درّب SVC على الداتا الأصلية واطبع الدقة والوقت التقريبي (عدد الـ features).
  3. اعمل PCA(0.95) على الـ training بس.
  4. درّب نفس الـ SVC على الداتا المضغوطة واطبع الدقة وعدد الأبعاد.
  5. اشرح: خسرنا إيه وكسبنا إيه؟
Expected output (press Run to produce your own):
raw: dims 64 accuracy 0.983
PCA: dims 28 accuracy 0.989

Credits: topic order follows the public chapter list of Aurélien Géron's book "Hands-On Machine Learning with Scikit-Learn, Keras and TensorFlow" (3rd ed.). All explanations and code here are original.