知行编程网知行编程网  2022-07-03 11:00 知行编程网 隐藏边栏 |   抢沙发  10 
文章评分 0 次,平均分 0.0

10个例子带你了解机器学习中的线性代数

选自 | machinelearningmastery

作者 | Jason Brownlee

编译 | 机器之心   参与 | 张倩、刘晓坤

本文介绍了 10 个常见机器学习案例,这些案例需要用线性代数才能得到最好的理解。

线性代数是数学的分支学科,涉及矢量、矩阵和线性变换。


它是机器学习的重要基础,从描述算法操作的符号到代码中算法的实现,都属于该学科的研究范围。

虽然线性代数是机器学习领域不可或缺的一部分,但二者的紧密关系往往无法解释,或只能用抽象概念(如向量空间或特定矩阵运算)解释。

阅读这篇文章后,你将会了解到:

  • 如何在处理数据时使用线性代数结构,如表格数据集和图像。

  • 数据准备过程中用到的线性代数概念,例如 one-hot 编码和降维。

  • 深度学习、自然语言处理和推荐系统等子领域中线性代数符号和方法的深入使用。


让我们开始吧。

这 10 个机器学习案例分别是:

1. Dataset and Data Files 数据集和数据文件
2. Images and Photographs 图像和照片
3. One-Hot Encoding one-hot 编码
4. Linear Regression 线性回归
5. Regularization 正则化
6. Principal Component Analysis 主成分分析
7. Singular-Value Decomposition 奇异值分解
8. Latent Semantic Analysis 潜在语义分析
9. Recommender Systems 推荐系统
10. Deep Learning 深度学习

   1. 数据集和数据文件

在机器学习中,你可以在数据集上拟合一个模型。

这是表格式的一组数字,其中每行代表一组观察值,每列代表观测的一个特征。

例如,下面这组数据是鸢尾花数据集的一部分

数据集:http://archive.ics.uci.edu/ml/datasets/Iris

<section style="box-sizing: border-box;padding: 0.5em;font-size: 14px;color: rgb(169, 183, 198);line-height: 18px;border-radius: 0px;background: rgb(40, 43, 46);font-family: Consolas, Inconsolata, Courier, monospace;display: block;overflow: auto;letter-spacing: 0px;margin-left: 8px;margin-right: 8px;overflow-wrap: normal !important;word-break: normal !important;"><span style="font-size: 16px;"><span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">5.1</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">3.5</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">1.4</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">0.2</span>,Iris-setosa<br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"  /><span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">4.9</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">3.0</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">1.4</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">0.2</span>,Iris-setosa<br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"  /><span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">4.7</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">3.2</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">1.3</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">0.2</span>,Iris-setosa<br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"  /><span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">4.6</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">3.1</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">1.5</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">0.2</span>,Iris-setosa<br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"  /><span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">5.0</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">3.6</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">1.4</span>,<span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">0.2</span>,Iris-setosa</span></section>

这些数据实际上是一个矩阵:线性代数中的一个关键数据结构。

接下来,将数据分解为输入数据和输出数据,来拟合一个监督机器学习模型(如测量值和花卉品种),得到矩阵(X)和矢量(y)。矢量是线性代数中的另一个关键数据结构。

每行长度相同,即每行的数据个数相同,因此我们可以说数据是矢量化的。这些行数据可以一次性或成批地提供给模型,并且可以预先配置模型,以得到固定宽度的行数据。

   2. 图像和照片

也许你更习惯于在计算机视觉应用中处理图像或照片。

你使用的每个图像本身都是一个固定宽度和高度的表格结构,每个单元格有用于表示黑白图像的 1 个像素值或表示彩色图像的 3 个像素值。

照片也是线性代数矩阵的一种。

与图像相关的操作,如裁剪、缩放、剪切等,都是使用线性代数的符号和运算来描述的。

   3. one-hot 编码

有时机器学习中要用到分类数据。

可能是用于解决分类问题的类别标签,也可能是分类输入变量。

对分类变量进行编码以使它们更易于使用并通过某些技术进行学习是很常见的。one-hot 编码是一种常见的分类变量编码。

