[ Machine Learning ]
What is Machine Learning?
A set of Algorithms that gives the computers the ability to learn from data,
and then make predictions and decisions.
ML vs DL vs Ai ?
Artificial Intelligence (Ai):
study/process which enables machines
to mimic human behavior,
through particular algorithm.
Machine Learning (ML):
ML is the subset of Ai.
ML is an Ai algorithm which allows system
to learn from data and make decisions
Deep Learning (DL):
DL is the subset of ML.
DL is a ML algorithm that uses deep
(more than one layer) neural networks
to analyze data and provide output
Why Machine Learning ?
• As a part of the Ai revolution, machine learning will transform every business
both online and offline – how decisions are made, how processes improve
• This technology will redefine many industries, such as: Healthcare, Financial services, Energy
and many other fields that directly impact our lives
• The power of machine learning is allowing us to solve more complex problems,
faster than any time before.
How Does it Work ?
What is a machine learning Model ?
• A “model” in machine learning is the output of a machine learning algorithm run on data.
• The best analogy is to think of the machine learning model as a “program.”
• Model == Model Data + Prediction Algorithm
Data Mining:
• The process of extracting useful information from a huge amount of data
• It’s a tool used by humans to discover new, accurate, and useful patterns in the data.
You first identify business goals and data mining goal, then you start this process:
Machine Learning vs Data Mining :
Machine Learning Process Work Flow:
High level ML Workflow Detailed ML Process Workflow
Also called the ML life-cycle,
this high-level workflow gives an
intuition about the overall work
through the ML process.
A more detailed version is present
to give a deeper insight about
the actual steps in each phase.
About Data Cleaning (Scrubbing):
Data cleaning is the process of fixing or removing incorrect, corrupted, incorrectly formatted, duplicate, or
incomplete data within a dataset. When combining multiple data sources, there are many opportunities for
data to be duplicated or mislabeled. garbage in, garbage out!
If data is incorrect, outcomes and algorithms are unreliable, even though they may look correct .
Steps:
1. Duplicate & irrelevant observations
removing duplicated to avoid inaccuracy and skewness.
2. Structural errors (normalize & standardize)
Handling strange naming conventions, typos
incorrect capitalization, inconsistent data formats.
3. Filter unwanted outliers
handling outliers, such as removing them or imputing them
with more reasonable values.
4. Handle missing data
handle them by imputing missing data, dropping the missing data
or leaving it as-is
5. Verify data quality
checking for consistency between different parts of the
dataset, and validating the accuracy of the data against external sources
Types of Machine Learning:
Basic Terminology:
Labels
A label is the thing we're predicting—the dependent y variable in simple linear regression.
The label could be the future price of wheat, the kind of animal shown in a picture… etc.
Features
A feature is an input variable—the independent x variable in simple linear regression.
Features could either be:
• Qualitative: categorical data (genders, nationality, marital status…)
• Quantitative: numerical data, both discrete and continuous (price, temperature …etc.)
Examples
An example is a particular instance of data, x (feature vector). We break examples into two categories:
• labeled examples: includes both feature(s) and the label.
• unlabeled examples: contains features but not the label.
Supervised vs Unsupervised:
Supervised machine learning:
You supervise the machine while training it to work on its own. This requires labeled training data
Unsupervised machine learning
Unsupervised learning: There is training data, but it’s unlabeled
Supervised Learning in Detail:
The goal of supervised machine learning is to learn a mapping function that can accurately
predict the target variable (or output) for new, unseen input data.
1- Step One: Preparing the data (data split)
first of all, the data is divided into 3 sets:
1. Training set: portion of the labeled data
used to train the machine learning model.
2. Validation set: portion of the labeled data
used as a reality check during/after training to:
• Ensure the model can handle new, unseen data
• Choosing the model with minimum loss *
• Tune the model’s hyperparameters**
• Prevent the model from overfitting***
3. Test set: portion of the labeled data,
used after completing the training to check
how generalizable the final chosen model is.
data splitting techniques:
Various data splitting techniques can be used like :
• Random (Holdout method)
the most basic and straight forward method, but it comes with a drawback,
For example, if a dataset has 1000 images, of which 800 belong to the category: “dog” and 200 belong to the
category: “cat,” it might so happen that the training set consists only dog images, while the validation set
consists of only cat images!
• Stratified
dividing the labeled data into subsets based on the distribution of the target variable.
Suppose a dataset consists of 1000 images, of which 600 are dog images and 400 are cat images. In that case,
stratified sampling ensures that 60% of the images are of category “dog” and 40% are of category “cat” in the
training and validation sets. can be used in conjunction with the holdout method or k-fold cross-validation.
• Cross-Validation (k-fold cross validation)
a more robust technique for data splitting, where we divide the labeled data into K equal-sized subsets, or
"folds". The model is trained K times, each time using a different fold as the validation set and the remaining
folds as the training set. The performance of the model is then averaged across the K runs.
When used with “Stratified” method, it creates the Stratified k-fold cross-validation
2- Step Two: Choosing an Algorithm
Based on the task at hand, we can one of two categories of supervised learning:
Regression
Used to predict continuous values such as sales, salary, weight, or temperature.
For example: A dataset containing features of the house such as size, number of bedrooms, number of
baths, neighborhood, etc. and the price of the house, a Regression algorithm can be trained to learn the
relationship between the features and the price of the house.
@ Common Regression Algorithms:
• Linear Regression
• Polynomial Regression
• #Decision Tree Regressor*
• #Random Forest Regressor*
• Neural Networks
Classification
Used for predicting discrete outcomes, if the outcome can take two possible values such as
[True or False], [Dog or Cat], [Yes or No], it is known as Binary Classification. When the outcome contains
more than two possible values [dog, cat, lizard…], it is known as Multiclass Classification.
@ Common Classification Algorithms:
• k-Nearest Neighbor (k-NN)
• Support Vector Machine (SVM)
• Naïve Bayes
• #Decision Tree Classifier*
• #Random Forest Classifier*
• Logistic Regression
@ Evaluation Metrices:
Confusion Matrix
Recall: how many relevant items are retrieved?
Calculated as = TP / (TP + FN).
Precision: how many retrieved items are relevant?
Calculated as = TP / (TP + FP).
F1-Score: The harmonic mean of precision and recall.
Supervised Algorithms Recap (AR) //
k-Nearest Neighbor (k-NN) -1
دى خوارزمية تصنيف ( ,)Classificationالغرض منها بعد ما تدرب المودل بتاعك
انها تقدر تحدد العينة الجديدة اللى دخلت تقع فى انهى تصنيف من اللى
المودل اتدرب عليهم ...
بنعمل ده عن طريق مقارنة العنصر الجديد باقرب جيرانه من ال data points
اللى حواليه ,والرمز kبيشير لعدد الجيران اللى هنشتغل عليهم ونحسب
مسافة العنصر الجديد منهم عشان نعرف بينمتى لمين بنسبة اكبر .
Naïve Bays -2
خوارزمية تصنيف معتمدة على نظرية Baysفى االحتماالت
وكلمة Naïveهنا (واللى معناها ساذج) بترمز الفتراض ان
وجود صفة أ فى شئ ملهاش عالقة بوجود صفة ب فيه !
بمعنى ان كل صفة "مستقلة" وليها نسبة خاصة بيها انها تكون
موجودة فى الشئ ده أو أل .
مثال لو عندك قطط وكالب ,وعندك الصفات دى
(ودان صغيرة ,فرو ,لون برتقانى ,ديل طويل)...
فانت بتحسب نسبة وجود كل صفة "لوحدها" عند الكالب و القطط
ولما يجيلك كائن جديد بتشوف نسبة كل صفة من دول عندو ايه
وبعدين تحسب "احتمال" كونه كلب او قطة حسب مجموع نسب الصفات دى.
Logistics Regression -3
خوارزمية تنصيف ثنائية ( )Binary Classificationبتجاوبك بنتيجة من اتنين
اه/ال ,ناجح/راسب ,مقبول/مرفوض ...الخ
● فكرة عملها انه الصفة المتغير ( ) independent variable xفى بينها
وبين النتيجة المراد توقعها ( ) dependent variable yعالقة خطية
وبناءا عليه نقدر نتوقع النتيجة النهائية حسب درجة الصفة المتغيرة دى
مثال ,لو جالنا شخص عمره 32سنة ,فحسب الموديل ده
هو سنه اقل من الحد الفاصل 40( Threshold Valueسنة)
يعنى احتمالية انه يشترى التامين اقل ,فبنصنفه على انه ()No
النه اقرب للفئة دى احصائيا.
Support Victor Machine (SVM) -4
خوارزمية تصنيف فكرتها االساسية قائمة على رسم
حد فاصل Hyperplaneيقسم الداتا لمجموعتين بافضل شكل
ممكن ,عشان لما تيجى داتا جديدة اقدر اصنفها بسهولة ....
● والمقياس الختيار الخط الفاصل ده ان المسافة بين
الخط بتاعنا وبين اقرب نقط داتا تكون اكبر ما يمكن
من كال الطرفين وبنسميها . Maximum Margin
● مصطلح Support Vectorبيطلق على ال datapointsاللى بتكون على اطراف
ال Hyperplaneواللى بتساعدنى فى التصنيف بحيث لو جاتلى داتا جديدة
بصنفها تبع المجموعة االقرب لل Support Vectorده .
Decision Trees -5
الخوارزمية دى ممكن تستخدم لتوقع تنصيف معين وساعتها
بنسميها Classification Tree :او لتوقع قيمة معينة وساعتها
بنسميها . Regression Tree :
الشجرة بتاعتنا عامة بيكون ليها نقطة بداية بنسميها Root Node
و بينزل منها فروع Branchesاو Decision Nodes
وفى النهاية عندنا ال Leavesواللى بتشاور على تصنيف معين
👈فى حالة ال Classification Treeاهم حاجة هو ال Split
بمعنى انك تحدد هتبدأ بانهى صفة ك , Root Nodeوالقرار ده
بيتبنى بكذا طريقة زى Entropy :و ...Gini Impurity
واالخيرة دى بتتبنى على معيار ال Impurityوهو مستوى
نقاء و جودة ارتباط الصفة دى بالنتيجة النهائية ,
وكل ما كانت قيمة ال impurityاقل كل ما كان جودة
الصفة وارتباطها بالنتيجة النهائية اعلى.
● لما نيجى نقرر انهى صفة هتكون هى ال Root Node
بنحسب ال Gini Impurityلكل صفة رئيسية Featureوبعدين
ناخد الصفة االكثر نقاءا ( اقل ) impurityوتبقى هى رأسه الهرم
وهكذا بنستمر على نفس الطريقة بحيث ال Leafالنهائية
تكون قرار حاسم قدر االمكان ( ) Impurity =~ 0
Random Forest -6
دى كمان خوارزمية بتستخدم لتوقع تصنيف معين
او قيمة معينة ()Classification & Regression
● الخوارزمية دى بتعالج مشكلة التداخل Overfitting
اللى كانت بتحصل فى خوارزمية ال Decision Tree
لما بتقابلك داتا فيها كذا Featureوبتبقى الشجرة
بتاعتك متشعبة وعميقة جدا ()Deep & Complex
● كمان بترفع نسبة الدقة Accuracyبتاعة النتيجة النهائية
باستخدام مفهوم ال , Baggingوهو ان بتجمع نتيجة كذا
خوارزمية متشابهه سوى ( كذا ) Treeوفى االخر بتعمل
اما Votingلو غرضك التصنيف او Averageلو غرضك رقم او قيمة عددية.
● الخطوة االولى المختلفة هنا انك بتقسم ال Training dataلعدد من األجزاء بطريقة اسمها Bootstrap Sampling
وكل جزء بتدخله على شجرة لوحده تتدرب عليه وتديلك نتيجة.
● والخطوة االخيرة زى ما قلنا انك بتجمع النتايج دى كلها وبناءا عليها بتاخد قرار نهائى.
Linear Regression -7
اول وابسط خوارزمية لتوقع قيمة مجهولة بناءا على عالقة Correlation
بينها وبين قيمة /عدة قيم معلومة ,زى مثال العمر والطول هنا
على محور السينات independent variable xبنحط قراءات الصفة المتغير
على محور الصادات dependent variable yبنقيس قيمة النتيحة المطلوبة
المسافة بين اى نقطة وبين الخط المرسوم بتسمى )R( residual
● اول واهم خطوة فى تنفيذ الموديل بعد عرض الداتا على Scatter plot
مثال ,هو اختيار مكان خط االنحدار Regression lineاللى هيوضحلنا
قوة و اتجاه العالقة بين الصفة المتغيرة xوالقيمة المراد استنتاجها y
● من اشهر الطرق فى اختيار خط االنحدار هى
ال Least Squaresواللى فيها ببساطة بنحسب
مجموع R2بتاع النقط لكل خط افتراضى ,وبنختار
الخط اللى قيمة R2الكلية عنده اصغر قيمة
● وخالص كدا بعد ما بنحدد الخط بتاعنا والموديل
يبقى جاهز ,بندخل داتا للصفة xوهو بيتوقع y
Unsupervised Algorithms Recap (AR) //
Clustering -8
خوارزمية تجميع داتا ,هدفها انها تجمع العينات اللى في بينها صفات مشتركة
وعندنا كذا اسلوب فى التجميع على حسب حالة الداتا :
: (K-Mean) Exclusive Clustering
يا ابيض يا اسود ,مفيش وسط ...العينة الزم تنتمى لمجموعة واحدة بس
: (C-Mean) Overlapping Clustering
ابيض واسود ورمادى ...مفيش مشكلة ان عينة تنتمى الكتر من مجموعة فى نفس الوقت
● من اشهر الخوارزميات فى التجميع واللى هنركز عليها هى k-Mean Clustering
وطبعا زى ما اخدت بالك هى بتتبع اسلوب ال Exclusive Clustering
فى اربع خطوات بنمشى عليهم عشان نوصل للنتيجة النهائية:
-1تحديد عدد المجموعات ()K
وده ليه طرق رياضية زى ال Elbow Methodبس نقدر نبدأ
برقم عشوائى وليكن 3مثال
-2اختيار مكان المحاور Centroidsعشوائيا
دور المحاور دى انك بتقيس البعد العمودى لكل نقطة
وتديها لون حسب اقرب محور ليها
-3حساب معدل التقارب Convergenceلكل مجموعة
وده عن طريق حساب متوسط بعد النقط عن المحور
Mean Distanceفى المجموعة الواحدة .خليك فاكر الرقم ده !
( كلمة Meanفى االسم جاية من هنا )
-4اختيار مكان جديد للمحاور !
وهتعيد خطوة رقم 3تانى وتحسب معدل التقارب
الخطوة دى هتكرر عدد مرات معين ( )Iterationsمحدد مسبقا
او لحد ما توصل الفضل معدل تقارب ممكن !
Association Rule -9
Data Mining قاعدة االرتباط هى مفهوم بيستخدم فى ال
وبيشير لوجود ارتباط بين عنصرين او اكترMachine Learning وال
● بتستخدم بشكل واسع فى مجال التجارة لفهم االنماط الشرائية
واللى فيها بتدرس ارتباطMarket Basket Analysis عن طريق
العناصر اللى اشتراها العمالء ببعض
.● الغرض االساسى للمتاجر هو عامة زيادة المبيعات
:وطبعا معلومة زى دى ممكن تفيدهم باكتر من شكل زى
تنظيم رفوف العرض بحيث المنتجات اللى بتتباع سوىتكون قريبة من بعض فتعلى نسبة المبيعات -
)الخ.. بناءا على قوة االرتباط بينهم ( زى منتجات االطفال والعربيات,تسويق منتجات معينة مع بعضها -
استهداف شرائح من العمالء بعروض وخصومات خاصة بناءا على نمطهم الشرائى -
Association Rule Mining ● من اشهر الخوارزميات المستخدمة فى ال
" Frequency واللى بتستخدم مبدأ " التكرارApriori Algorithm هى
. بين العناصر المطلوبةAssociation عشان تستنج وجود
)Transactions( ● بناءا على تحليل عمليات الشراء السابقة للعمالء
واللى منهاfrequent itemset نقدر نستخرج المجموعات المتكررة
. نقدر نعرف العناصر المرتبط ظهورها ببعض
----------------------------------------------------------------------------------------------------------------------------------------
Model Evaluation Metrices
Bias: the difference between the predicted values and the actual values.
high-bias results in underfitting, the model is too simple and can’t capture trends in the dataset.
Variance: the variability of the model's predictions for different training sets.
high-variance results in overfitting, the model is too complex and performs well on the training data
but poorly on new, unseen test data.
The bias-variance trade-off:
As the complexity of the model increases,
the bias decreases but the variance increases, and vice versa.
ROC(Receiver Operating Characteristic) : a graphical plot that shows
the performance of a binary classifier system as its discrimination
threshold is varied. such as logistic regression, decision trees,
random forests, support vector machines.