文章目录


前言

本章介绍了Spark MLlib的相关概念、Spark MLlib的体系结构、MLlib的一些经典算法和实现等。 通过本章的介绍,您将对spark MLlib有一个全面的了解。


本章前半部分是对机器学习的概念整理,包括基本分类,在后续更新深度学习笔记的时候还会提到,只是部分内容

一、机器学习的基本概念

(一)什么是机器学习

机器学习是人工智能的一个分支,致力于创建模型和算法,使计算机能够从数据中学习,并根据以往的经验进行改进,而无需为每个特定任务明确编程。

Machine learning, a branch of Artificial Intelligence, is dedicated to creating models and algorithms that enable computers to learn from data and improve based on previous experiences without being explicitly programmed for each specific task.

在这里插入图片描述
总之,机器学习是一个优化数学函数以从数据中学习并做出明智决策的过程。 机器学习模型的成功取决于仔细选择特征,将数据转换为合适的格式,以及为算法最大化定义明确的目标。 有了这些元素,机器学习算法就可以有效地对数据进行分类、预测和聚类,从而得出可操作的见解和决策。

(二)机器学习的基本问题类型

机器学习包含各种各样的问题,每个问题都有不同的目标。

1.分类

(1)概念

分类是一种监督学习,专注于预测类别型目标变量,这些变量代表离散的类别或标签。

Classification is a type of supervised learning that focuses on predicting categorical target variables, which represent discrete classes or labels

这包括将数据点分类到预定义的类中。

(2)例子

例如,将电子邮件分类为垃圾邮件或非垃圾邮件。

Email ContentLinksKeywordsLabel
“Congratulations! …”53Spam
“Meeting at 3 PM…”00Not Spam
“Special offer for you”22Spam
(3)常见算法

Logistic Regression、Decision Trees
RandomForest、K-Nearest Neighbors (KNN)
Support Vector Machines (SVMs).

2.回归

(1)概念

回归是一种监督学习,专注于预测连续的目标变量(表示数值)。与预测离散标签的分类不同,回归算法学习将输入特征映射为连续的数值。

Regression is a type of supervised learning that focuses on predicting continuous target variables, which represent numerical values. Unlike classification, which predicts discrete labels, regression algorithms learn to map input features to continuous numerical values.

(2)例子

这里的目标是预测一个连续的结果,比如房价。

SizeBedroomsBathroomsLocationPrice
2000321$300,000
1500213$200,000
1800322$250,000
(3)常见算法

Linear Regression 线性回归
Decision Tree 决策树
Polynomial Regression 多项式回归
RandomForest 随机数

3.聚类Clustering

(1)概念

聚类是无监督机器学习中的一种技术,在这种技术中,数据点根据它们的相似性或共享特征被分组到聚类中。
Clustering is a technique in unsupervised machine learning where data points are grouped into clusters based on their similarity or shared characteristics.

它旨在将数据组织成有意义的结构,而不需要预定义的标签。
It aims to organize data into meaningful structures without requiring predefined labels.

(2)目的

聚类:目的是根据相似性对数据点进行分组,没有预定义的类别。 特征定义的关键作用

(3)常见算法

K-Means
Hierarchical Clustering

4.Association 关联

(1)概念

关联是一种无监督学习技术,主要用于在大型数据集中发现关系或模式。 关联的目标是识别频繁项集和关联规则,这些规则描述了项或属性如何在数据中同时出现或相互关联。

Association refers to a type of unsupervised learning technique primarily used to find relationships or patterns in large datasets. In association, the goal is to identify frequent itemsets and association rules that describe how items or attributes tend to co-occur or are related to one another within the data.

(2)关联算法

Apriori Algorithm
FP-growth

(3)例子

考虑以下零售店的交易:

Transaction IDItems
1Milk, Bread
2Milk, Diaper, Beer
3Milk, Bread, Diaper
4Milk, Bread, Beer
5Milk, Diaper, Beer

频繁项集(Frequent Itemsets)
{Milk}, {Bread}, {Diaper}, {Beer}, {Milk, Bread}, {Milk, Diaper}, etc.

