فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
أمثلة Python
أمثلة صغيرة بالـ scikit-learn
الكود والنتيجة معروضين هنا كنص ثابت (النتائج اتجرّبت فعلاً). لتشغيل الكود بنفسك انسخه في Google Colab المجاني. تقدر كمان تفتح الدفتر الرسمي للكتاب (ترخيص Apache-2.0) من الرابط تحت.
المثال ١: RMSE و MAE
Sample output (press Run to produce your own): RMSE by hand: 134.7 | MAE by hand: 70.0 RMSE sklearn: 134.7 | MAE sklearn: 70.0
المثال ٢: نظرة سريعة على الداتا
Sample output (press Run to produce your own): (20640, 9) ['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude', 'MedHouseVal'] count 20640.00 mean 3.87 std 1.90 min 0.50 25% 2.56 50% 3.53 75% 4.74 max 15.00 Name: MedInc, dtype: float64
المثال ٣: random مقابل stratified
Sample output (press Run to produce your own):
overall random stratified
income_cat
1 0.040 0.040 0.040
2 0.319 0.324 0.319
3 0.351 0.359 0.351
4 0.176 0.167 0.176
5 0.114 0.109 0.114
اختبار قصير
اختبر فهمك
1. Predicting a house price (a number) from labelled examples is:
2. Which measure punishes large errors more strongly?
3. Why set the test set aside early?
4. What does a stratified split keep?
5. What do head(), info() and describe() help with?
تمارين برمجة
جرّب بإيدك
التمرين 1
احسب RMSE و MAE بنفسك بـ numpy للقيم الفعلية [3, 5, 2, 7] والمتوقعة [2, 5, 4, 8].
Compute RMSE and MAE with numpy for actual [3, 5, 2, 7] and predicted [2, 5, 4, 8].
(output appears here)
import numpy as np actual = np.array([3, 5, 2, 7]) predicted = np.array([2, 5, 4, 8]) rmse = np.sqrt(np.mean((predicted - actual) ** 2)) mae = np.mean(np.abs(predicted - actual)) print(round(float(rmse), 3), round(float(mae), 3))
Output
1.225 1.0
التمرين 2
حمّل داتا California housing، اطبع المتوسط لعمود MedHouseVal، ثم قسّمها 80/20 بـ random_state=42 واطبع حجم الـ train والـ test.
Load California housing, print the mean of MedHouseVal, then split 80/20 with random_state=42 and print the train and test sizes.
(output appears here)
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
df = fetch_california_housing(as_frame=True).frame
print('mean value:', round(df['MedHouseVal'].mean(), 3))
train, test = train_test_split(df, test_size=0.2, random_state=42)
print('train:', len(train), '| test:', len(test))
Output
mean value: 2.069 train: 16512 | test: 4128
مشروع صغير في آخر المحاضرة
قسّم الداتا صح وحدّد الـ baseline
الداتا: Diabetes dataset: 442 patients, 10 baseline features, target = disease progression after one year. Source: Efron, Hastie, Johnstone, Tibshirani (Least Angle Regression, 2004).
License / source: Ships with scikit-learn. Dataset page
الخطوات
- حمّل الداتا واطبع شكلها وأول قيم للـ target.
- قسّم ٨٠٪ تدريب و٢٠٪ اختبار بـ random_state=42.
- احسب RMSE لـ baseline بيتوقّع متوسط الـ training دايماً.
- درّب LinearRegression واحسب RMSE على الـ test.
- قارن الرقمين: النموذج أحسن من الـ baseline بكام؟
Expected output (press Run to produce your own): baseline RMSE: 73.2 linear regression RMSE: 53.9
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.