فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
أمثلة Python
أمثلة صغيرة بالـ scikit-learn
الكود والنتيجة معروضين هنا كنص ثابت (النتائج اتجرّبت فعلاً). لتشغيل الكود بنفسك انسخه في Google Colab المجاني. تقدر كمان تفتح الدفتر الرسمي للكتاب (ترخيص Apache-2.0) من الرابط تحت.
المثال ١: GMM بتدّي احتمالات
Sample output (press Run to produce your own): weights: [0.33 0.33 0.33] converged: True probabilities of the first point: [[1. 0. 0.]]
المثال ٢: اختيار المكوّنات بالـ BIC
Sample output (press Run to produce your own): components 1 | BIC 5653 components 2 | BIC 5308 components 3 | BIC 4606 components 4 | BIC 4637 components 5 | BIC 4676 components 6 | BIC 4706
المثال ٣: Isolation Forest
Sample output (press Run to produce your own): flagged points: 4 the 3 planted outliers flagged: [-1, -1, -1]
اختبار قصير
اختبر فهمك
1. A GMM gives for each point:
2. For BIC, which model is preferred?
3. An anomaly is:
4. Isolation Forest flags points that are:
5. What does contamination control in IsolationForest?
تمارين برمجة
جرّب بإيدك
التمرين 1
على نفس بيانات المثال ٢ جرّب covariance_type = 'full' و 'diag' و 'spherical' بـ 3 مكوّنات واطبع الـ BIC.
On the data of example 2 try covariance_type = 'full', 'diag' and 'spherical' with 3 components and print the BIC.
(output appears here)
from sklearn.datasets import make_blobs
from sklearn.mixture import GaussianMixture
X, _ = make_blobs(n_samples=600, centers=3, cluster_std=[0.5, 1.0, 1.5], random_state=3)
for cov in ('full', 'diag', 'spherical'):
gm = GaussianMixture(n_components=3, covariance_type=cov, n_init=3, random_state=0).fit(X)
print(cov.ljust(10), 'BIC', round(gm.bic(X)))
Output
full BIC 4606 diag BIC 4588 spherical BIC 4572
التمرين 2
في المثال ٣ غيّر contamination إلى 0.001 و 0.05 واطبع عدد النقط المعلّمة.
In example 3 change contamination to 0.001 and 0.05 and print the number of flagged points.
(output appears here)
import numpy as np
from sklearn.ensemble import IsolationForest
rng = np.random.default_rng(0)
X = np.vstack([rng.normal(0, 1, size=(300, 2)), [[6, 6], [-7, 5], [8, -6]]])
for c in (0.001, 0.05):
flag = IsolationForest(contamination=c, random_state=0).fit(X).predict(X)
print('contamination', c, '| flagged', int((flag == -1).sum()))
Output
contamination 0.001 | flagged 1 contamination 0.05 | flagged 16
مشروع صغير في آخر المحاضرة
دوّر على القراءات الشاذة في داتا السرطان
الداتا: Breast Cancer Wisconsin (Diagnostic): 569 samples, 30 features. Source: UCI.
License / source: Ships with scikit-learn; open data from the UCI repository. Dataset page
الخطوات
- حمّل الداتا واعمل StandardScaler (من غير الـ labels).
- درّب IsolationForest(contamination=0.05, random_state=0).
- اطبع عدد العينات المعلّمة كشاذة.
- احسب نسبة العينات الخبيثة (target==0) بين الشاذة وبين الباقي.
- اشرح: هل الشاذ بالضرورة خبيث؟
Expected output (press Run to produce your own): flagged: 29 of 569 malignant share among flagged: 0.9 malignant share among the rest: 0.34
مشروع نهاية الفصل
ملف المشروع العملي للفصل 9
ملف 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.