فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
أمثلة Python
أمثلة صغيرة بالـ scikit-learn
الكود والنتيجة معروضين هنا كنص ثابت (النتائج اتجرّبت فعلاً). لتشغيل الكود بنفسك انسخه في Google Colab المجاني. تقدر كمان تفتح الدفتر الرسمي للكتاب (ترخيص Apache-2.0) من الرابط تحت.
المثال ١: تجهيز الداتا بـ ColumnTransformer
Sample output (press Run to produce your own): [[-1.41 -0.98 0. 1. ] [ 0. -0.05 1. 0. ] [ 0. -0.61 0. 1. ] [ 1.41 1.63 1. 0. ]] columns: ['num__rooms', 'num__area', 'cat__ocean_far', 'cat__ocean_near']
المثال ٢: خطأ التدريب مقابل cross-validation
Sample output (press Run to produce your own): linear | train RMSE: 0.55 | CV RMSE: 0.97 tree | train RMSE: 0.0 | CV RMSE: 0.84
المثال ٣: Grid search
Sample output (press Run to produce your own):
max_depth 3 -> CV RMSE 0.717
max_depth 6 -> CV RMSE 0.685
max_depth 12 -> CV RMSE 0.734
best: {'max_depth': 6}
اختبار قصير
اختبر فهمك
1. Which is the correct order for preparing data safely?
2. One-hot encoding turns a text category into:
3. A decision tree shows near-zero training error. What should you check?
4. What does a pipeline give you?
5. When should the test set be used for the final check?
تمارين برمجة
جرّب بإيدك
التمرين 1
ابنِ pipeline فيه SimpleImputer(strategy='mean') ثم StandardScaler على مصفوفة فيها قيمة ناقصة، واطبع الناتج.
Build a pipeline of SimpleImputer(strategy='mean') then StandardScaler on an array with one missing value, and print the result.
(output appears here)
import numpy as np from sklearn.pipeline import make_pipeline from sklearn.impute import SimpleImputer from sklearn.preprocessing import StandardScaler X = np.array([[1.0], [2.0], [np.nan], [5.0]]) pipe = make_pipeline(SimpleImputer(strategy='mean'), StandardScaler()) print(pipe.fit_transform(X).round(2))
Output
[[-1.13] [-0.45] [ 0. ] [ 1.59]]
التمرين 2
جرّب Ridge بـ alpha = 0.1 و 1 و 10 على أول 4000 صف من الداتا واطبع CV RMSE لكل واحد (cv=5).
Try Ridge with alpha = 0.1, 1 and 10 on the first 4000 housing rows and print the CV RMSE for each (cv=5).
(output appears here)
from sklearn.datasets import fetch_california_housing
from sklearn.linear_model import Ridge
from sklearn.model_selection import cross_val_score
X, y = fetch_california_housing(return_X_y=True)
X, y = X[:4000], y[:4000]
for a in (0.1, 1, 10):
s = -cross_val_score(Ridge(alpha=a), X, y, cv=5, scoring='neg_root_mean_squared_error').mean()
print('alpha', a, '-> CV RMSE', round(s, 3))
Output
alpha 0.1 -> CV RMSE 0.965 alpha 1 -> CV RMSE 0.965 alpha 10 -> CV RMSE 0.962
مشروع صغير في آخر المحاضرة
Pipeline كامل: جهّز، درّب، وقارن
الداتا: Diabetes dataset (442 patients, 10 features). Source: Efron et al., 2004.
License / source: Ships with scikit-learn. Dataset page
الخطوات
- حمّل الداتا وقسّمها ٨٠/٢٠.
- اعمل Pipeline: StandardScaler ثم Ridge.
- اعمل Pipeline تاني: StandardScaler ثم RandomForestRegressor(n_estimators=100, random_state=0).
- احسب متوسط RMSE بـ 5-fold cross-validation للاتنين على الـ training.
- اختار الأحسن واقيسه مرة واحدة على الـ test.
Expected output (press Run to produce your own): ridge cv RMSE: 55.9 forest cv RMSE: 59.0 chosen: ridge test RMSE: 53.8
مشروع نهاية الفصل
ملف المشروع العملي للفصل 2
ملف 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.