فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
أمثلة 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
اختبار قصير
اختبر فهمك
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)
from sklearn.datasets import make_moons
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
X, y = make_moons(n_samples=300, noise=0.25, random_state=42)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.3, random_state=0)
for d in (2, 3, 5):
m = SVC(kernel='poly', degree=d, coef0=1).fit(X_tr, y_tr)
print('degree', d, round(m.score(X_te, y_te), 3))
Output
degree 2 0.867 degree 3 0.922 degree 5 0.956
التمرين 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)
from sklearn.datasets import load_diabetes
from sklearn.svm import SVR
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
X, y = load_diabetes(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=0)
for eps in (1, 10, 50):
m = make_pipeline(StandardScaler(), SVR(kernel='rbf', C=100, epsilon=eps)).fit(X_tr, y_tr)
print('epsilon', eps, 'test R2', round(m.score(X_te, y_te), 3))
Output
epsilon 1 test R2 0.187 epsilon 10 test R2 0.196 epsilon 50 test R2 0.213
مشروع صغير في آخر المحاضرة
اقرأ أرقام مكتوبة بإيد: 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
الخطوات
- حمّل digits وقسّمه ٨٠/٢٠ بـ stratify.
- درّب Pipeline: StandardScaler ثم SVC(kernel='rbf') بالقيم الافتراضية.
- اعمل GridSearchCV على C = 1, 10 وgamma = 0.001, 0.01 بـ cv=3.
- اطبع أحسن parameters ودقة الـ test.
- اطبع أول ٥ توقّعات جنب الأرقام الحقيقية.
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 فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.
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.