LECTURE 04 | المحاضرة 04

Machine Learning

التعلم الآلي
Data, Regression, Classification, Training, and Evaluation
البيانات والانحدار والتصنيف والتدريب وتقييم النماذج

🎯 Learning Objectives

  • Understand the basic idea of Machine Learning.
  • Distinguish supervised and unsupervised learning.
  • Understand features, targets, training data, and test data.
  • Derive the basic linear-regression model.
  • Understand the mean-squared-error loss function.
  • Explain training, validation, and testing.
  • Understand overfitting and underfitting.
  • Apply Machine Learning to load forecasting.

🎯 أهداف التعلم

  • فهم الفكرة الأساسية للتعلم الآلي.
  • التمييز بين التعلم الخاضع وغير الخاضع للإشراف.
  • فهم الميزات والمتغير الهدف وبيانات التدريب والاختبار.
  • اشتقاق نموذج الانحدار الخطي الأساسي.
  • فهم دالة متوسط مربع الخطأ.
  • فهم التدريب والتحقق والاختبار.
  • فهم فرط المطابقة ونقص المطابقة.
  • تطبيق التعلم الآلي على التنبؤ بالحمل الكهربائي.

1. What is Machine Learning?

Machine Learning is a branch of Artificial Intelligence in which a computer learns patterns and relationships from data.

Instead of programming every rule explicitly, we provide examples, and the model learns the underlying relationship.
Data
Learning Algorithm
Model
Prediction

1. ما هو التعلم الآلي؟

التعلم الآلي هو أحد فروع الذكاء الاصطناعي، حيث يتعلم الحاسوب الأنماط والعلاقات من البيانات.

بدلًا من كتابة جميع القواعد يدويًا، نزود الخوارزمية بأمثلة وبيانات، ثم يتعلم النموذج العلاقة الموجودة فيها.

2. Features and Target

In supervised learning, the input variables are called features, while the value to be predicted is called the target.

x = [x₁, x₂, ..., xₙ]
y = target

For load forecasting, the features may include temperature, hour, day type, and historical load.

2. الميزات والمتغير الهدف

في التعلم الخاضع للإشراف تسمى متغيرات الدخل بالميزات، بينما تسمى القيمة المطلوب التنبؤ بها بالمتغير الهدف.

x = [x₁, x₂, ..., xₙ]
y = الهدف

في التنبؤ بالحمل يمكن أن تشمل الميزات درجة الحرارة والساعة ونوع اليوم والحمل التاريخي.

3. Supervised vs Unsupervised Learning

Supervised Learning

Training data contain inputs and known outputs.

Examples: regression and classification.

Unsupervised Learning

Training data contain inputs only, without known target values.

Example: clustering.

3. التعلم الخاضع وغير الخاضع للإشراف

التعلم الخاضع للإشراف

تحتوي بيانات التدريب على المدخلات والقيم الصحيحة للمخرجات.

من أمثلته الانحدار والتصنيف.

التعلم غير الخاضع للإشراف

تحتوي البيانات على المدخلات فقط دون وجود قيمة هدف معروفة.

من أمثلته التجميع Clustering.

4. Regression

Regression is used when the target is a continuous numerical value.

Example: predict electrical load = 5.73 MW

The simplest regression model is linear regression.

ŷ = wx + b

where:

  • w = model weight or slope
  • b = bias or intercept
  • x = input
  • ŷ = predicted output

4. الانحدار

يستخدم الانحدار عندما تكون القيمة الهدف قيمة عددية مستمرة.

مثال: التنبؤ بأن الحمل الكهربائي القادم يساوي 5.73 MW.

أبسط نموذج للانحدار هو الانحدار الخطي:

ŷ = wx + b

5. Why Does Linear Regression Have This Form?

The equation of a straight line is:

y = mx + c

In Machine Learning notation, we simply rename:

m → w
c → b

therefore:

ŷ = wx + b
Linear regression is therefore simply a straight-line model whose parameters w and b are learned from data.

5. لماذا يأخذ الانحدار الخطي هذه الصيغة؟

معادلة الخط المستقيم هي:

y = mx + c

وفي التعلم الآلي نغيّر الرموز فقط:

m → w
c → b

فنحصل على:

ŷ = wx + b
إذن الانحدار الخطي ليس سوى نموذج خط مستقيم يتم تعلم ميله وانزياحه من البيانات.

6. Visual Meaning of Linear Regression

Input x Target y

The blue points are measured data. The yellow line is the model.

6. المعنى الرسومي للانحدار الخطي

