كل المقررات

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

الفصل 5 (الجزء 2): Support Vector Machines

Nonlinear SVMs, the kernel trick, SVM regression and tuning

في الجزء ده هنتعامل مع الداتا اللي مش بتتفصل بخط مستقيم: الـ Polynomial features والـ Kernel trick والـ RBF، وبعدين SVR وضبط الـ hyperparameters.

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

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

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

أمثلة Python

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

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

المثال ١: داتا moons

Sample output (press Run to produce your own):
linear                   0.878
poly features + linear   0.922
RBF kernel               0.911

المثال ٢: تأثير gamma

Sample output (press Run to produce your own):
gamma 0.1 | train 0.82 | test 0.87
gamma 1 | train 0.93 | test 0.91
gamma 10 | train 0.94 | test 0.89
gamma 100 | train 0.98 | test 0.82

المثال ٣: SVR و GridSearchCV

Sample output (press Run to produce your own):
best params: {'svr__C': 100, 'svr__gamma': 0.01}
test R2: 0.345

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

اختبار قصير

اختبر فهمك

1. The kernel trick lets you:

2. A very large gamma in an RBF SVM tends to cause:

3. What does epsilon control in SVR?

4. How do we pick C and gamma?

5. Which kernel is a strong general-purpose default?

تمارين برمجة

جرّب بإيدك

التمرين 1

على داتا moons جرّب SVC بـ kernel='poly' ودرجات 2 و 3 و 5 واطبع دقة الـ test.

On the moons data try SVC with kernel='poly' and degrees 2, 3 and 5 and print the test accuracy.

(output appears here)

التمرين 2

جرّب SVR بـ epsilon = 1 و 10 و 50 على داتا diabetes (بعد scaling) واطبع R2 على الـ test.

Try SVR with epsilon = 1, 10 and 50 on the diabetes data (after scaling) and print the test R2.

(output appears here)

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

اقرأ أرقام مكتوبة بإيد: SVM على digits

الداتا: Optical Recognition of Handwritten Digits: 1,797 images of 8x8 pixels, 10 classes. Source: UCI.

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

الخطوات

  1. حمّل digits وقسّمه ٨٠/٢٠ بـ stratify.
  2. درّب Pipeline: StandardScaler ثم SVC(kernel='rbf') بالقيم الافتراضية.
  3. اعمل GridSearchCV على C = 1, 10 وgamma = 0.001, 0.01 بـ cv=3.
  4. اطبع أحسن parameters ودقة الـ test.
  5. اطبع أول ٥ توقّعات جنب الأرقام الحقيقية.
Expected output (press Run to produce your own):
default test accuracy: 0.986
best params: {'svc__C': 10, 'svc__gamma': 0.001}
tuned test accuracy: 0.981
predicted: [7, 6, 3, 7, 7] true: [7, 6, 3, 7, 7]

مشروع نهاية الفصل

ملف المشروع العملي للفصل 5

ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (PDF)

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.