mirror of
https://github.com/Visualize-ML/Book4_Power-of-Matrix.git
synced 2026-02-03 02:24:03 +08:00
189 lines
4.0 KiB
Plaintext
189 lines
4.0 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
|
||
"metadata": {},
|
||
"source": [
|
||
"Chapter 04\n",
|
||
"\n",
|
||
"# diag() 函数\n",
|
||
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "53c03812-0303-4691-9961-f87c9059705c",
|
||
"metadata": {},
|
||
"source": [
|
||
"\n",
|
||
"\n",
|
||
"该代码首先定义了一个 $3 \\times 3$ 的矩阵 $A$:\n",
|
||
"\n",
|
||
"$$\n",
|
||
"A = \\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\\\ 7 & 8 & 9 \\end{bmatrix}\n",
|
||
"$$\n",
|
||
"\n",
|
||
"然后,代码使用 `np.diag` 提取矩阵 $A$ 的对角元素,得到向量 $a = [1, 5, 9]$。接下来,通过向量 $a$ 构建了一个新的对角矩阵 $A_{\\text{diag}}$,其形式为:\n",
|
||
"\n",
|
||
"$$\n",
|
||
"A_{\\text{diag}} = \\begin{bmatrix} 1 & 0 & 0 \\\\ 0 & 5 & 0 \\\\ 0 & 0 & 9 \\end{bmatrix}\n",
|
||
"$$\n",
|
||
"\n",
|
||
"此过程展示了从一个矩阵中提取对角线元素并利用这些元素构造新的对角矩阵的操作。"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "ccb1e0a3-d0ed-4996-9880-b08ba4918993",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 导入所需库"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "e1227d8c-7f70-48b9-af13-a17776aa8152",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import numpy as np # 导入NumPy库,用于数值计算"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "49b8f8e3-3955-48f6-ab9c-de1db5afbe2a",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 定义矩阵"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"id": "c6358766-e0e7-480c-814e-c1ccfbae8e24",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"matrix([[1, 2, 3],\n",
|
||
" [4, 5, 6],\n",
|
||
" [7, 8, 9]])"
|
||
]
|
||
},
|
||
"execution_count": 2,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"A = np.matrix([[1, 2, 3], # 定义矩阵A\n",
|
||
" [4, 5, 6],\n",
|
||
" [7, 8, 9]])\n",
|
||
"A "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "3a682155-7c02-4961-acb9-ab7a3c1b17e8",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 提取对角元素"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"id": "7b2ea9e5-48fa-4b8f-ad4c-6b040817a15d",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([1, 5, 9])"
|
||
]
|
||
},
|
||
"execution_count": 3,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"a = np.diag(A) # 提取矩阵A的对角元素\n",
|
||
"a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "f4e2f66a-370e-410a-ab12-c1f071662dc9",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 构造对角矩阵"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"id": "eea76f59-4071-47b4-b852-740f3df7045c",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[1, 0, 0],\n",
|
||
" [0, 5, 0],\n",
|
||
" [0, 0, 9]])"
|
||
]
|
||
},
|
||
"execution_count": 4,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"A_diag = np.diag(a) # 使用对角元素a构造对角矩阵A_diag\n",
|
||
"A_diag"
|
||
]
|
||
},
|
||
{
|
||
"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
|
||
}
|