تمثل النقاط الزرقاء البيانات المقاسة، بينما يمثل الخط الأصفر النموذج الذي نحاول مطابقته مع البيانات.

7. Prediction Error

For each training example, the prediction error is:

eᵢ = ŷᵢ - yᵢ

If the prediction is exact:

eᵢ = 0

7. خطأ التنبؤ

لكل مثال تدريب نحسب الفرق بين القيمة المتنبأ بها والقيمة الحقيقية:

eᵢ = ŷᵢ - yᵢ

إذا كان التنبؤ دقيقًا تمامًا فإن:

eᵢ = 0

8. Mean Squared Error

We need one number that summarizes the errors of all training samples.

One common choice is Mean Squared Error:

MSE = (1/N) Σ (ŷᵢ - yᵢ)²

Why square the errors?

  • Positive and negative errors cannot cancel each other.
  • Large errors receive a stronger penalty.
  • The function is mathematically convenient for optimization.

8. متوسط مربع الخطأ

نحتاج إلى قيمة واحدة تلخص أخطاء جميع أمثلة التدريب.

إحدى أشهر الدوال هي متوسط مربع الخطأ:

MSE = (1/N) Σ (ŷᵢ - yᵢ)²

لماذا نقوم بتربيع الخطأ؟

  • حتى لا تلغي الأخطاء الموجبة الأخطاء السالبة.
  • حتى نعطي الأخطاء الكبيرة عقوبة أكبر.
  • لأن هذه الدالة مناسبة رياضيًا لعمليات التحسين.

9. Training as an Optimization Problem

Training means finding model parameters that minimize the loss.

w*, b* = arg min MSE(w,b)
Learning = changing model parameters until the prediction error becomes small.

9. التدريب كمسألة تحسين

يعني التدريب إيجاد قيم المعاملات التي تجعل دالة الخطأ أصغر ما يمكن.

w*, b* = arg min MSE(w,b)
التعلم يعني تعديل معاملات النموذج تدريجيًا حتى يصبح خطأ التنبؤ صغيرًا.

10. A Simple Gradient Idea

The derivative tells us how the loss changes when a parameter changes.

dJ/dw

If the derivative is positive, increasing w increases the loss. Therefore, we move w in the opposite direction.

w(new) = w(old) - η dJ/dw

where η is the learning rate.

10. فكرة بسيطة عن التدرج

يخبرنا الاشتقاق كيف تتغير دالة الخطأ عندما نغير أحد معاملات النموذج.

dJ/dw

إذا كان الاشتقاق موجبًا فهذا يعني أن زيادة w ترفع الخطأ، ولذلك نتحرك في الاتجاه المعاكس.

w(new) = w(old) - η dJ/dw

حيث η هو معدل التعلم.

11. Classification

Classification is used when the target belongs to a category.

Fault Classification

Healthy / Faulty

Operating-State Classification

Normal / Warning / Emergency

11. التصنيف

يستخدم التصنيف عندما تكون القيمة المطلوبة فئة وليست قيمة عددية مستمرة.

تصنيف الأعطال

سليم / معطّل

تصنيف حالة التشغيل

طبيعية / تحذير / طوارئ

12. Training, Validation, and Testing

Dataset
Training Set
+
Validation Set
+
Test Set
Training Set

Used to learn model parameters.

Validation Set

Used to tune model choices and hyperparameters.

Test Set

Used for final unbiased performance evaluation.

12. التدريب والتحقق والاختبار

بيانات التدريب

تستخدم لتعلم معاملات النموذج.

بيانات التحقق

تستخدم لاختيار إعدادات النموذج المناسبة.

بيانات الاختبار

تستخدم لتقييم النموذج بعد انتهاء عملية التدريب.

13. Underfitting and Overfitting

Underfitting

The model is too simple and cannot capture the important relationship.

Overfitting

The model memorizes training data but performs poorly on new data.

A good model should learn the general pattern, not memorize the training examples.

13. نقص المطابقة وفرط المطابقة

نقص المطابقة

النموذج بسيط جدًا ولا يستطيع تمثيل العلاقة الموجودة في البيانات.

فرط المطابقة

يتعلم النموذج بيانات التدريب بصورة مفرطة لكنه يفشل مع بيانات جديدة.

النموذج الجيد يتعلم النمط العام ولا يحفظ أمثلة التدريب فقط.

14. Common Regression Metrics

Metric Equation Meaning
MAE (1/N) Σ |ŷᵢ-yᵢ| Average absolute error
MSE (1/N) Σ (ŷᵢ-yᵢ)² Average squared error
RMSE √MSE Error in original physical units

