Files
Iris Series: Visualize Math -- From Arithmetic Basics to Machine Learning 79be5dda7d Add files via upload
2025-02-01 17:06:45 +08:00

199 lines
3.9 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
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.
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 02\n",
"\n",
"# L2范数\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "ab88dee3-1a2e-4357-b6b5-136abf3073c5",
"metadata": {},
"source": [
"此代码定义了两个二维列向量 `a` 和 `b`并计算它们的L2范数。L2范数也称为欧几里得范数用于测量向量的长度。公式如下\n",
"\n",
"$$\n",
"\\| a \\|_2 = \\sqrt{a_1^2 + a_2^2}\n",
"$$ \n",
"\n",
"对于向量 `a` 和 `b`L2范数分别为\n",
"\n",
"$$\n",
"\\| a \\|_2 = \\sqrt{4^2 + 3^2} = 5\n",
"$$\n",
"\n",
"$$\n",
"\\| b \\|_2 = \\sqrt{(-3)^2 + 4^2} = 5\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "2a36cd49-a4e6-4321-a7e8-7f9a34c6aaef",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "01a128eb-3998-4172-bd8a-ec95a23bbc83",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "8ad8b4b0-856b-47a7-adb8-b020180d0c30",
"metadata": {},
"source": [
"## 定义两个列向量"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "212d3a51-f2c6-4785-8c9a-40fa93ae0566",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[4],\n",
" [3]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([[4], [3]]) # 定义向量a值为[4, 3]\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8d5f6350-fddc-4633-946e-f21797b6f5e8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-3],\n",
" [ 4]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.array([[-3], [4]]) # 定义向量b值为[-3, 4]\n",
"b"
]
},
{
"cell_type": "markdown",
"id": "a3ba8142-26a8-43d6-b777-685207499ca9",
"metadata": {},
"source": [
"## 计算L2范数"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1954434f-cb3e-44d3-b985-45b73af8a815",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_L2_norm = np.linalg.norm(a) # 计算向量a的L2范数\n",
"a_L2_norm"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8110a0d6-c941-4825-bf60-bc989324f592",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b_L2_norm = np.linalg.norm(b) # 计算向量b的L2范数\n",
"b_L2_norm"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}