فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
أمثلة Python
أمثلة صغيرة بالـ scikit-learn
الكود والنتيجة معروضين هنا كنص ثابت (النتائج اتجرّبت فعلاً). لتشغيل الكود بنفسك انسخه في Google Colab المجاني. تقدر كمان تفتح الدفتر الرسمي للكتاب (ترخيص Apache-2.0) من الرابط تحت.
المثال ١: فخ الـ accuracy
Sample output (press Run to produce your own): share of 5s: 0.101 always-say-'not 5' accuracy: 0.898 SGD accuracy: 0.989
المثال ٢: confusion matrix و precision و recall
Sample output (press Run to produce your own): [[484 1] [ 5 50]] precision: 0.98 recall: 0.909 F1: 0.943
المثال ٣: نفس الأرقام بإيدك
Sample output (press Run to produce your own): precision: 0.889 recall: 0.8 F1: 0.842
اختبار قصير
اختبر فهمك
1. If 90% of examples are negative, a model that always predicts negative has accuracy about:
2. Precision answers:
3. Recall answers:
4. In a confusion matrix the rows are usually:
5. F1 score is high when:
تمارين برمجة
جرّب بإيدك
التمرين 1
غيّر الهدف لـ «هل الرقم ٣؟» بدل ٥ واطبع precision و recall بـ SGDClassifier.
Change the target to 'is the digit 3?' instead of 5 and print precision and recall with SGDClassifier.
(output appears here)
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import precision_score, recall_score
X, y = load_digits(return_X_y=True)
y3 = (y == 3)
X_tr, X_te, y_tr, y_te = train_test_split(X, y3, test_size=0.3, random_state=42, stratify=y3)
pred = SGDClassifier(random_state=42).fit(X_tr, y_tr).predict(X_te)
print('precision:', round(precision_score(y_te, pred), 3))
print('recall:', round(recall_score(y_te, pred), 3))
Output
precision: 0.785 recall: 0.927
التمرين 2
اعمل DummyClassifier(strategy='most_frequent') لمهمة «هل الرقم ٣؟» واطبع دقته. ليه الرقم عالي رغم إنه مش بيتعلّم حاجة؟
Fit a DummyClassifier(strategy='most_frequent') for the 'is it 3?' task and print its accuracy. Why is it high even though it learns nothing?
(output appears here)
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.dummy import DummyClassifier
X, y = load_digits(return_X_y=True)
y3 = (y == 3)
X_tr, X_te, y_tr, y_te = train_test_split(X, y3, test_size=0.3, random_state=42, stratify=y3)
d = DummyClassifier(strategy='most_frequent').fit(X_tr, y_tr)
print('accuracy:', round(d.score(X_te, y_te), 3))
# High because about 90% of the images are NOT a 3: the classes are skewed.
Output
accuracy: 0.898
مشروع صغير في آخر المحاضرة
كاشف رقم ٥: Binary classifier
الداتا: Optical Recognition of Handwritten Digits (1,797 images, 8x8). Source: UCI.
License / source: Ships with scikit-learn; open data from the UCI repository. Dataset page
الخطوات
- حمّل digits واعمل target جديد: True لو الرقم ٥.
- قسّم الداتا ٨٠/٢٠ بـ stratify.
- درّب LogisticRegression (max_iter=2000).
- اطبع accuracy و confusion matrix و precision و recall.
- اكتب: ليه الـ accuracy لوحدها بتضلّلنا هنا؟ (الفئة ٥ حوالي ١٠٪ بس)
Expected output (press Run to produce your own): accuracy: 0.992 [[322 2] [ 1 35]] precision: 0.946 recall: 0.972
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.