14. مقاييس شائعة لتقييم الانحدار

تستخدم MAE وMSE وRMSE لقياس الفرق بين القيم الحقيقية والمتنبأ بها.

يتميز RMSE بأن وحدته هي نفس وحدة المتغير الهدف، مثل MW عند التنبؤ بالحمل.

15. Power-System Application: Load Forecasting

Suppose we want to predict electrical load for the next hour.

Possible input features:

x = [Temperature, Hour, DayType, PreviousLoad]

Target:

y = NextHourLoad
Weather + Time + Historical Load
ML Model
Predicted Load

15. تطبيق في نظم الطاقة: التنبؤ بالحمل

لنفترض أننا نريد التنبؤ بالحمل الكهربائي للساعة القادمة.

يمكن أن تكون المدخلات:

x = [درجة الحرارة، الساعة، نوع اليوم، الحمل السابق]

أما الهدف فهو:

y = الحمل في الساعة القادمة

16. Simple Load-Forecasting Example

Assume a simplified model using temperature only:

ŷ = 0.18T + 2.4

If:

T = 20°C

then:

ŷ = 0.18(20) + 2.4
ŷ = 6.0 MW
The model predicts an electrical load of 6 MW.

16. مثال مبسط للتنبؤ بالحمل

لنفترض استخدام نموذج مبسط يعتمد على درجة الحرارة فقط:

ŷ = 0.18T + 2.4

إذا كانت:

T = 20°C

فإن:

ŷ = 6.0 MW
أي أن النموذج يتنبأ بحمل كهربائي مقداره 6 MW.

17. Machine-Learning Workflow

Collect Data
Clean Data
Choose Features
Train Model
Evaluate
Deploy

17. خطوات مشروع التعلم الآلي

جمع البيانات ← تنظيفها ← اختيار الميزات ← تدريب النموذج ← تقييمه ← استخدامه

18. MATLAB Concept

A simple linear-regression model can be evaluated in MATLAB using:

yhat = w*x + b

The error can be calculated using:

e = yhat - y

and the mean-squared error:

MSE = mean(e.^2)

Later, the MATLAB lab will update w and b iteratively.

18. الفكرة البرمجية في MATLAB

يمكن حساب خرج نموذج الانحدار الخطي في MATLAB من خلال:

yhat = w*x + b

ثم حساب الخطأ:

e = yhat - y

ثم متوسط مربع الخطأ:

MSE = mean(e.^2)

19. Summary

  • Machine Learning learns patterns from data.
  • Features are inputs and targets are outputs to be predicted.
  • Regression predicts continuous numerical values.
  • Classification predicts categories.
  • Linear regression has the form ŷ = wx + b.
  • MSE measures prediction error.
  • Training minimizes a loss function.
  • Good models should generalize to unseen data.
  • Load forecasting is an important power-system ML application.

19. الخلاصة

  • يتعلم التعلم الآلي الأنماط من البيانات.
  • الميزات هي المدخلات والهدف هو القيمة المطلوب التنبؤ بها.
  • يستخدم الانحدار للتنبؤ بالقيم العددية المستمرة.
  • يستخدم التصنيف للتنبؤ بالفئات.
  • صيغة الانحدار الخطي هي ŷ = wx + b.
  • يستخدم MSE لقياس خطأ التنبؤ.
  • تهدف عملية التدريب إلى تصغير دالة الخطأ.
  • يجب أن يعمل النموذج الجيد جيدًا مع بيانات لم يرها من قبل.
  • يعد التنبؤ بالحمل من أهم تطبيقات التعلم الآلي في نظم الطاقة.

📝 Review Questions

  1. What is Machine Learning?
  2. What is the difference between a feature and a target?
  3. What is supervised learning?
  4. What is the difference between regression and classification?
  5. Explain the model ŷ = wx + b.
  6. Why do we square prediction errors in MSE?
  7. What is the purpose of training data?
  8. What is overfitting?
  9. How can Machine Learning be used for load forecasting?

📝 أسئلة المراجعة

  1. ما هو التعلم الآلي؟
  2. ما الفرق بين الميزة والمتغير الهدف؟
  3. ما المقصود بالتعلم الخاضع للإشراف؟
  4. ما الفرق بين الانحدار والتصنيف؟
  5. اشرح النموذج ŷ = wx + b.
  6. لماذا نقوم بتربيع الأخطاء في MSE؟
  7. ما وظيفة بيانات التدريب؟
  8. ما المقصود بفرط المطابقة؟
  9. كيف يمكن استخدام التعلم الآلي للتنبؤ بالحمل الكهربائي؟