كل المقررات

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

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

Large margin classification, soft margin and the C parameter

الفصل ده عن الـ SVM: نموذج قوي بيدوّر على أوسع شارع بين الفئات. في الجزء ده هنفهم الـ Margin والـ Soft margin وتأثير الـ C وليه الـ scaling مهم.

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

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

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

أمثلة Python

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

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

المثال ١: SVM خطي على iris

Sample output (press Run to produce your own):
test accuracy: 1.0
prediction for a flower with petals 1.5 x 0.3: 0

المثال ٢: تأثير الـ C

Sample output (press Run to produce your own):
C = 0.001 | support vectors: 195 | test accuracy: 0.93
C = 0.1 | support vectors: 44 | test accuracy: 0.959
C = 10 | support vectors: 25 | test accuracy: 0.953

المثال ٣: ليه الـ scaling مهم

Sample output (press Run to produce your own):
without scaling: 0.906
with scaling   : 0.953

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

اختبار قصير

اختبر فهمك

1. What are support vectors?

2. A smaller C gives:

3. Why scale features before an SVM?

4. Which class is built for fast linear SVMs on large data?

5. Hard margin classification works only when:

تمارين برمجة

جرّب بإيدك

التمرين 1

في المثال ٢ زوّد قيم C لتشمل 100 و 1000 واطبع دقة الـ test. هل زيادة C دايماً بتحسّن النتيجة؟

In example 2 extend C to include 100 and 1000 and print the test accuracy. Does a bigger C always help?

(output appears here)

التمرين 2

درّب LinearSVC على داتا wine (الـ ١٣ feature) مع StandardScaler واطبع دقة الـ test وعدد الـ features.

Train LinearSVC on the wine data (13 features) with StandardScaler and print the test accuracy and the number of features.

(output appears here)

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

SVM على داتا النبيذ: قارن مع baseline

الداتا: Wine recognition dataset: 178 samples, 13 chemical measurements, 3 grape varieties. Source: UCI.

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

الخطوات

  1. حمّل داتا wine وقسّمها ٧٠/٣٠ بـ stratify.
  2. اعمل baseline بـ DummyClassifier(strategy='most_frequent') واطبع دقته.
  3. درّب Pipeline من StandardScaler و SVC(kernel='linear', C=1).
  4. اطبع دقة الـ test وعدد الـ support vectors.
  5. شغّل نفس الـ SVC من غير scaling وقارن.
Expected output (press Run to produce your own):
baseline: 0.389
scaled SVC: 1.0 | support vectors: 19
unscaled SVC: 0.981

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.