Machine Learning · Topic 1 of 2

Machine Learning Algorithms, Types and Process

Learn what machine learning is, supervised, unsupervised, semi-supervised and reinforcement learning, the ML process, overfitting and precision and recall.

  • 5 min
  • Includes a diagram
In a nutshell
  • Supervised uses labels (classification and regression); unsupervised has no labels (clustering, dimensionality reduction).
  • Semi-supervised mixes a few labels with many unlabeled samples; reinforcement learning learns from rewards.
  • Process: collect, cleanse, select features, train, evaluate, deploy.

What it is

Machine learning (ML) lets a computer improve at a task by learning from data instead of following hand-written rules. Tom Mitchell's definition: a program learns from experience E for task T and performance measure P if its performance on T, measured by P, improves with E.

A machine learning algorithm takes training data and produces a model, a function that maps new inputs to predictions.

Training dataexperience EML algorithmlearns patternsModellearned functionNew inputPredictionmeasured by P

How it works

Rule-based versus machine learning

ApproachHow the logic is madeGood for
Rule-basedHumans write explicit if-then rulesSimple, stable logic that people can state
Machine learningThe algorithm finds the rules from labeled or unlabeled dataComplex patterns, such as image or speech recognition, that are hard to write as rules

Use ML when the rules are too complex to write by hand, change over time, or must adapt to large amounts of data.

Types of machine learning

TypeTraining dataTypical tasksExamples
Supervised learningLabeled (input and correct output)Classification (discrete label), regression (continuous value)Spam filtering, house price prediction
Unsupervised learningUnlabeledClustering, dimensionality reduction, associationCustomer segmentation with k-means, PCA
Semi-supervised learningA few labeled plus many unlabeled samplesClassification when labeling is costlyLabeling a large image set from a small labeled seed
Reinforcement learningNo labels; rewards from an environmentLearning a policy of actionsGame playing, robot control

In reinforcement learning an agent observes a state, takes an action, and receives a reward. It learns a policy that maximizes total long-term reward.

AgentactionEnvironmentstate and rewardAgent

Classification versus regression

Classification predicts a category, such as "cat" or "dog". Regression predicts a number, such as tomorrow's temperature. Both are supervised.

The machine learning process

StepWhat happens
1. Data collectionGather raw data from databases, logs, sensors or the web
2. Data cleansingFix missing values, remove duplicates, correct errors and outliers
3. Feature extraction and selectionTurn raw data into useful features; keep relevant ones, drop redundant ones
4. Model trainingFit the algorithm to the training set
5. Model evaluationMeasure performance on the validation and test sets
6. Deployment and integrationPut the model into service and monitor it

Data preparation usually takes most of the time in a real project. Poor data gives a poor model, whatever the algorithm.

Feature selection methods: filter methods score features by statistics such as correlation before training; wrapper methods test feature subsets by training the model; embedded methods select features during training, such as L1 regularization driving weights to zero.

Data sets

Data set roles: labeled data is split into a training set, a validation set and a test set; the training set trains candidate models, the validation set compares them to pick the best model, and the test set evaluates that model once for an unbiased performance estimate.

SetRole
Training setFits the model parameters
Validation setTunes hyperparameters and picks the best model
Test setFinal unbiased estimate of performance; used once at the end

Generalization, overfitting and underfitting

Generalization is how well the model performs on new data. The gap between training error and test error shows how well it generalizes.

ProblemTraining errorTest errorCauseFixes
UnderfittingHighHighModel too simple (high bias)More features, more complex model
OverfittingLowHighModel too complex or too little data (high variance)More data, regularization, simpler model, early stopping

Bias is error from wrong assumptions; variance is error from sensitivity to the particular training set. Raising model complexity lowers bias but raises variance, so the goal is a balance.

Evaluation metrics

For classification, the confusion matrix counts true positives (TP), false positives (FP), false negatives (FN) and true negatives (TN).

MetricFormulaMeaning
Accuracy(TP + TN) / allShare of all predictions that are right
PrecisionTP / (TP + FP)Of predicted positives, how many are real
RecallTP / (TP + FN)Of real positives, how many were found
F1 score2 x P x R / (P + R)Harmonic mean of precision and recall

For regression, common metrics are MSE (mean squared error), MAE (mean absolute error) and R squared.

A worked example

A fraud model is tested on 100 transactions: TP = 40, FP = 10, FN = 20, TN = 30. Accuracy = (40 + 30) / 100 = 0.70.

Precision = 40 / (40 + 10) = 0.80 and recall = 40 / (40 + 20) = 0.667. F1 = 2 x 0.80 x 0.667 / (0.80 + 0.667) = 1.067 / 1.467 = 0.727.

Recall is low, so the model misses a third of fraud cases. For fraud, recall usually matters more than precision.

How the exam asks about this

  • "Which type of learning groups customers with no labels into segments?" → Unsupervised learning (clustering).
  • "A model has low training error and high test error. What is the problem?" → Overfitting.
  • "Which data set should be used only once, for the final performance estimate?" → The test set.

What to remember for the exam

  • Supervised uses labels (classification and regression); unsupervised has no labels (clustering, dimensionality reduction).
  • Semi-supervised mixes a few labels with many unlabeled samples; reinforcement learning learns from rewards.
  • Process: collect, cleanse, select features, train, evaluate, deploy.
  • Underfitting means high bias; overfitting means high variance.
  • Precision = TP / (TP + FP), recall = TP / (TP + FN), F1 is their harmonic mean.
Watch out

Do not tune hyperparameters on the test set. Use the validation set or cross-validation; otherwise the test score is no longer an unbiased estimate.

Common mistakes

Calling regression unsupervised. Regression needs a known target value for each training sample, so it is supervised learning.

Confusing precision and recall. Precision divides by predicted positives (TP + FP). Recall divides by actual positives (TP + FN).

Treating high accuracy as a good model. On imbalanced data, a model that always says "not fraud" can score high accuracy while finding no fraud at all.

Check yourself

  • What separates semi-supervised learning from supervised learning?

    Semi-supervised learning trains on a small set of labeled samples plus many unlabeled ones. Supervised learning uses labeled samples only.

  • What is the validation set used for?

    It is used to tune hyperparameters and choose between models, keeping the test set for the final unbiased check.

  • A model has TP = 30, FP = 30, FN = 10. What is its precision?

    Precision = 30 / (30 + 30) = 0.5, so half of its positive predictions are correct.

Topic 1 of 2 in Machine Learning

Test yourself on Machine Learning

The full Huawei HCIA-AI H13-311 question bank has 600 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Huawei. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.