one-hot 编码可以理解为:创建一个表格,用列表示每个类别,用行表示数据集中每个例子。在列中为给定行的分类值添加一个检查或「1」值,并将「0」值添加到所有其他列。

例如,共计 3 行的颜色变量:


<section style="box-sizing: border-box;padding: 0.5em;font-size: 14px;color: rgb(169, 183, 198);line-height: 18px;border-radius: 0px;background: rgb(40, 43, 46);font-family: Consolas, Inconsolata, Courier, monospace;display: block;overflow: auto;letter-spacing: 0px;margin-left: 8px;margin-right: 8px;overflow-wrap: normal !important;word-break: normal !important;"><span style="font-size: 16px;">red<br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"  />green<br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"  />blue<br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"  />...</span></section>

这些变量可能被编码为:

<section style="box-sizing: border-box;padding: 0.5em;font-size: 14px;color: rgb(169, 183, 198);line-height: 18px;border-radius: 0px;background: rgb(40, 43, 46);font-family: Consolas, Inconsolata, Courier, monospace;display: block;overflow: auto;letter-spacing: 0px;margin-left: 8px;margin-right: 8px;overflow-wrap: normal !important;word-break: normal !important;"><span style="font-size: 16px;">red, green, blue<br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"  /><span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, <span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">0</span><br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"  /><span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, <span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">0</span><br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"  /><span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="box-sizing: border-box;color: rgb(174, 135, 250);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">1</span><br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;word-wrap: inherit !important;word-break: inherit !important;"  />...</span></section>

每一行都被编码为一个二进制矢量,一个被赋予「0」或「1」值的矢量。这是一个稀疏表征的例子,线性代数的一个完整子域。

   4. 线性回归

线性回归是一种用于描述变量之间关系的统计学传统方法。

该方法通常在机器学习中用于预测较简单的回归问题的数值。

描述和解决线性回归问题有很多种方法,即找到一组系数,用这些系数与每个输入变量相乘并将结果相加,得出最佳的输出变量预测。

如果您使用过机器学习工具或机器学习库,解决线性回归问题的最常用方法是通过最小二乘优化,这一方法是使用线性回归的矩阵分解方法解决的(例如 LU 分解或奇异值分解)。

即使是线性回归方程的常用总结方法也使用线性代数符号:


<section style="box-sizing: border-box;padding: 0.5em;font-size: 14px;color: rgb(169, 183, 198);line-height: 18px;border-radius: 0px;background: rgb(40, 43, 46);font-family: Consolas, Inconsolata, Courier, monospace;display: block;overflow: auto;letter-spacing: 0px;margin-left: 8px;margin-right: 8px;overflow-wrap: normal !important;word-break: normal !important;"><span style="font-size: 16px;">y = A . b</span></section>

其中,y 是输出变量,A 是数据集,b 是模型系数。

   5. 正则化

在应用机器学习时,我们往往寻求最简单可行的模型来发挥解决问题的最佳技能。

较简单的模型通常更擅长从具体示例泛化到未见过的数据。

在涉及系数的许多方法中,例如回归方法和人工神经网络,较简单的模型通常具有较小的系数值。

一种常用于模型在数据拟合时尽量减小系数值的技术称为正则化,常见的实现包括正则化的 L2 和 L1 形式。

这两种正则化形式实际上是系数矢量的大小或长度的度量,是直接脱胎于名为矢量范数的线性代数方法。

   6. 主成分分析

通常,数据集有许多列,列数可能达到数十、数百、数千或更多。

对具有许多特征的数据进行建模具有一定的挑战性。而且,从包含不相关特征的数据构建的模型通常不如用最相关的数据训练的模型。

我们很难知道数据的哪些特征是相关的,而哪些特征又不相关。

自动减少数据集列数的方法称为降维,其中也许最流行的方法是主成分分析法(简称 PCA)。

该方法在机器学习中,为可视化和模型创建高维数据的投影。

PCA 方法的核心是线性代数的矩阵分解方法,可能会用到特征分解,更广义的实现可以使用奇异值分解(SVD)。

   7. 奇异值分解

