mirror of
https://github.com/Visualize-ML/Book4_Power-of-Matrix.git
synced 2026-02-04 02:53:47 +08:00
216 lines
4.7 KiB
Plaintext
216 lines
4.7 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
|
||
"metadata": {},
|
||
"source": [
|
||
"Chapter 02\n",
|
||
"\n",
|
||
"# 向量夹角\n",
|
||
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "8bb390c4-26af-44db-bf6a-d0edb2d14b1e",
|
||
"metadata": {},
|
||
"source": [
|
||
"该代码定义了两个二维列向量 $a$ 和 $b$,并计算了它们之间夹角的余弦值、弧度和度数。\n",
|
||
"\n",
|
||
"### 计算公式\n",
|
||
"\n",
|
||
"向量 $a$ 和 $b$ 分别为:\n",
|
||
"\n",
|
||
"$$\n",
|
||
"a = \\begin{bmatrix} 4 \\\\ 3 \\end{bmatrix}, \\quad b = \\begin{bmatrix} 5 \\\\ -2 \\end{bmatrix}\n",
|
||
"$$\n",
|
||
"\n",
|
||
"余弦夹角公式为:\n",
|
||
"\n",
|
||
"$$\n",
|
||
"\\cos \\theta = \\frac{a \\cdot b}{\\|a\\|_2 \\|b\\|_2} = \\frac{a^T b}{\\|a\\|_2 \\|b\\|_2}\n",
|
||
"$$\n",
|
||
"\n",
|
||
"接着,使用反余弦函数计算角度的弧度值:\n",
|
||
"\n",
|
||
"$$\n",
|
||
"\\theta = \\arccos(\\cos \\theta)\n",
|
||
"$$\n",
|
||
"\n",
|
||
"最后将弧度值转换为角度:\n",
|
||
"\n",
|
||
"$$\n",
|
||
"\\theta_{\\text{degree}} = \\theta \\times \\frac{180}{\\pi}\n",
|
||
"$$\n",
|
||
"\n",
|
||
"代码中的运算展示了向量间夹角的计算方法,包括余弦、弧度和度数转换。"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "01c7be45-8810-4f12-aca9-ecc099054269",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 导入所需库"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "53d8013b-3c6e-40da-bc98-e5787ed25f79",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import numpy as np # 导入NumPy库,用于数值计算"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "6c657ddd-c8a1-4ddc-825f-e9e82bcade44",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 定义两个列向量"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"id": "8e262906-50d0-44ee-b31b-4d6569b1b5b9",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"a, b = np.array([[4], [3]]), np.array([[5], [-2]]) # 定义向量a和b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "ff9561ac-02f3-4b67-82e7-be3dddfb4460",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 计算余弦值"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"id": "50fba935-3aa3-41f7-8aed-f29544822233",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[0.51994695]])"
|
||
]
|
||
},
|
||
"execution_count": 3,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"cos_theta = (a.T @ b) / (np.linalg.norm(a, 2) * np.linalg.norm(b, 2)) \n",
|
||
"# 计算向量a和b之间夹角的余弦值\n",
|
||
"cos_theta"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "d65329e6-0d9e-4005-b6b0-a8c46fdb2c04",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 计算夹角的弧度值"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"id": "a94cfe3e-0338-4fb4-99d2-0b2e5e08d198",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[1.02400749]])"
|
||
]
|
||
},
|
||
"execution_count": 4,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"cos_radian = np.arccos(cos_theta) # 计算余弦值对应的弧度\n",
|
||
"cos_radian"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "56bc6d0e-831a-4f2f-ab59-9ff9fd558a41",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 将弧度转换为度数"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"id": "f018e489-013e-4f38-bdb4-291023e46179",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[58.67130713]])"
|
||
]
|
||
},
|
||
"execution_count": 5,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"cos_degree = cos_radian * (180 / np.pi) # 将弧度转换为度数\n",
|
||
"cos_degree"
|
||
]
|
||
},
|
||
{
|
||
"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
|
||
}
|