关联规则(Association Rule)
牛奶→面包:如果顾客买了牛奶,他们很可能会买面包。
Milk → Bread: If a customer buys Milk, they are likely to buy Bread.


二、机器学习的类型

(一)有监督学习

1.概念

监督学习是一种机器学习方法,其中模型在标记的数据集上进行训练,这意味着每个数据点都与相应的输出标签配对。

Supervised learning is a machine learning approach where a model is trained on a labeled dataset, meaning that each data point is paired with a corresponding output label.

这些标记的数据集包含输入特征和相关的输出值,允许算法学习它们之间的关系。

These labeled datasets contain both input features and the associated output values, allowing the algorithm to learn the relationship between them.

2.常见问题类型

classification,regression
在这里插入图片描述

(二)无监督学习

1.概念

无监督学习是一种机器学习技术,算法分析未标记的数据,以揭示数据集中的模式、关系和结构。
Unsupervised learning is a machine learning technique where algorithms analyze unlabeled data to uncover patterns, relationships, and structures within the dataset.

与依赖标记目标输出进行训练的监督学习不同,无监督学习不使用预定义的标签。
Unlike supervised learning, which relies on labeled target outputs for training, unsupervised learning does not use predefined labels.

相反,它试图识别数据中的隐藏模式和内在结构。
Instead, it seeks to identify hidden patterns and intrinsic structures in the data.

2.常见问题类型

clustering、association
在这里插入图片描述


三、Spark MLlib

MLlib是Apache Spark的可扩展机器学习库。
MLlib is Apache Spark’s scalable machine learning library.

它为机器学习(ML)任务提供了一组算法和实用程序,如分类、回归、聚类、协同过滤和降维,以及用于评估和调优模型的工具。
It provides a set of algorithms and utilities for machine learning (ML) tasks such as classification, regression, clustering, collaborative filtering, and dimensionality reduction, along with tools for evaluating and tuning models.

MLlib利用Apache Spark的分布式特性,可以高效地扩展大型数据集。
MLlib is designed to scale efficiently across large datasets, leveraging the distributed nature of Apache Spark.

MLlib提供的API主要分为以下两类:spark提供的主要API,spark中提供的用于构建机器学习工作流的高级API。
The API provided by MLlib is mainly divided into the
following two categories:
1.The main API provided in the spark.mllib package.
2.The high-level API for building machine learning workflows provided in the spark.ml package.


四、Spark MLlib Architecture

(一)介绍

Spark MLlib的体系结构如下所示。 从架构图可以看出,MLlib主要包括三个部分:
1.底层基础:包括Spark的运行库、矩阵库和向量库;
2.算法库:包含广义线性模型、推荐系统、聚类、决策树和评估算法;
3. 实用程序:包括测试数据生成和外部数据读取等功能。

  1. The underlying foundation: including Spark’s runtime library, matrix library and vector library;
  2. Algorithm library: contains generalized linear models, recommendation systems, clustering,decision trees and evaluation algorithms;
  3. Utility program: includes functions such as test data generation and external data reading.
    在这里插入图片描述

(二)数据类型

1.DataFrames

MLlib的主要数据结构。

Primary data structure for MLlib.

包含行和列的基于模式的数据结构。

Schema-based data structure containing rows and columns.

支持特征工程的转换和操作。

Supports transformations and actions for feature engineering.

2.Vectors

(1)概念

表示机器学习模型中的特征或标签。

Represents features or labels in machine learning models.

(2)类型
DenseVector

显式存储所有元素。

SparseVector

有效地存储具有多个0的向量。

举例:(size, [indexes_of_non_zero], [values_of_non_zero])
(5,[2,4],[5.0,3.0])

(三)逐层介绍

1.Top Layer: Evaluation Metrics 评估指标

定义评估模型性能的指标:
Defines the evaluation metrics to assess model performance:

(1)AUC (Area Under Curve) 曲线下面积

用于分类任务,特别是二分类。
Used for classification tasks, especially binary classification.

(2)ROC (Receiver Operating Characteristic)