另一种流行的降维方法是奇异值分解方法,简称 SVD。

如上所述,正如该方法名称所示,它是源自线性代数领域的矩阵分解方法。

该方法在线性代数中有广泛的用途,可直接应用于特征选择、可视化、降噪等方面。

在机器学习中我们会看到以下两个使用 SVD 的情况。

   8. 潜在语义分析

在用于处理文本数据的机器学习子领域(称为自然语言处理),通常将文档表示为词出现的大矩阵。

例如,矩阵的列可以是词汇表中的已知词,行可以是文本的句子、段落、页面或文档,矩阵中的单元格标记为单词出现的次数或频率。

这是文本的稀疏矩阵表示。矩阵分解方法(如奇异值分解)可以应用于此稀疏矩阵,该分解方法可以提炼出矩阵表示中相关性最强的部分。以这种方式处理的文档比较容易用来比较、查询,并作为监督机器学习模型的基础。

这种形式的数据准备称为潜在语义分析(简称 LSA),也称为潜在语义索引(LSI)。

   9. 推荐系统

涉及产品推荐的预测建模问题被称为推荐系统,这是机器学习的一个子领域。

例如,基于你在亚马逊上的购买记录和与你类似的客户的购买记录向你推荐书籍,或根据你或与你相似的用户在 Netflix 上的观看历史向你推荐电影或电视节目。

推荐系统的开发主要涉及线性代数方法。一个简单的例子就是使用欧式距离或点积之类的距离度量来计算稀疏顾客行为向量之间的相似度。

像奇异值分解这样的矩阵分解方法在推荐系统中被广泛使用,以提取项目和用户数据的有用部分,以备查询、检索及比较。

   10. 深度学习

人工神经网络是一种非线性机器学习算法,它受大脑中信息处理元素的启发,其有效性已经在一系列问题中得到验证,其中最重要的是预测建模。

深度学习是近期出现的、使用最新方法和更快硬件的人工神经网络的复兴,这一方法使得在非常大的数据集上开发和训练更大更深的(更多层)网络成为可能。深度学习方法通常会在机器翻译、照片字幕、语音识别等一系列具有挑战性的领域取得最新成果。

神经网络的执行涉及线性代数数据结构的相乘和相加。如果扩展到多个维度,深度学习方法可以处理向量、矩阵,甚至输入和系数的张量,此处的张量是一个两维以上的矩阵。

线性代数是描述深度学习方法的核心,它通过矩阵表示法来实现深度学习方法,例如 Google 的 TensorFlow Python 库,其名称中包含「tensor」一词。

原文链接:https://machinelearningmastery.com/examples-of-linear-algebra-in-machine-learning/


<section data-brushtype="text" style="white-space: normal;max-width: 100%;letter-spacing: 0.544px;color: rgb(62, 62, 62);font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;widows: 1;word-spacing: 2px;caret-color: rgb(255, 0, 0);background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;color: rgb(0, 0, 0);font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong>完<strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong></span></strong></span></strong></section><pre style="max-width: 100%;letter-spacing: 0.544px;color: rgb(62, 62, 62);font-size: 16px;widows: 1;word-spacing: 2px;caret-color: rgb(255, 0, 0);background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-bottom: 15px;padding-right: 0em;padding-left: 0em;max-width: 100%;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 25.5938px;letter-spacing: 3px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;font-family: 微软雅黑;caret-color: red;box-sizing: border-box !important;overflow-wrap: break-word !important;">为您推荐</span></strong></span></section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;">干货 | 算法工程师超实用技术路线图</section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 14px;">Github上10个超好看的可视化面板,nice!</span></section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 14px;">吴恩达上新:生成对抗网络(GAN)专项课程</span><br  /></section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;">拿到2021年灰飞烟灭的算法岗offer的大佬们是啥样的?<br  /></section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">你一定从未看过如此通俗易懂的YOLO系列解读 (下)</section></section></section></section></section></section></section></section></section>

10个例子带你了解机器学习中的线性代数

本篇文章来源于: 深度学习这件小事

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享