Files
2026-07-13 10:35:03 +00:00

157 lines
10 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!-- WEHUB_ZH_README -->
> [!NOTE]
> 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
> [English](./README.en.md) · [原始项目](https://github.com/trekhleb/homemade-machine-learning) · [上游 README](https://github.com/trekhleb/homemade-machine-learning/blob/HEAD/README.md)
> 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
# Homemade Machine Learning
> 🇺🇦 乌克兰 [正遭受攻击](https://war.ukraine.ua/),俄罗斯军队正在发动袭击。平民正在遇害,居民区正在遭受轰炸。
> - 通过以下方式援助乌克兰:
> - [Serhiy Prytula Charity Foundation](https://prytulafoundation.org/en/)
> - [Come Back Alive Charity Foundation](https://savelife.in.ua/en/donate-en/)
> - [National Bank of Ukraine](https://bank.gov.ua/en/news/all/natsionalniy-bank-vidkriv-spetsrahunok-dlya-zboru-koshtiv-na-potrebi-armiyi)
> - 更多信息请访问 [war.ukraine.ua](https://war.ukraine.ua/) 和 [MFA of Ukraine](https://twitter.com/MFA_Ukraine)
<hr/>
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/trekhleb/homemade-machine-learning/master?filepath=notebooks)
> _其他语言版本:_ [_Español_](README.es-ES.md)
> _你可能还会感兴趣:_
> - _[Homemade GPT • JS](https://github.com/trekhleb/homemade-gpt-js)_
> - _[Interactive Machine Learning Experiments](https://github.com/trekhleb/machine-learning-experiments)_
_如需 Octave/MatLab 版本,请查看 [machine-learning-octave](https://github.com/trekhleb/machine-learning-octave) 项目。_
> 本仓库包含用 **Python** 实现的流行机器学习算法示例,并附有背后的数学原理说明。每个算法都配有交互式 **Jupyter Notebook** 演示,让你可以调整训练数据与算法配置,并立即在浏览器中查看结果、图表和预测。多数讲解基于 Andrew Ng 的 [这门优秀的机器学习课程](https://www.coursera.org/learn/machine-learning)。
本仓库的目的 _并非_ 借助第三方库的一行代码来实现机器学习算法,_而是_ 从零开始练习实现这些算法,从而更深入理解每种算法背后的数学原理。因此,所有算法实现都称为“homemade”(自制),并不打算用于生产环境。
## 监督学习(Supervised Learning
在监督学习中,我们有一组训练数据作为输入,以及对应每组训练数据的标签或“正确答案”作为输出。随后我们训练模型(机器学习算法的参数),使其能够正确地将输入映射到输出(做出正确预测)。最终目标是找到这样的模型参数,使其即使面对新的输入样本,也能持续正确地进行 _输入→输出_ 映射(预测)。
### 回归(Regression
在回归问题中,我们进行实数值预测。本质上,我们尝试沿着训练样本拟合一条直线、一个平面或 n 维超平面。
_使用示例:股价预测、销售分析、任意数值之间的依赖关系等。_
#### 🤖 线性回归(Linear Regression
- 📗 [Math | Linear Regression](homemade/linear_regression) - 理论与延伸阅读链接
- ⚙️ [Code | Linear Regression](homemade/linear_regression/linear_regression.py) - 实现示例
- ▶️ [Demo | Univariate Linear Regression](https://nbviewer.jupyter.org/github/trekhleb/homemade-machine-learning/blob/master/notebooks/linear_regression/univariate_linear_regression_demo.ipynb) - 根据 `economy GDP` 预测 `country happiness` 分数
- ▶️ [Demo | Multivariate Linear Regression](https://nbviewer.jupyter.org/github/trekhleb/homemade-machine-learning/blob/master/notebooks/linear_regression/multivariate_linear_regression_demo.ipynb) - 根据 `economy GDP``freedom index` 预测 `country happiness` 分数
- ▶️ [Demo | Non-linear Regression](https://nbviewer.jupyter.org/github/trekhleb/homemade-machine-learning/blob/master/notebooks/linear_regression/non_linear_regression_demo.ipynb) - 使用带有 _多项式__正弦_ 特征的线性回归来预测非线性依赖关系
### 分类(Classification
在分类问题中,我们根据某种特征将输入样本划分到不同类别。
_使用示例:垃圾邮件过滤、语言检测、查找相似文档、手写字符识别等。_
#### 🤖 逻辑回归(Logistic Regression
- 📗 [Math | Logistic Regression](homemade/logistic_regression) - 理论与延伸阅读链接
- ⚙️ [Code | Logistic Regression](homemade/logistic_regression/logistic_regression.py) - 实现示例
- ▶️ [Demo | Logistic Regression (Linear Boundary)](https://nbviewer.jupyter.org/github/trekhleb/homemade-machine-learning/blob/master/notebooks/logistic_regression/logistic_regression_with_linear_boundary_demo.ipynb) - 根据 `petal_length``petal_width` 预测鸢尾花 `class`
- ▶️ [Demo | Logistic Regression (Non-Linear Boundary)](https://nbviewer.jupyter.org/github/trekhleb/homemade-machine-learning/blob/master/notebooks/logistic_regression/logistic_regression_with_non_linear_boundary_demo.ipynb) - 根据 `param_1``param_2` 预测芯片 `validity`
- ▶️ [Demo | Multivariate Logistic Regression | MNIST](https://nbviewer.jupyter.org/github/trekhleb/homemade-machine-learning/blob/master/notebooks/logistic_regression/multivariate_logistic_regression_demo.ipynb) - 从 `28x28` 像素图像中识别手写数字
- ▶️ [Demo | Multivariate Logistic Regression | Fashion MNIST](https://nbviewer.jupyter.org/github/trekhleb/homemade-machine-learning/blob/master/notebooks/logistic_regression/multivariate_logistic_regression_fashion_demo.ipynb) - 从 `28x28` 像素图像中识别服装类型
## 无监督学习(Unsupervised Learning
无监督学习是机器学习的一个分支,它从未标注、未分类或未归类的测试数据中学习。与响应反馈不同,无监督学习会识别数据中的共性,并根据每条新数据中是否存在此类共性来做出反应。
### 聚类(Clustering
在聚类问题中,我们根据未知特征将训练样本划分到不同群组。算法本身会决定使用哪种特征进行划分。
_使用示例:市场细分、社交网络分析、计算集群组织、天文数据分析、图像压缩等。_
#### 🤖 K-means 算法
- 📗 [Math | K-means Algorithm](homemade/k_means) - 理论与延伸阅读链接
- ⚙️ [Code | K-means Algorithm](homemade/k_means/k_means.py) - 实现示例
- ▶️ [Demo | K-means Algorithm](https://nbviewer.jupyter.org/github/trekhleb/homemade-machine-learning/blob/master/notebooks/k_means/k_means_demo.ipynb) - 根据 `petal_length``petal_width` 将鸢尾花划分为不同簇
### 异常检测(Anomaly Detection
异常检测(也称离群点检测)用于识别与数据中大多数样本显著不同、因而值得怀疑的罕见项、事件或观测值。
_使用示例:入侵检测、欺诈检测、系统健康监控、从数据集中移除异常数据等。_
#### 🤖 基于高斯分布的异常检测(Anomaly Detection using Gaussian Distribution
- 📗 [Math | Anomaly Detection using Gaussian Distribution](homemade/anomaly_detection) - 理论与延伸阅读链接
- ⚙️ [Code | Anomaly Detection using Gaussian Distribution](homemade/anomaly_detection/gaussian_anomaly_detection.py) - 实现示例
- ▶️ [Demo | Anomaly Detection](https://nbviewer.jupyter.org/github/trekhleb/homemade-machine-learning/blob/master/notebooks/anomaly_detection/anomaly_detection_gaussian_demo.ipynb) - 在服务器运行参数(如 `latency``threshold`)中查找异常
## 神经网络(Neural Network, NN
神经网络本身并非一种算法,而是一种框架,让多种不同的机器学习算法协同工作,以处理复杂的输入数据。
_使用示例:总体上可替代其他各类算法、图像识别、语音识别、图像处理(应用特定风格)、语言翻译等。_
#### 🤖 多层感知机(Multilayer Perceptron, MLP
- 📗 [Math | Multilayer Perceptron](homemade/neural_network) - 理论与延伸阅读链接
- ⚙️ [Code | Multilayer Perceptron](homemade/neural_network/multilayer_perceptron.py) - 实现示例
- ▶️ [Demo | Multilayer Perceptron | MNIST](https://nbviewer.jupyter.org/github/trekhleb/homemade-machine-learning/blob/master/notebooks/neural_network/multilayer_perceptron_demo.ipynb) - 从 `28x28` 像素图像中识别手写数字
- ▶️ [Demo | Multilayer Perceptron | Fashion MNIST](https://nbviewer.jupyter.org/github/trekhleb/homemade-machine-learning/blob/master/notebooks/neural_network/multilayer_perceptron_fashion_demo.ipynb) - 从 `28x28` 像素图像中识别服装类型
## 机器学习图谱
![机器学习图谱](images/machine-learning-map.png)
以下机器学习主题图谱的来源是[这篇精彩的博客文章](https://vas3k.ru/blog/machine_learning/)
## 前置要求
#### 安装 Python
请确保你的机器上[已安装 Python](https://realpython.com/installing-python/) on your machine.
你可能想使用 [venv](https://docs.python.org/3/library/venv.html) standard Python library
来创建虚拟环境,让 Python、`pip` 以及所有依赖包从本地项目目录安装并运行,
从而避免干扰系统级软件包及其版本。
#### 安装依赖
运行以下命令来安装项目所需的全部依赖:
```bash
pip install -r requirements.txt
```
#### 在本地启动 Jupyter
项目中的所有演示都可以直接在浏览器中运行,无需在本地安装 Jupyter。但若要在本地启动 [Jupyter Notebook](http://jupyter.org/) locally you may do it by running the following command from the root folder of the project:
```bash
jupyter notebook
```
之后,可通过 `http://localhost:8888` 访问 Jupyter Notebook。
#### 远程启动 Jupyter
每个算法章节都包含指向 [Jupyter NBViewer](http://nbviewer.jupyter.org/). 的演示链接。这是一款快速的 Jupyter 笔记本(Jupyter notebooks)在线预览工具,你可以在浏览器中直接查看演示代码、图表和数据,而无需在本地安装任何内容。如果你想_修改_代码并在演示笔记本上_实验_,则需要在 [Binder](https://mybinder.org/). 中启动该笔记本。只需点击 NBViewer 右上角的 _"Execute on Binder"_ 链接即可。
![](./images/binder-button-place.png)
## 数据集
Jupyter Notebook 演示所使用的数据集列表可在 [data 文件夹](data) 中找到。
## 支持本项目
你可以通过 ❤️ [GitHub](https://github.com/sponsors/trekhleb) 或 ❤️ [Patreon](https://www.patreon.com/trekhleb). 支持本项目。
## 作者
- [@trekhleb](https://trekhleb.dev)