评价模型区分类别能力的曲线。
A curve to evaluate the model’s ability to distinguish between classes.

(3)Precision-Recall

精确率-召回率:用于评估不平衡分类任务的性能。
Used to evaluate performance in imbalanced classification tasks.

(4)F-measure

平衡准确率和召回率。
Balances precision and recall.

2.Second Layer: Core MLlib Algorithms MLlib核心算法

(1)Classification

分类:逻辑回归、支持向量机。
Logistic Regression, Support Vector Machines.

(2)Regression

回归:线性回归
Linear Regression, Ridge Regression, Lasso.

(3)Clustering

聚类:k均值、高斯混合模型(GMM)
K-means, Gaussian Mixture Models (GMM).

(4)Recommendation

最小二乘法(Alternating Least Squares, ALS)。
Alternating Least Squares (ALS).

3.Third Layer: Optimization Techniques 优化技术

(1)SGD (Stochastic Gradient Descent)

(随机梯度下降):增量更新参数。
Updates parameters incrementally.

(2)ADMM

优化约束问题。
Optimizes constrained problems.

(3)L-BFGS

有限内存优化技术。
Limited-memory optimization technique.

(4)GLMs (Generalized Linear Models)

(广义线性模型):对回归模型进行泛化的框架。
A framework that generalizes regression models.

4. Fourth Layer: Core Computational Components

核心计算组件

(1)RDD (Resilient Distributed Dataset)

支持并行处理的分布式数据结构。
Distributed data structure for parallel processing.

(2)MLlib Matrix Interface

矩阵操作接口。 Interface for matrix operations.

(3)MLlib Vector Interface:

高效处理特征向量。 Efficient handling of feature vectors.

5.Fifth Layer: Spark & Libraries

(1)Spark & Runtime

分布式计算的可扩展性
Distributed computing for scalability.

(2)Breeze

数值处理库
Numerical processing library.

(3)Netlib-java

优化的线性代数例程。
Optimized linear algebra routines.

(4)BLAS/LAPACK

线性代数操作库
Libraries for linear algebra operations.

6.Bottom Layer: Utilities

提供实用程序:

模型评估:使用指标来评估模型。

(1)Model evaluation

模型评估:使用指标来评估模型。
Use metrics to assess models.

(2)Hyperparameter tuning

超参数调优:优化超参数。
Optimizing hyperparameters.

(3)Model saving/loading

模型保存/加载:为未来预测持久化模型。
:Persisting models for future predictions.

(四)Workflow in MLlib

1.Data Preparation: 数据准备

将数据加载到DataFrames中。Load data into DataFrames.

预处理数据(例如,缺失值处理、编码分类变量)。
Preprocess data (e.g., missing value handling, encoding categorical variables).

2.Pipeline Construction:

构建transformer和估计器的管道。
Build a pipeline of Transformers and Estimators.

3.Model Training:模型训练

使用分布式算法在训练数据上训练模型。
Train the model on training data using distributed algorithms.

4.Model Evaluation:模型评价

使用评估指标来测试模型性能。
Use evaluation metrics to test model performance.

5.Model Deployment:

保存模型并用于对新数据的预测。
Save the model and use it for predictions on new data.

(五)Advantages of Spark MLlib

1.Scalability

可扩展性:专为大规模数据设计。
Scalability: Designed for large-scale data.

2.Flexibility

灵活性:支持多种语言。
Flexibility: Supports multiple languages.

3.Integration

集成(Integration):与Spark生态系统无缝对接。
Integration: Works seamlessly with Spark’s ecosystem.

4.Ease of Use

易用性:为机器学习管道提供高级API。
Ease of Use: Provides a high-level API for machine learning pipelines.

这种架构确保了MLlib不仅能高效地进行分布式处理,而且对开发人员和数据科学家也很友好。


总结

在本章中,您学习了:Spark MLlib的相关概念,Spark MLlib的体系结构,MLlib的一些经典算法和实现等:·机器学习基础知识·Spark MLlib简介·Spark MLlib体系结构·MLlib经典算法分析和案例

更多推荐