03 - Machine Learning Overview
03 - Machine Learning Overview
03 - Machine Learning Overview
Foreword
2 Huawei Confidential
Objectives
3 Huawei Confidential
Contents
6. Case Study
4 Huawei Confidential
Machine Learning Algorithms (1)
l Machine learning (including deep learning) is a study of learning algorithms. A
computer program is said to learn from experience � with respect to some class of
tasks � and performance measure � if its performance at tasks in �, as measured
by �, improves with experience �.
Learning Basic
Data
algorithms understanding
(Experience E)
(Task T) (Measure P)
5 Huawei Confidential
Machine Learning Algorithms (2)
Experience Historical
data
Induction Training
6 Huawei Confidential
Created by: Jim Liang
Training
data
Machine
learning
7 Huawei Confidential
Application Scenarios of Machine Learning (1)
l The solution to a problem is complex, or the problem may involve a large amount
of data without a clear data distribution function.
l Machine learning can be used in the following scenarios:
Rules are complex or cannot Task rules change over time. Data distribution changes over
be described, such as facial For example, in the part-of- time, requiring constant
recognition and voice speech tagging task, new readaptation of programs, such
recognition. words or meanings are as predicting the trend of
generated at any time. commodity sales.
8 Huawei Confidential
Application Scenarios of Machine Learning (2)
Complex
Machine learning
Manual rules
algorithms
Rule complexity
Simple
Rule-based
Simple problems
algorithms
Small Large
9 Huawei Confidential
Rational Understanding of Machine Learning Algorithms
Target equation
�: � → �
Ideal
Actual
Training data Hypothesis function
Learning algorithms
�: {(�1 , �1 )⋯, (�� , �� )} �≈�
10 Huawei Confidential
Main Problems Solved by Machine Learning
l Machine learning can deal with many types of tasks. The following describes the most typical and common types
of tasks.
p Classification: A computer program needs to specify which of the k categories some input belongs to. To accomplish this task,
learning algorithms usually output a function �: �� → (1,2, …, �). For example, the image classification algorithm in computer
vision is developed to handle classification tasks.
p Regression: For this type of task, a computer program predicts the output for the given input. Learning algorithms typically
output a function �: �� → �. An example of this task type is to predict the claim amount of an insured person (to set the
insurance premium) or predict the security price.
p Clustering: A large amount of data from an unlabeled dataset is divided into multiple categories according to internal
similarity of the data. Data in the same category is more similar than that in different categories. This feature can be used in
scenarios such as image retrieval and user profile management.
l Classification and regression are two main types of prediction, accounting from 80% to 90%. The output of
classification is discrete category values, and the output of regression is continuous numbers.
11 Huawei Confidential
Contents
6. Case study
12 Huawei Confidential
Machine Learning Classification
l Supervised learning: Obtain an optimal model with required performance through training and learning based on
the samples of known categories. Then, use the model to map all inputs to outputs and check the output for the
purpose of classifying unknown data.
l Unsupervised learning: For unlabeled samples, the learning algorithms directly model the input datasets.
Clustering is a common form of unsupervised learning. We only need to put highly similar samples together,
calculate the similarity between new samples and existing ones, and classify them by similarity.
l Semi-supervised learning: In one task, a machine learning model that automatically uses a large amount of
unlabeled data to assist learning directly of a small amount of labeled data.
l Reinforcement learning: It is an area of machine learning concerned with how agents ought to take actions in an
environment to maximize some notion of cumulative reward. The difference between reinforcement learning and
supervised learning is the teacher signal. The reinforcement signal provided by the environment in reinforcement
learning is used to evaluate the action (scalar signal) rather than telling the learning system how to perform
correct actions.
13 Huawei Confidential
Machine Learning Classification
l Supervised learning: Obtain an optimal model with required performance through training and learning based on
the samples of known categories. Then, use the model to map all inputs to outputs and check the output for the
purpose of classifying unknown data.
l Unsupervised learning: For unlabeled samples, the learning algorithms directly model the input datasets.
Clustering is a common form of unsupervised learning. We only need to put highly similar samples together,
calculate the similarity between new samples and existing ones, and classify them by similarity.
l Semi-supervised learning: In one task, a machine learning model that automatically uses a large amount of
unlabeled data to assist learning directly of a small amount of labeled data.
l Reinforcement learning: It is an area of machine learning concerned with how agents ought to take actions in an
environment to maximize some notion of cumulative reward. The difference between reinforcement learning and
supervised learning is the teacher signal. The reinforcement signal provided by the environment in reinforcement
learning is used to evaluate the action (scalar signal) rather than telling the learning system how to perform
correct actions.
14 Huawei Confidential
Supervised Learning
Data feature Label
Supervised learning
Feature 1 ... Feature n Goal
algorithm
Wind Enjoy
Weather Temperature
Speed Sports
Sunny Warm Strong Yes
Rainy Cold Fair No
Sunny Cold Weak Yes
15 Huawei Confidential
Supervised Learning - Regression Questions
l Regression: reflects the features of attribute values of samples in a sample dataset. The
dependency between attribute values is discovered by expressing the relationship of
sample mapping through functions.
p How much will I benefit from the stock next week?
p What's the temperature on Tuesday?
16 Huawei Confidential
Supervised Learning - Classification Questions
l Classification: maps samples in a sample dataset to a specified category by using a
classification model.
p Will there be a traffic jam on XX road during
the morning rush hour tomorrow?
p Which method is more attractive to customers:
5 yuan voucher or 25% off?
17 Huawei Confidential
Unsupervised Learning
Data Feature
Unsupervised Internal
Feature 1 ... Feature n similarity
learning algorithm
Monthly Consumption
Commodity
Consumption Time Category
Badminton Cluster 1
1000–2000 6:00–12:00
racket
Cluster 2
500–1000 Basketball 18:00–24:00
1000–2000 Game console 00:00–6:00
18 Huawei Confidential
Unsupervised Learning - Clustering Questions
l Clustering: classifies samples in a sample dataset into several categories based on
the clustering model. The similarity of samples belonging to the same category is
high.
p Which audiences like to watch movies
of the same subject?
p Which of these components are
damaged in a similar way?
19 Huawei Confidential
Semi-Supervised Learning
Data Feature Label
Semi-supervised
Feature 1 ... Feature n Unknown
learning algorithms
Wind Enjoy
Weather Temperature
Speed Sports
Sunny Warm Strong Yes
Rainy Cold Fair /
Sunny Cold Weak /
20 Huawei Confidential
Reinforcement Learning
l The model perceives the environment, takes actions, and makes adjustments and
choices based on the status and award or punishment.
Model
Award or Action ��
Status ��
punishment ��
��+1
��+1 Environment
21 Huawei Confidential
Reinforcement Learning - Best Behavior
l Reinforcement learning: always looks for best behaviors. Reinforcement learning is
targeted at machines or robots.
p Autopilot: Should it brake or accelerate when the yellow light starts to flash?
p Cleaning robot: Should it keep working or go back for charging?
22 Huawei Confidential
Contents
6. Case study
23 Huawei Confidential
Machine Learning Process
Feature Model
Data Data Model Model
extraction and deployment and
collection cleansing training evaluation
selection integration
24 Huawei Confidential
Basic Machine Learning Concept — Dataset
l Dataset: a collection of data used in machine learning tasks. Each data record is
called a sample. Events or attributes that reflect the performance or nature of a
sample in a particular aspect are called features.
l Training set: a dataset used in the training process, where each sample is referred
to as a training sample. The process of creating a model from data is called
learning (training).
l Test set: Testing refers to the process of using the model obtained after learning
for prediction. The dataset used is called a test set, and each sample is called a test
sample.
25 Huawei Confidential
Checking Data Overview
l Typical dataset form
4 80 9 Southeast 1100
26 Huawei Confidential
Importance of Data Processing
l Data is crucial to models. It is the ceiling of model capabilities. Without good data,
there is no good model.
Data
Data
Data cleansing
preprocessing normalization
Data dimension
reduction
27 Huawei Confidential
Workload of Data Cleansing
l Statistics on data scientists' work in machine learning
28 Huawei Confidential
Data Cleansing
l Most machine learning models process features, which are usually numeric
representations of input variables that can be used in the model.
l In most cases, the collected data can be used by algorithms only after being
preprocessed. The preprocessing operations include the following:
p Data filtering
p Processing of lost data
p Processing of possible exceptions, errors, or abnormal values
p Combination of data from multiple data sources
p Data consolidation
29 Huawei Confidential
Dirty Data (1)
l Generally, real data may have some quality problems.
p Incompleteness: contains missing values or the data that lacks attributes
p Noise: contains incorrect records or exceptions.
p Inconsistency: contains inconsistent records.
30 Huawei Confidential
Dirty Data (2)
IsTe #Stu
# Id Name Birthday Gender ache dent Country City
r s
31 Huawei Confidential
Data Conversion
l After being preprocessed, the data needs to be converted into a representation form suitable for the
machine learning model. Common data conversion forms include the following:
p With respect to classification, category data is encoded into a corresponding numerical representation.
p Value data is converted to category data to reduce the value of variables (for age segmentation).
p Other data
n In the text, the word is converted into a word vector through word embedding (generally using the word2vec model, BERT
model, etc).
n Process image data (color space, grayscale, geometric change, Haar feature, and image enhancement)
p Feature engineering
n Normalize features to ensure the same value ranges for input variables of the same model.
n Feature expansion: Combine or convert existing variables to generate new features, such as the average.
32 Huawei Confidential
Necessity of Feature Selection
l Generally, a dataset has many features, some of which may be redundant or
irrelevant to the value to be predicted.
l Feature selection is necessary in the following aspects:
Simplify
models to
Reduce the
make them
training time
easy for users
to interpret
Improve
Avoid model
dimension generalization
explosion and avoid
overfitting
33 Huawei Confidential
Feature Selection Methods - Filter
l Filter methods are independent of the model during feature selection.
By evaluating the correlation between each feature
and the target attribute, these methods use a
statistical measure to assign a value to each
feature. Features are then sorted by score, which is
helpful for preserving or eliminating specific
features.
Select the
Common methods
Traverse all Train Evaluate the
features optimal models performance • Pearson correlation coefficient
feature subset • Chi-square coefficient
• Mutual information
Procedure of a filter method
Limitations
• The filter method tends to select redundant
variables as the relationship between features is
not considered.
34 Huawei Confidential
Feature Selection Methods - Wrapper
l Wrapper methods use a prediction model to score feature subsets.
35 Huawei Confidential
Feature Selection Methods - Embedded
l Embedded methods consider feature selection as a part of model construction.
Common methods
Procedure of an embedded method
• Lasso regression
• Ridge regression
36 Huawei Confidential
Overall Procedure of Building a Model
Model Building Procedure
1 2 3
6 5 4
37 Huawei Confidential
Examples of Supervised Learning - Learning Phase
l Use the classification model to predict whether a person is a basketball player.
Feature
(attribute) Target
38 Huawei Confidential
Examples of Supervised Learning - Prediction Phase
Name City Age Label
Marine Miami 45 ?
Julien Miami 52 ? Unknown data
New Recent data, it is not
Fred Orlando 20 ?
data known whether the
Michelle Boston 34 ? people are basketball
Nicolas Phoenix 90 ? players.
39 Huawei Confidential
What Is a Good Model?
• Generalization capability
Can it accurately predict the actual service data?
• Interpretability
Is the prediction result easy to interpret?
• Prediction speed
How long does it take to predict each piece of data?
• Practicability
Is the prediction rate still acceptable when the service
volume increases with a huge data volume?
40 Huawei Confidential
Model Validity (1)
l Generalization capability: The goal of machine learning is that the model obtained after learning
should perform well on new samples, not just on samples used for training. The capability of applying
a model to new samples is called generalization or robustness.
l Error: difference between the sample result predicted by the model obtained after learning and the
actual sample result.
p Training error: error that you get when you run the model on the training data.
p Generalization error: error that you get when you run the model on new samples. Obviously, we prefer a
model with a smaller generalization error.
l Underfitting: occurs when the model or the algorithm does not fit the data well enough.
l Overfitting: occurs when the training error of the model obtained after learning is small but the
generalization error is large (poor generalization capability).
41 Huawei Confidential
Model Validity (2)
l Model capacity: model's capability of fitting functions, which is also called model complexity.
p When the capacity suits the task complexity and the amount of training data provided, the algorithm effect is
usually optimal.
p Models with insufficient capacity cannot solve complex tasks and underfitting may occur.
p A high-capacity model can solve complex tasks, but overfitting may occur if the capacity is higher than that
required by a task.
l Variance: Bias
l Bias:
p Difference between the expected (or average) prediction value and the correct
value we are trying to predict.
43 Huawei Confidential
Variance and Bias
l Combinations of variance and bias are as
follows:
p Low bias & low variance –> Good model
p Low bias & high variance
p High bias & low variance
p High bias & high variance –> Poor model
44 Huawei Confidential
Model Complexity and Error
l As the model complexity increases, the training error decreases.
l As the model complexity increases, the test error decreases to a certain point and
then increases in the reverse direction, forming a convex curve.
Testing error
Error
Training error
Model Complexity
45 Huawei Confidential
Machine Learning Performance Evaluation - Regression
l The closer the Mean Absolute Error (MAE) is to 0, the better the model can fit the training data.
1 �
��� = �=1
�� − ��
m
l The value range of R2 is (–∞, 1]. A larger value indicates that the model can better fit the training data.
TSS indicates the difference between samples. RSS indicates the difference between the predicted
value and sample value.
�
2
��� �=1
(�� − �� )2
� =1− =1− �
��� �=1
(�� − �� )2
46 Huawei Confidential
Machine Learning Performance Evaluation - Classification (1)
l Terms and definitions: Estimated
amount
p �: positive, indicating the number of real positive cases yes no Total
Actual amount
in the data.
yes �� �� �
p �: negative, indicating the number of real negative cases
no �� �� �
in the data.
Total �′ �′ �+�
p �P : true positive, indicating the number of positive cases that are correctly
classified by the classifier. Confusion matrix
p ��: true negative, indicating the number of negative cases that are correctly classified by the classifier.
p ��: false positive, indicating the number of positive cases that are incorrectly classified by the classifier.
p ��: false negative, indicating the number of negative cases that are incorrectly classified by the classifier.
l Confusion matrix: at least an � × � table. ���,� of the first � rows and � columns indicates the number of cases
that actually belong to class � but are classified into class � by the classifier.
p Ideally, for a high accuracy classifier, most prediction values should be located in the diagonal from ��1,1 to ���,� of the
table while values outside the diagonal are 0 or close to 0. That is, �� and �� are close to 0.
47 Huawei Confidential
Machine Learning Performance Evaluation - Classification (2)
Measurement Ratio
�� + ��
Accuracy and recognition rate
�+�
�� + ��
Error rate and misclassification rate
�+�
Sensitivity, true positive rate, and ��
recall �
��
Specificity and true negative rate
�
��
Precision
�� + ��
�1 , harmonic mean of the recall rate 2 × ��������� × ������
and precision ��������� + ������
48 Huawei Confidential
Example of Machine Learning Performance Evaluation
l We have trained a machine learning model to identify whether the object in an image is a
cat. Now we use 200 pictures to verify the model performance. Among the 200 images,
objects in 170 images are cats, while others are not. The identification result of the model is
that objects in 160 images are cats, while others are not.
�� 140
Precision: � = ��+�� = 140+20 = 87.5% Estimated
amount
��� �� Total
Actual
�� 140 amount
Recall: � = �
=
170
= 82.4%
��� 140 30 170
��+�� 140+10
Accuracy: ��� = �+�
=
170+30
= 75% �� 20 10 30
49 Huawei Confidential
Contents
6. Case study
50 Huawei Confidential
Machine Learning Training Method - Gradient Descent (1)
l The gradient descent method uses the negative gradient Cost surface
direction of the current position as the search direction,
which is the steepest direction. The formula is as follows:
wk 1 wk f wk ( x )
i
51 Huawei Confidential
Machine Learning Training Method - Gradient Descent (2)
l Batch Gradient Descent (BGD) uses the samples (m in total) in all datasets to
update the weight parameter based on the gradient value at the current point.
1 m
wk 1 wk f wk ( x i )
m i 1
l Stochastic Gradient Descent (SGD) randomly selects a sample in a dataset to
update the weight parameter based on the gradient value at the current point.
wk 1 wk f wk ( x i )
l Mini-Batch Gradient Descent (MBGD) combines the features of BGD and SGD and
selects the gradients of n samples in a dataset to update the weight parameter.
1 t n 1
wk 1 wk f wk ( xi )
n it
52 Huawei Confidential
Machine Learning Training Method - Gradient Descent (3)
l Comparison of three gradient descent methods
p In the SGD, samples selected for each training are stochastic. Such instability causes the loss function to be
unstable or even causes reverse displacement when the loss function decreases to the lowest point.
p BGD has the highest stability but consumes too many computing resources. MBGD is a method that balances
SGD and BGD.
BGD
Uses all training samples for training each time.
SGD
Uses one training sample for training each time.
MBGD
Uses a certain number of training samples for
training each time.
53 Huawei Confidential
Parameters and Hyperparameters in Models
l The model contains not only parameters but also hyperparameters. The purpose is
to enable the model to learn the optimal parameters.
p Parameters are automatically learned by models.
p Hyperparameters are manually set.
Model
Training
Use hyperparameters
to control training.
54 Huawei Confidential
Hyperparameters of a Model
55 Huawei Confidential
Hyperparameter Search Procedure and Method
1. Dividing a dataset into a training set, validation set, and test set.
2. Optimizing the model parameters using the training set based on the model performance
indicators.
3. Searching for the model hyper-parameters using the validation set based on the model
Procedure for performance indicators.
searching 4. Perform step 2 and step 3 alternately. Finally, determine the model parameters and
hyperparameters hyperparameters and assess the model using the test set.
• Grid search
• Random search
• Heuristic intelligent search
Search algorithm • Bayesian search
(step 3)
56 Huawei Confidential
Hyperparameter Searching Method - Grid Search
l Grid search attempts to exhaustively search all possible
hyperparameter combinations to form a hyperparameter
value grid. Grid search
l In practice, the range of hyperparameter values to search is 5
specified manually.
Hyperparameter 1
4
2
p This method works well when the number of hyperparameters
is relatively small. Therefore, it is applicable to generally 1
57 Huawei Confidential
Hyperparameter Searching Method - Random Search
l When the hyperparameter search space is large, random
search is better than grid search.
Random search
l In random search, each setting is sampled from the
distribution of possible parameter values, in an attempt to
find the best subset of hyperparameters.
Parameter 1
l Note:
p Search is performed within a coarse range, which then will be
narrowed based on where the best result appears.
p Some hyperparameters are more important than others, and
Parameter 2
the search deviation will be affected during random search.
58 Huawei Confidential
Cross Validation (1)
l Cross validation: It is a statistical analysis method used to validate the performance of a classifier. The
basic idea is to divide the original dataset into two parts: training set and validation set. Train the
classifier using the training set and test the model using the validation set to check the classifier
performance.
l k-fold cross validation (� − ��):
p Divide the raw data into � groups (generally, evenly divided).
p Use each subset as a validation set, and use the other � − 1 subsets as the training set. A total of � models can
be obtained.
p Use the mean classification accuracy of the final validation sets of � models as the performance indicator of
the � − �� classifier.
59 Huawei Confidential
Cross Validation (2)
Entire dataset
60 Huawei Confidential
Contents
6. Case study
61 Huawei Confidential
Machine Learning Algorithm Overview
Machine learning
GBDT GBDT
KNN
Naive Bayes
62 Huawei Confidential
Linear Regression (1)
l Linear regression: a statistical analysis method to determine the quantitative relationships
between two or more variables through regression analysis in mathematical statistics.
l Linear regression is a type of supervised learning.
63 Huawei Confidential
Linear Regression (2)
l The model function of linear regression is as follows, where � indicates the weight parameter, � indicates the bias,
and � indicates the sample attribute.
hw ( x) wT x b
l The relationship between the value predicted by the model and actual value is as follows, where � indicates the
actual value, and � indicates the error.
y w xb
T
l The error � is influenced by many factors independently. According to the central limit theorem, the error � follows
normal distribution. According to the normal distribution function and maximum likelihood estimation, the loss
function of linear regression is as follows:
1
hw ( x) y
2
J ( w)
2m
l To make the predicted value close to the actual value, we need to minimize the loss value. We can use the
gradient descent method to calculate the weight parameter � when the loss function reaches the minimum, and
then complete model building.
64 Huawei Confidential
Linear Regression Extension - Polynomial Regression
l Polynomial regression is an extension of linear regression. Generally, the complexity of a
dataset exceeds the possibility of fitting by a straight line. That is, obvious underfitting
occurs if the original linear regression model is used. The solution is to use polynomial
regression.
hw ( x) w1x w2 x2 wn xn b
l where, the nth power is a polynomial regression
dimension (degree).
l Polynomial regression belongs to linear regression
as the relationship between its weight parameters
� is still linear while its nonlinearity is reflected in
Comparison between linear regression and
the feature dimension. polynomial regression
65 Huawei Confidential
Linear Regression and Overfitting Prevention
l Regularization terms can be used to reduce overfitting. The value of � cannot be too large
or too small in the sample space. You can add a square sum loss on the target function.
1
w 2
2 2
J ( w) h ( x ) y + w
l Regularization terms (norm):2 m
The regularization term here is called L2-norm. Linear
regression that uses this loss function is also called Ridge regression.
1
hw ( x) y + w 1
2
J ( w)
2m
l Linear regression with absolute loss is called Lasso regression.
66 Huawei Confidential
Logistic Regression (1)
l Logistic regression: The logistic regression model is used to solve classification problems.
The model is defined as follows:
���+�
�(� = 1 �) =
1+���+�
1
�(� = 0 �) =
1+���+�
where � indicates the weight, � indicates the bias, and �� + � is regarded as the linear function of �. Compare the
preceding two probability values. The class with a higher probability value is the class of �.
67 Huawei Confidential
Logistic Regression (2)
l Both the logistic regression model and linear regression model are generalized linear
models. Logistic regression introduces nonlinear factors (the sigmoid function) based on
linear regression and sets thresholds, so it can deal with binary classification problems.
l According to the model function of logistic regression, the loss function of logistic
regression can be estimated as follows by using the maximum likelihood estimation:
1
J ( w) y ln hw ( x ) (1 y ) ln(1 hw ( x ))
m
l where � indicates the weight parameter, � indicates the number of samples, � indicates the
sample, and � indicates the real value. The values of all the weight parameters � can also
be obtained through the gradient descent algorithm.
68 Huawei Confidential
Logistic Regression Extension - Softmax Function (1)
l Logistic regression applies only to binary classification problems. For multi-class
classification problems, use the Softmax function.
Grape?
Male? Orange?
Apple?
Female? Banana?
69 Huawei Confidential
Logistic Regression Extension - Softmax Function (2)
l Softmax regression is a generalization of logistic regression that we can use for K-
class classification.
l The Softmax function is used to map a K-dimensional vector of arbitrary real values
to another K-dimensional vector of real values, where each vector element is in the
interval (0, 1).
l The regression probability function of Softmax is as follows:
wkT x
e
p ( y k | x; w) K
, k 1, 2 , K
e
l 1
wlT x
70 Huawei Confidential
Logistic Regression Extension - Softmax Function (3)
l Softmax assigns a probability to each class in a multi-class problem. These probabilities
must add up to 1.
p Softmax may produce a form belonging to a particular class. Example:
Category Probability
Grape? 0.09
Banana? 0.01
71 Huawei Confidential
Decision Tree
l A decision tree is a tree structure (a binary tree or a non-binary tree). Each non-leaf node represents a test on a
feature attribute. Each branch represents the output of a feature attribute in a certain value range, and each leaf
node stores a category. To use the decision tree, start from the root node, test the feature attributes of the items
to be classified, select the output branches, and use the category stored on the leaf node as the final result.
Root
Short Tall
Short Long
Might be a Might be a
Might be nose nose
squirrel giraffe
a rat
Might be a Might be a
rhinoceros hippo
72 Huawei Confidential
Decision Tree Structure
Root Node
Internal Internal
Node Node
Internal
Leaf Node Leaf Node Node Leaf Node
73 Huawei Confidential
Key Points of Decision Tree Construction
l To create a decision tree, we need to select attributes and determine the tree structure
between feature attributes. The key step of constructing a decision tree is to divide data of
all feature attributes, compare the result sets in terms of 'purity', and select the attribute
with the highest 'purity' as the data point for dataset division.
l The metrics to quantify the 'purity' include the information entropy and GINI Index. The
formula is as follows:
K K
H( X )= - pk log2 ( pk )
k 1
G in i 1
k 1
p k2
l where �� indicates the probability that the sample belongs to class k (there are K classes in
total). A greater difference between purity before segmentation and that after
segmentation indicates a better decision tree.
l Common decision tree algorithms include ID3, C4.5, and CART.
74 Huawei Confidential
Decision Tree Construction Process
l Feature selection: Select a feature from the features of the training data as the split
standard of the current node. (Different standards generate different decision tree
algorithms.)
l Decision tree generation: Generate internal node upside down based on the
selected features and stop until the dataset can no longer be split.
l Pruning: The decision tree may easily become overfitting unless necessary pruning
(including pre-pruning and post-pruning) is performed to reduce the tree size and
optimize its node structure.
75 Huawei Confidential
Decision Tree Example
l The following figure shows a classification when a decision tree is used. The classification result is
impacted by three attributes: Refund, Marital Status, and Taxable Income.
Marital Taxable
Tid Refund Cheat
Status Income
Refund
1 Yes Single 125,000 No
2 No Married 100,000 No
Marital
3 No Single 70,000 No No Status
4 Yes Married 120,000 No
5 No Divorced 95,000 Yes
Taxable
6 No Married 60,000 No Income No
7 Yes Divorced 220,000 No
8 No Single 85,000 Yes No Yes
9 No Married 75,000 No
10 No Single 90,000 Yes
76 Huawei Confidential
SVM
l SVM is a binary classification model whose basic model is a linear classifier defined in the eigenspace
with the largest interval. SVMs also include kernel tricks that make them nonlinear classifiers. The SVM
learning algorithm is the optimal solution to convex quadratic programming.
Projection
77 Huawei Confidential
Linear SVM (1)
l How do we split the red and blue datasets by a straight line?
or
With binary classification Both the left and right methods can be used to
Two-dimensional dataset divide datasets. Which of them is correct?
78 Huawei Confidential
Linear SVM (2)
l Straight lines are used to divide data into different classes. Actually, we can use multiple straight lines
to divide data. The core idea of the SVM is to find a straight line and keep the point close to the
straight line as far as possible from the straight line. This can enable strong generalization capability
of the model. These points are called support vectors.
l In two-dimensional space, we use straight lines for segmentation. In high-dimensional space, we use
hyperplanes for segmentation.
Distance between
support vectors
is as far as possible
79 Huawei Confidential
Nonlinear SVM (1)
l How do we classify a nonlinear separable dataset?
80 Huawei Confidential
Nonlinear SVM (2)
l Kernel functions are used to construct nonlinear SVMs.
l Kernel functions allow algorithms to fit the largest hyperplane in a transformed high-
dimensional feature space.
Common kernel functions
Linear Polynomial
kernel kernel
function function
Gaussian Sigmoid
kernel kernel
function function Input space High-dimensional
feature space
81 Huawei Confidential
KNN Algorithm (1)
l The KNN classification algorithm is a
theoretically mature method and one of the
simplest machine learning algorithms.
According to this method, if the majority of k
samples most similar to one sample (nearest ?
neighbors in the eigenspace) belong to a
specific category, this sample also belongs to
this category.
82 Huawei Confidential
KNN Algorithm (2)
l As the prediction result is determined based on the
number and weights of neighbors in the training set,
the KNN algorithm has a simple logic.
l KNN is a non-parametric method which is usually
used in datasets with irregular decision boundaries.
p The KNN algorithm generally adopts the majority
voting method for classification prediction and the
average value method for regression prediction.
l KNN requires a huge number of computations.
83 Huawei Confidential
KNN Algorithm (3)
l Generally, a larger k value reduces the impact of noise on classification, but obfuscates the boundary
between classes.
p A larger k value means a higher probability of underfitting because the segmentation is too rough. A smaller k
value means a higher probability of overfitting because the segmentation is too refined.
84 Huawei Confidential
Naive Bayes (1)
l Naive Bayes algorithm: a simple multi-class classification algorithm based on the Bayes theorem. It
assumes that features are independent of each other. For a given sample feature �, the probability
that a sample belongs to a category � is:
P X 1 ,, X n | Ck P Ck
P Ck | X1 ,, X n
P X 1 , , X n
p �1 , …, �� are data features, which are usually described by measurement values of m attribute sets.
n For example, the color feature may have three attributes: red, yellow, and blue.
85 Huawei Confidential
Naive Bayes (2)
l Independent assumption of features.
p For example, if a fruit is red, round, and about 10 cm (3.94 in.) in diameter, it can be
considered an apple.
p A Naive Bayes classifier considers that each feature independently contributes to the
probability that the fruit is an apple, regardless of any possible correlation between the
color, roundness, and diameter.
86 Huawei Confidential
Ensemble Learning
l Ensemble learning is a machine learning paradigm in which multiple learners are trained and combined to solve
the same problem. When multiple learners are used, the integrated generalization capability can be much stronger
than that of a single learner.
l If you ask a complex question to thousands of people at random and then summarize their answers, the
summarized answer is better than an expert's answer in most cases. This is the wisdom of the masses.
Training set
Large
Model
model
synthesis
87 Huawei Confidential
Classification of Ensemble Learning
88 Huawei Confidential
Ensemble Methods in Machine Learning (1)
l Random forest = Bagging + CART decision tree
l Ra n d o m f o r e s t s b u i l d mu l t i p l e d e c i s i o n t r e e s a n d me r g e t h e m t o g e t h e r t o m a k e p r e d i c t i o n s m o r e a c c u r a t e
and stable.
p Random forests can be used for classification and regression problems.
Bootstrap sampling Decision tree building Aggregation
prediction result
Data subset 1 Prediction 1
89 Huawei Confidential
Ensemble Methods in Machine Learning (2)
l GBDT is a type of boosting algorithm.
l For an aggregative mode, the sum of the results of all the basic learners equals the predicted value. In
essence, the residual of the error function to the predicted value is fit by the next basic learner. (The
residual is the error between the predicted value and the actual value.)
l During model training, GBDT requires that the sample loss for model prediction be as small as
possible.
Prediction
30 years old 20 years old
Residual
calculation
Prediction
10 years old 9 years old
Residual
calculation
Prediction
1 year old 1 year old
90 Huawei Confidential
Unsupervised Learning - K-means
l K-means clustering aims to partition n observations into k clusters in which each observation belongs
to the cluster with the nearest mean, serving as a prototype of the cluster.
l For the k-means algorithm, specify the final number of clusters (k). Then, divide n data objects into k
clusters. The clusters obtained meet the following conditions: (1) Objects in the same cluster are
highly similar. (2) The similarity of objects in different clusters is small.
x1 x1
K-means clustering
91 Huawei Confidential
Unsupervised Learning - Hierarchical Clustering
l Hierarchical clustering divides a dataset at different layers and forms a tree-like clustering structure.
The dataset division may use a "bottom-up" aggregation policy, or a "top-down" splitting policy. The
hierarchy of clustering is represented in a tree graph. The root is the unique cluster of all samples, and
the leaves are the cluster of only a sample.
Agglomerative hierarchical
Split hierarchical
clustering
clustering
92 Huawei Confidential
Contents
6. Case study
93 Huawei Confidential
Comprehensive Case
l Assume that there is a dataset containing the house areas and prices of 21,613
housing units sold in a city. Based on this data, we can predict the prices of other
houses in the city.
House Area Price
1,180 221,900
2,570 538,000
770 180,000
1,960 604,000
1,680 510,000
5,420 1,225,000 Dataset
1,715 257,500
1,060 291,850
1,160 468,000
1,430 310,000
1,370 400,000
1,810 530,000
… …
94 Huawei Confidential
Problem Analysis
l This case contains a large amount of data, including input x (house area), and output y (price), which is a
continuous value. We can use regression of supervised learning. Draw a scatter chart based on the data and use
linear regression.
l Our goal is to build a model function h(x) that infinitely approximates the function that expresses true distribution
of the dataset.
l Then, use the model to predict unknown price data.
Price
Dataset Learning h(x)
algorithm
Output
y
Label: price
House area
95 Huawei Confidential
Goal of Linear Regression
l Linear regression aims to find a straight line that best fits the dataset.
l Linear regression is a parameter-based model. Here, we need learning parameters �0 and
�1 . When these two parameters are found, the best model appears.
h( x) wo w1 x
Price
Price
t r
Be s m e t e
a
par
96 Huawei Confidential
Loss Function of Linear Regression
l To find the optimal parameter, construct a loss function and find the parameter
values when the loss function becomes the minimum.
1
2
Loss function of linear J ( w) h( x ) y
regression: 2m
Error
Error
Error
Error
Goal:
1
Price
h( x ) y
2
arg min J ( w)
w 2m
• where, m indicates the number of samples,
• h(x) indicates the predicted value, and y
House area indicates the actual value.
97 Huawei Confidential
Gradient Descent Method
l The gradient descent algorithm finds the minimum value of a function through iteration.
l It aims to randomize an initial point on the loss function, and then find the global minimum value of
the loss function based on the negative gradient direction. Such parameter value is the optimal
parameter value.
p Point A: the position of �0 and �1 after random initialization.
�0 and �1 are the required parameters. Cost surface
p A-B connection line: a track formed based on descents in
98 Huawei Confidential
Iteration Example
l The following is an example of a gradient descent iteration. We can see that as red points
on the loss function surface gradually approach a lowest point, fitting of the linear
regression red line with data becomes better and better. At this time, we can get the best
parameters.
99 Huawei Confidential
Model Debugging and Application
l After the model is trained, test it with the test The final model result is as follows:
set to ensure the generalization capability. h( x) 280.62 x 43581
l If overfitting occurs, use Lasso regression or
Ridge regression with regularization terms and
tune the hyperparameters.
Price
l
1. (True or false) Gradient descent iteration is the only method of machine learning algorithms.
( )
A. True
B. False
B. Decision tree
C. KNN
D. K-means