فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
أمثلة 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
اختبار قصير
اختبر فهمك
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)
from sklearn.datasets import load_breast_cancer
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
X, y = load_breast_cancer(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
for C in (0.1, 10, 100, 1000):
m = make_pipeline(StandardScaler(), SVC(kernel='linear', C=C)).fit(X_tr, y_tr)
print(C, round(m.score(X_te, y_te), 3))
Output
0.1 0.959 10 0.953 100 0.93 1000 0.93
التمرين 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)
from sklearn.datasets import load_wine
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
from sklearn.model_selection import train_test_split
X, y = load_wine(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
m = make_pipeline(StandardScaler(), LinearSVC(max_iter=5000, random_state=0)).fit(X_tr, y_tr)
print('accuracy:', round(m.score(X_te, y_te), 3))
print('features:', X.shape[1])
Output
accuracy: 1.0 features: 13
مشروع صغير في آخر المحاضرة
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
الخطوات
- حمّل داتا wine وقسّمها ٧٠/٣٠ بـ stratify.
- اعمل baseline بـ DummyClassifier(strategy='most_frequent') واطبع دقته.
- درّب Pipeline من StandardScaler و SVC(kernel='linear', C=1).
- اطبع دقة الـ test وعدد الـ support vectors.
- شغّل نفس الـ 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.