Add files via upload

This commit is contained in:
Iris Series: Visualize Math -- From Arithmetic Basics to Machine Learning
2025-02-01 17:06:45 +08:00
committed by GitHub
parent f985536e82
commit 79be5dda7d
47 changed files with 10428 additions and 0 deletions

View File

@@ -0,0 +1,406 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# NumPy数组\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "e8ae51ee-958b-484b-8ddc-fa7aad6bd2a5",
"metadata": {},
"source": [
"这段代码演示了如何在 NumPy 中定义不同维度的数组和矩阵,并输出每种数据结构的形状和类型。首先定义一个 $2 \\times 2$ 的二维矩阵 $A_{\\text{matrix}}$:\n",
"\n",
"$$\n",
"A_{\\text{matrix}} = \\begin{bmatrix} 2 & 4 \\\\ 6 & 8 \\end{bmatrix}\n",
"$$\n",
"\n",
"其类型为 `np.matrix`,形状为 $(2, 2)$。\n",
"\n",
"接下来,定义了一维数组 $A_{\\text{1d}} = [2, 4]$,其形状为 $(2,)$,类型为 `np.ndarray`。然后定义一个二维数组 $A_{\\text{2d}}$,与 $A_{\\text{matrix}}$ 具有相同的数据内容和形状 $(2, 2)$,类型为 `np.ndarray`:\n",
"\n",
"$$\n",
"A_{\\text{2d}} = \\begin{bmatrix} 2 & 4 \\\\ 6 & 8 \\end{bmatrix}\n",
"$$\n",
"\n",
"最后,通过三个 $2 \\times 2$ 数组 $A1$、$A2$ 和 $A3$ 构建了一个三维数组 $A_{\\text{3d}}$:\n",
"\n",
"$$\n",
"A_{\\text{3d}} = \\begin{bmatrix} \\begin{bmatrix} 2 & 4 \\\\ 6 & 8 \\end{bmatrix}, \\begin{bmatrix} 1 & 3 \\\\ 5 & 7 \\end{bmatrix}, \\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix} \\end{bmatrix}\n",
"$$\n",
"\n",
"三维数组的形状为 $(3, 2, 2)$,类型为 `np.ndarray`。这些定义展示了 NumPy 中矩阵和数组在一维、二维和三维空间的不同表示方式及其对应的形状信息。"
]
},
{
"cell_type": "markdown",
"id": "361fb77e-857e-4843-bfed-2b91753d3173",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "99f7bc75-95de-4ac9-8641-2f2d6446d5de",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "3dad65f7-ba8f-4402-bbfc-379396517927",
"metadata": {},
"source": [
"## 定义二维矩阵"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ce746ee1-6041-49c9-964c-4ebc513ceda0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[2, 4],\n",
" [6, 8]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_matrix = np.matrix([[2, 4], # 定义矩阵A_matrix\n",
" [6, 8]])\n",
"A_matrix"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5eb2aa30-3537-48b7-9cd8-375b126ece82",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2, 2)\n"
]
}
],
"source": [
"print(A_matrix.shape) # 输出矩阵的形状"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "28d1ad2b-e3e0-47c3-9e45-e47eaebf38e7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'numpy.matrix'>\n"
]
}
],
"source": [
"print(type(A_matrix)) # 输出矩阵的类型"
]
},
{
"cell_type": "markdown",
"id": "8c571ccc-746f-4da0-9a4e-1b9868033465",
"metadata": {},
"source": [
"## 定义一维数组"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "96121e32-ab22-4c8e-882e-698348bdd152",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 4])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_1d = np.array([2, 4]) # 定义一维数组A_1d\n",
"A_1d"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "730a99d6-1623-4078-8cf2-90b22f28d966",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2,)\n"
]
}
],
"source": [
"print(A_1d.shape) # 输出数组的形状"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "56457d26-1eca-4cec-b29b-7896f99ec064",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'numpy.ndarray'>\n"
]
}
],
"source": [
"print(type(A_1d)) # 输出数组的类型"
]
},
{
"cell_type": "markdown",
"id": "367612cb-9b72-405f-a55e-2fb950889246",
"metadata": {},
"source": [
"## 定义二维数组"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3196ea94-71bd-44a6-8079-812dd3c3757c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 4],\n",
" [6, 8]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_2d = np.array([[2, 4], # 定义二维数组A_2d\n",
" [6, 8]])\n",
"A_2d"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b61ac04b-4060-44fb-ad4d-a171536b5411",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2, 2)\n"
]
}
],
"source": [
"print(A_2d.shape) # 输出数组的形状"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "3a011d02-b22c-42a5-97e7-b652bc543ec0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'numpy.ndarray'>\n"
]
}
],
"source": [
"print(type(A_2d)) # 输出数组的类型"
]
},
{
"cell_type": "markdown",
"id": "3bd3a633-c4f6-46cb-a325-fe0d9b28cf92",
"metadata": {},
"source": [
"## 定义三维数组"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "16daa0c8-c544-46ba-be31-eaa40ba2dbe4",
"metadata": {},
"outputs": [],
"source": [
"A1 = [[2, 4], # 定义第一个二维数组A1\n",
" [6, 8]]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "fc2603f8-e6e9-4f81-9918-fbbe49bf3127",
"metadata": {},
"outputs": [],
"source": [
"A2 = [[1, 3], # 定义第二个二维数组A2\n",
" [5, 7]]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "51c45098-6ebd-44ea-8360-190b70b9ce41",
"metadata": {},
"outputs": [],
"source": [
"A3 = [[1, 0], # 定义第三个二维数组A3\n",
" [0, 1]]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "ba92b36b-bd5c-4df2-a829-982b292f0368",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[2, 4],\n",
" [6, 8]],\n",
"\n",
" [[1, 3],\n",
" [5, 7]],\n",
"\n",
" [[1, 0],\n",
" [0, 1]]])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_3d = np.array([A1, A2, A3]) # 将A1、A2和A3组合为三维数组A_3d\n",
"A_3d"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "8687bb73-81ac-4ad4-9722-1bbbdc29fd28",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 2, 2)\n"
]
}
],
"source": [
"print(A_3d.shape) # 输出三维数组的形状"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "6e36a674-efa1-4584-8037-2b117ceece6f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'numpy.ndarray'>\n"
]
}
],
"source": [
"print(type(A_3d)) # 输出数组的类型"
]
},
{
"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
}

View File

@@ -0,0 +1,188 @@
{
"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
}

View File

@@ -0,0 +1,260 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 矩阵加减法\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "a8042cab-e99c-4d94-a9be-96175baa837e",
"metadata": {},
"source": [
"该代码定义了两个 $2 \\times 2$ 矩阵 $A$ 和 $B$,分别为:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 2 & 6 \\\\ 4 & 8 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码随后计算了矩阵 $A$ 和 $B$ 的加法与减法。矩阵加法 $A + B$ 的结果为:\n",
"\n",
"$$\n",
"A + B = \\begin{bmatrix} 1+2 & 2+6 \\\\ 3+4 & 4+8 \\end{bmatrix} = \\begin{bmatrix} 3 & 8 \\\\ 7 & 12 \\end{bmatrix}\n",
"$$\n",
"\n",
"矩阵减法 $A - B$ 的结果为:\n",
"\n",
"$$\n",
"A - B = \\begin{bmatrix} 1-2 & 2-6 \\\\ 3-4 & 4-8 \\end{bmatrix} = \\begin{bmatrix} -1 & -4 \\\\ -1 & -4 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码展示了使用 `np.add` 和 `np.subtract` 函数以及直接使用操作符完成矩阵加减法的两种实现方式。"
]
},
{
"cell_type": "markdown",
"id": "05fbf711-6eee-49a5-a5a5-a00c9726e60e",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ced7d4cd-a41e-4a61-b8eb-a5221ac81fcf",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "f2afe209-4fad-4614-9697-2c8dcee0ac98",
"metadata": {},
"source": [
"## 定义矩阵"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7c955d5b-6e3b-4ae8-b162-16e5b739b31a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.matrix([[1, 2], \n",
" [3, 4]]) # 定义矩阵A\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7d16a094-2bae-46e1-b98c-35e1a1cbabba",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[2, 6],\n",
" [4, 8]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B = np.matrix([[2, 6], \n",
" [4, 8]]) # 定义矩阵B\n",
"B"
]
},
{
"cell_type": "markdown",
"id": "83b0d359-e7e0-45ed-9241-b5643ec9a631",
"metadata": {},
"source": [
"## 矩阵加法"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "476f434f-f285-47f2-b444-240bf970142d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[ 3, 8],\n",
" [ 7, 12]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_plus_B = np.add(A, B) # 使用np.add函数计算矩阵A和B的和\n",
"A_plus_B"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "dbeb5ab0-70f7-4c11-9ba4-86fceeade833",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[ 3, 8],\n",
" [ 7, 12]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_plus_B_2 = A + B # 使用加法操作符计算矩阵A和B的和\n",
"A_plus_B_2"
]
},
{
"cell_type": "markdown",
"id": "8d40792e-8cd1-4d9a-a372-38bf758f6f6e",
"metadata": {},
"source": [
"## 矩阵减法"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d1535c49-e64c-4503-8655-405263614ae6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[-1, -4],\n",
" [-1, -4]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_minus_B = np.subtract(A, B) # 使用np.subtract函数计算矩阵A和B的差\n",
"A_minus_B"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "32372955-1f18-4c2f-bcb7-fc734e608328",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[-1, -4],\n",
" [-1, -4]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_minus_B_2 = A - B # 使用减法操作符计算矩阵A和B的差\n",
"A_minus_B_2"
]
},
{
"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
}

View File

@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 矩阵标量乘法\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "0cf4bb7f-f36d-486a-9187-a6f4f7ec18ba",
"metadata": {},
"source": [
"该代码定义了一个标量 $k = 2$ 和一个 $2 \\times 2$ 矩阵 $X$:\n",
"\n",
"$$\n",
"X = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码随后计算了标量 $k$ 与矩阵 $X$ 的乘积,得到的结果矩阵 $k \\times X$ 为:\n",
"\n",
"$$\n",
"k \\times X = 2 \\times \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix} = \\begin{bmatrix} 2 & 4 \\\\ 6 & 8 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码展示了使用 `np.dot` 函数和直接标量乘法来实现标量与矩阵乘积的两种方法。"
]
},
{
"cell_type": "markdown",
"id": "9b646351-bc61-4c43-96ed-a265eb25eaac",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "9a74e2ab-fc70-43a4-85f6-3ddeeacfc5ab",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "6bf76475-2000-4adb-82bf-33664ee781da",
"metadata": {},
"source": [
"## 定义标量和矩阵"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e74e9afd-6c86-4ccd-bd9e-4382bfe0c31f",
"metadata": {},
"outputs": [],
"source": [
"k = 2 # 定义标量k"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "db0ca529-60e7-4093-8942-8976a5bc8b29",
"metadata": {},
"outputs": [],
"source": [
"X = [[1, 2], \n",
" [3, 4]] # 定义矩阵X"
]
},
{
"cell_type": "markdown",
"id": "8a306144-ae4b-4b29-9b1a-9a4282ef2f21",
"metadata": {},
"source": [
"## 标量与矩阵的乘法"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "33380485-bdda-4392-8ace-7e11690f111b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 4],\n",
" [6, 8]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k_times_X = np.dot(k, X) # 使用np.dot计算k和X的标量乘法\n",
"k_times_X"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "be39b596-ea34-4108-9447-e3a46d21f91c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[2, 4],\n",
" [6, 8]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k_times_X_2 = k * np.matrix(X) # 使用*操作符计算k和X的标量乘法\n",
"k_times_X_2"
]
},
{
"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
}

View File

@@ -0,0 +1,362 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 广播机制\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "61a42cc1-9923-4983-973d-b03b59d6acc1",
"metadata": {},
"source": [
"这段代码展示了 NumPy 的广播机制,通过定义矩阵、标量、列向量和行向量进行一系列加法操作。代码首先定义了一个 $3 \\times 2$ 的矩阵 $A$、一个标量 $k$、一个列向量 $c$ 和一个行向量 $r$。它们的具体定义如下:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\\\ 5 & 6 \\end{bmatrix}, \\quad k = 2, \\quad c = \\begin{bmatrix} 3 \\\\ 2 \\\\ 1 \\end{bmatrix}, \\quad r = \\begin{bmatrix} 2 & 1 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码使用 NumPy 广播机制,分别计算以下几种加法操作:\n",
"\n",
"1. **矩阵 $A$ 与标量 $k$ 相加**:标量 $k$ 会广播到矩阵 $A$ 的每个元素,即每个元素都加上 $k$ 值。这种情况下,运算结果为:\n",
"\n",
" $$\n",
" A + k = \\begin{bmatrix} 1+2 & 2+2 \\\\ 3+2 & 4+2 \\\\ 5+2 & 6+2 \\end{bmatrix} = \\begin{bmatrix} 3 & 4 \\\\ 5 & 6 \\\\ 7 & 8 \\end{bmatrix}\n",
" $$\n",
"\n",
"2. **矩阵 $A$ 与列向量 $c$ 相加**:列向量 $c$ 被广播到矩阵 $A$ 的每一列,使得 $c$ 的每个元素与 $A$ 相同行的每个元素相加,得到:\n",
"\n",
" $$\n",
" A + c = \\begin{bmatrix} 1+3 & 2+3 \\\\ 3+2 & 4+2 \\\\ 5+1 & 6+1 \\end{bmatrix} = \\begin{bmatrix} 4 & 5 \\\\ 5 & 6 \\\\ 6 & 7 \\end{bmatrix}\n",
" $$\n",
"\n",
"3. **矩阵 $A$ 与行向量 $r$ 相加**:行向量 $r$ 被广播到矩阵 $A$ 的每一行,使得 $r$ 的每个元素与 $A$ 相同行的每个元素相加,结果为:\n",
"\n",
" $$\n",
" A + r = \\begin{bmatrix} 1+2 & 2+1 \\\\ 3+2 & 4+1 \\\\ 5+2 & 6+1 \\end{bmatrix} = \\begin{bmatrix} 3 & 3 \\\\ 5 & 5 \\\\ 7 & 7 \\end{bmatrix}\n",
" $$\n",
"\n",
"4. **列向量 $c$ 与行向量 $r$ 相加**:通过广播,$c$ 和 $r$ 组合成一个 $3 \\times 2$ 的矩阵。每个 $c$ 的元素加到每一行的 $r$ 上,得到:\n",
"\n",
" $$\n",
" c + r = \\begin{bmatrix} 3+2 & 3+1 \\\\ 2+2 & 2+1 \\\\ 1+2 & 1+1 \\end{bmatrix} = \\begin{bmatrix} 5 & 4 \\\\ 4 & 3 \\\\ 3 & 2 \\end{bmatrix}\n",
" $$\n",
"\n",
"该代码展示了 NumPy 广播机制的灵活性,使得矩阵、向量和标量间的加法运算在不同维度下可以直接进行。"
]
},
{
"cell_type": "markdown",
"id": "dbbb9e8f-480a-4b84-be1c-10299faa79c3",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2da2788b-62a1-4402-bcc8-7c4448392cb3",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "371997be-719d-416d-9e23-6c0c9ae9bc96",
"metadata": {},
"source": [
"## 定义矩阵"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7acefee2-ddb0-4ec5-924e-b15c468f3bda",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[1, 2],\n",
" [3, 4],\n",
" [5, 6]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.matrix([[1, 2], # 定义矩阵A\n",
" [3, 4],\n",
" [5, 6]])\n",
"A"
]
},
{
"cell_type": "markdown",
"id": "6a695575-9a76-41e6-bb17-6d77d1dbca49",
"metadata": {},
"source": [
"## 定义标量"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7b44157d-b5c4-450c-b5a9-8c17ec1fb9fc",
"metadata": {},
"outputs": [],
"source": [
"k = 2 # 定义标量k"
]
},
{
"cell_type": "markdown",
"id": "950972dd-22ff-4a81-9793-2172ba4cc752",
"metadata": {},
"source": [
"## 定义列向量"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f59ea6bd-d607-48cf-8a7f-80eb5aac438c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3],\n",
" [2],\n",
" [1]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = np.array([[3], # 定义列向量c\n",
" [2],\n",
" [1]])\n",
"c"
]
},
{
"cell_type": "markdown",
"id": "1a4a65e0-ce82-414a-990b-64713bca3913",
"metadata": {},
"source": [
"## 定义行向量"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c571bb39-d6b7-44ea-b5d7-2b197223eb5f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 1]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r = np.array([[2, 1]]) # 定义行向量r\n",
"r"
]
},
{
"cell_type": "markdown",
"id": "7a7d2094-3824-4efa-a099-40781551d1ff",
"metadata": {},
"source": [
"## 广播原则"
]
},
{
"cell_type": "markdown",
"id": "82e777a0-142f-4a7d-b3e1-2e38af1fe399",
"metadata": {},
"source": [
"## 矩阵A加标量k"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c7c158d0-9576-471c-953f-8767f19d9473",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[3, 4],\n",
" [5, 6],\n",
" [7, 8]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_plus_k = A + k # 矩阵A与标量k相加\n",
"A_plus_k"
]
},
{
"cell_type": "markdown",
"id": "07c8aa0c-d857-4267-a1b7-99c7fc856f6d",
"metadata": {},
"source": [
"## 矩阵A加列向量c"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "919ef4b1-d43d-4c1e-933c-f9c8fde768f1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[4, 5],\n",
" [5, 6],\n",
" [6, 7]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_plus_c = A + c # 矩阵A与列向量c相加\n",
"A_plus_c"
]
},
{
"cell_type": "markdown",
"id": "07bd091a-0642-400a-8fd2-57ea4588875a",
"metadata": {},
"source": [
"## 矩阵A加行向量r"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3a18f414-460e-452e-808e-47fd9d339345",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[3, 3],\n",
" [5, 5],\n",
" [7, 7]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_plus_r = A + r # 矩阵A与行向量r相加\n",
"A_plus_r"
]
},
{
"cell_type": "markdown",
"id": "5fd72975-156a-46c3-abce-170467f5d416",
"metadata": {},
"source": [
"## 列向量c加行向量r"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e5ad12d3-b203-4a4e-90c4-20b693dfa4a2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[5, 4],\n",
" [4, 3],\n",
" [3, 2]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c_plus_r = c + r # 列向量c与行向量r相加\n",
"c_plus_r"
]
},
{
"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
}

View File

@@ -0,0 +1,202 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 矩阵乘法\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "f78179ac-736d-417f-964f-a079c966931d",
"metadata": {},
"source": [
"\n",
"\n",
"该代码定义了两个 $2 \\times 2$ 的矩阵 $A$ 和 $B$,并计算了它们的矩阵乘积。矩阵 $A$ 和 $B$ 分别为:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 2 & 4 \\\\ 1 & 3 \\end{bmatrix}\n",
"$$\n",
"\n",
"矩阵乘积 $A @ B$ 的计算结果为:\n",
"\n",
"$$\n",
"A @ B = \\begin{bmatrix} 1 \\cdot 2 + 2 \\cdot 1 & 1 \\cdot 4 + 2 \\cdot 3 \\\\ 3 \\cdot 2 + 4 \\cdot 1 & 3 \\cdot 4 + 4 \\cdot 3 \\end{bmatrix} = \\begin{bmatrix} 4 & 10 \\\\ 10 & 24 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码中,矩阵乘法操作使用了 `np.matmul` 函数和 `@` 运算符两种方式。这展示了 NumPy 中进行矩阵乘法的两种等效方法。"
]
},
{
"cell_type": "markdown",
"id": "cc4e525d-60d4-4447-a29e-2e1ead9aa96e",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "80885172-f546-4973-add9-496ddb779de5",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "bef4a295-b3fe-493c-9baf-04b2a95d913e",
"metadata": {},
"source": [
"## 定义两个矩阵"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8abe8f8d-6398-4bb6-ac14-a1b1139873fd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([[1, 2], # 定义矩阵A\n",
" [3, 4]])\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dbb8337a-f940-4337-9a98-65692ff3e854",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 4],\n",
" [1, 3]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B = np.array([[2, 4], # 定义矩阵B\n",
" [1, 3]])\n",
"B"
]
},
{
"cell_type": "markdown",
"id": "fd37527e-b082-4899-a2fe-6b04c8db9fea",
"metadata": {},
"source": [
"## 矩阵乘法"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "313d8fec-6e35-4e36-900f-efa4bd5c6adc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 4, 10],\n",
" [10, 24]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_times_B = np.matmul(A, B) # 使用np.matmul计算矩阵A和B的乘积\n",
"A_times_B"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3efab8d4-012b-4f28-b515-d9047d180a89",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 4, 10],\n",
" [10, 24]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_times_B_2 = A @ B # 使用@操作符计算矩阵A和B的乘积\n",
"A_times_B_2"
]
},
{
"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
}

View File

@@ -0,0 +1,249 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 矩阵乘法操作\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "d134e1be-222a-4fec-b5d9-78dfd4fcb3cc",
"metadata": {},
"source": [
"该代码演示了 NumPy 中数组和矩阵的乘法操作,分别展示了逐元素乘法和矩阵乘法的不同结果。\n",
"\n",
"1. **逐元素乘法**:在第一个操作中,$A$ 和 $B$ 都是 `np.array` 类型。按逐元素方式进行乘法时,$A$ 将被广播到与 $B$ 形状相匹配,结果为:\n",
"\n",
" $$\n",
" A \\odot B = \\begin{bmatrix} 1 \\times 5 & 2 \\times 6 \\\\ 1 \\times 8 & 2 \\times 9 \\end{bmatrix} = \\begin{bmatrix} 5 & 12 \\\\ 8 & 18 \\end{bmatrix}\n",
" $$\n",
"\n",
"2. **矩阵乘法**:在第二个操作中,$A$ 为 `np.array` 类型,而 $B$ 为 `np.matrix` 类型。此时 `*` 操作符按矩阵乘法执行,计算结果为:\n",
"\n",
" $$\n",
" A \\times B = \\begin{bmatrix} 1 \\times 5 + 2 \\times 8 & 1 \\times 6 + 2 \\times 9 \\end{bmatrix} = \\begin{bmatrix} 21 & 24 \\end{bmatrix}\n",
" $$\n",
"\n",
"3. **矩阵乘法**:在第三个操作中,$A$ 和 $B$ 都是 `np.matrix` 类型,再次执行矩阵乘法,结果与第二个操作相同:\n",
"\n",
" $$\n",
" A \\times B = \\begin{bmatrix} 21 & 24 \\end{bmatrix}\n",
" $$\n",
"\n",
"此代码展示了在 NumPy 中 `np.array` 和 `np.matrix` 类型的不同行为及 `*` 操作符在逐元素和矩阵乘法间的区别。"
]
},
{
"cell_type": "markdown",
"id": "e694f73c-5da9-404b-930b-1b15c38c149e",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "04dd0c40-af12-47f6-8f60-1bba1b377b51",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "9dab05d3-2be2-47c9-97a5-e1b779132a1f",
"metadata": {},
"source": [
"## 定义数组A和B并进行逐元素乘法"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "87bb0d5c-67d8-4a10-95c8-c407a32932ad",
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1, 2]]) # 定义数组A"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8b9a1b34-090e-4432-a096-d45934e8c0aa",
"metadata": {},
"outputs": [],
"source": [
"B = np.array([[5, 6], # 定义数组B\n",
" [8, 9]])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ef365f07-ebfe-432e-9ea3-bc45cecc0335",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 5, 12],\n",
" [ 8, 18]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A * B # 打印A和B的逐元素乘法结果"
]
},
{
"cell_type": "markdown",
"id": "00486c88-927e-4c24-b2be-d6f9e8672e0f",
"metadata": {},
"source": [
"## 定义数组A和矩阵B并进行矩阵乘法"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "82d3c241-4790-40c4-aa1f-3a383250ba74",
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1, 2]]) # 定义数组A"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e6298ff4-cf44-4851-a835-d22563bd4d1d",
"metadata": {},
"outputs": [],
"source": [
"B = np.matrix([[5, 6], # 定义矩阵B\n",
" [8, 9]])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c036b953-8d3d-443e-87eb-1fe46fcd6014",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[21, 24]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A * B # 打印A和B的矩阵乘法结果"
]
},
{
"cell_type": "markdown",
"id": "ab903f71-3eb5-4290-a835-ae58622e69a4",
"metadata": {},
"source": [
"## 定义矩阵A和矩阵B并进行矩阵乘法"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "ab60983c-0ad3-4263-ad82-84986df908d0",
"metadata": {},
"outputs": [],
"source": [
"A = np.matrix([[1, 2]]) # 定义矩阵A"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "6ccade11-529f-4962-8eab-139e08a62592",
"metadata": {},
"outputs": [],
"source": [
"B = np.matrix([[5, 6], # 定义矩阵B\n",
" [8, 9]])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "e60a1889-600c-4982-9b61-3001d9fbd32f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[21, 24]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A * B # 打印A和B的矩阵乘法结果"
]
},
{
"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
}

View File

@@ -0,0 +1,202 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 矩阵幂\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "82179f04-01ef-4824-a7ec-80be438ed9fd",
"metadata": {},
"source": [
"该代码演示了在 NumPy 中如何计算矩阵的三次幂与逐元素三次方的区别:\n",
"\n",
"1. **矩阵的三次幂**:通过 `matrix_power` 或连续的矩阵乘法 `A @ A @ A` 计算矩阵 $A$ 的三次幂。给定矩阵:\n",
"\n",
" $$\n",
" A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}\n",
" $$\n",
"\n",
" 其三次幂 $A^3$ 计算为:\n",
"\n",
" $$\n",
" A^3 = A @ A @ A = \\begin{bmatrix} 37 & 54 \\\\ 81 & 118 \\end{bmatrix}\n",
" $$\n",
"\n",
"2. **逐元素三次方**:`A ** 3` 计算的是矩阵 $A$ 每个元素的三次方,结果为:\n",
"\n",
" $$\n",
" A \\odot A \\odot A = \\begin{bmatrix} 1^3 & 2^3 \\\\ 3^3 & 4^3 \\end{bmatrix} = \\begin{bmatrix} 1 & 8 \\\\ 27 & 64 \\end{bmatrix}\n",
" $$\n",
"\n",
"代码展示了矩阵幂运算(整体矩阵相乘)和逐元素幂运算(每个元素单独运算)的区别。"
]
},
{
"cell_type": "markdown",
"id": "5a569286-52c7-4d2b-a1c1-57f1b89ba147",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "826fccd1-ad68-46f8-8229-75da1e8ddec8",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from numpy.linalg import matrix_power as pw # 导入矩阵幂函数"
]
},
{
"cell_type": "markdown",
"id": "f0390858-5fb8-4db6-8139-a9c6e98b48ec",
"metadata": {},
"source": [
"## 定义矩阵"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b358aa8f-44ad-4366-8fd5-9890ab983691",
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1., 2.], # 定义矩阵A\n",
" [3., 4.]])"
]
},
{
"cell_type": "markdown",
"id": "1ee41f87-83e0-417e-8429-5650e71574c9",
"metadata": {},
"source": [
"## 计算矩阵的三次幂"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dcff939d-82f9-4098-ada3-81a6bf29e76d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 37., 54.],\n",
" [ 81., 118.]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_3 = pw(A, 3) # 使用matrix_power计算矩阵A的三次幂\n",
"A_3"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "fa89d52c-7726-4d10-b170-ddf0274aa160",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 37., 54.],\n",
" [ 81., 118.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A @ A @ A # 使用矩阵乘法逐步计算A的三次幂"
]
},
{
"cell_type": "markdown",
"id": "3290bcd6-38f4-449f-8dd0-4e51a00a7067",
"metadata": {},
"source": [
"## 元素逐次三次方"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9cf3a84f-d907-4e8e-b3e9-c0637c68bd97",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 8.],\n",
" [27., 64.]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_3_piecewise = A ** 3 # 计算矩阵A的每个元素的三次方\n",
"A_3_piecewise"
]
},
{
"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
}

View File

@@ -0,0 +1,181 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 矩阵乘幂比较\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "f7cb8538-a1e9-4d16-9e49-6772327912c0",
"metadata": {},
"source": [
"\n",
"该代码展示了 `np.matrix` 和 `np.array` 在平方操作中的不同行为。\n",
"\n",
"1. **矩阵平方(`np.matrix` 类型)**:当 $A$ 是 `np.matrix` 类型时,`A**2` 计算的是矩阵乘法,即 $A @ A$。对于矩阵:\n",
"\n",
" $$\n",
" A = \\begin{bmatrix} 1 & 3 \\\\ 2 & 4 \\end{bmatrix}\n",
" $$\n",
"\n",
" 其平方 $A^2$ 计算为:\n",
"\n",
" $$\n",
" A^2 = \\begin{bmatrix} 1 & 3 \\\\ 2 & 4 \\end{bmatrix} @ \\begin{bmatrix} 1 & 3 \\\\ 2 & 4 \\end{bmatrix} = \\begin{bmatrix} 7 & 15 \\\\ 10 & 22 \\end{bmatrix}\n",
" $$\n",
"\n",
"2. **逐元素平方(`np.array` 类型)**:当 $B$ 是 `np.array` 类型时,`B**2` 计算的是逐元素平方,即每个元素单独平方。对于数组:\n",
"\n",
" $$\n",
" B = \\begin{bmatrix} 1 & 3 \\\\ 2 & 4 \\end{bmatrix}\n",
" $$\n",
"\n",
" 逐元素平方的结果为:\n",
"\n",
" $$\n",
" B\\odot B = \\begin{bmatrix} 1^2 & 3^2 \\\\ 2^2 & 4^2 \\end{bmatrix} = \\begin{bmatrix} 1 & 9 \\\\ 4 & 16 \\end{bmatrix}\n",
" $$\n",
"\n",
"此代码展示了 `np.matrix` 和 `np.array` 在平方操作上的关键差异:一个执行矩阵乘法,另一个进行逐元素运算。"
]
},
{
"cell_type": "markdown",
"id": "9734eeec-2b37-40f9-add5-c4ccd409546a",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "4b42a3b3-504c-45eb-8c6b-2830e6a11be8",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "79f50411-80c3-487e-b20d-e42867cea09a",
"metadata": {},
"source": [
"## 定义矩阵A并计算其矩阵平方"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5879a711-cd82-4352-a41f-c9b950865d71",
"metadata": {},
"outputs": [],
"source": [
"A = np.matrix([[1, 3], # 定义为np.matrix类型的矩阵A\n",
" [2, 4]])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "02a0e4c0-e50d-4955-8e72-351468e22d57",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 7 15]\n",
" [10 22]]\n"
]
}
],
"source": [
"print(A**2) # 打印矩阵A的矩阵平方结果"
]
},
{
"cell_type": "markdown",
"id": "8dee9c28-b297-4f4d-9f0b-b0c8880d792e",
"metadata": {},
"source": [
"## 定义数组B并计算其逐元素平方"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7ab6ed46-fca3-46de-aa69-8d1a16b67d24",
"metadata": {},
"outputs": [],
"source": [
"B = np.array([[1, 3], # 定义为np.array类型的数组B\n",
" [2, 4]])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8e1194bb-59fa-40a2-b0a2-e2c85b7cc667",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1 9]\n",
" [ 4 16]]\n"
]
}
],
"source": [
"print(B**2) # 打印数组B的逐元素平方结果"
]
},
{
"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
}

View File

@@ -0,0 +1,178 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 转置\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "34fd7eb5-f8ae-40eb-832c-23f93d07cfeb",
"metadata": {},
"source": [
"该代码定义了一个 $3 \\times 2$ 矩阵 $A$,并计算其转置矩阵。矩阵 $A$ 定义为:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\\\ 5 & 6 \\end{bmatrix}\n",
"$$\n",
"\n",
"矩阵的转置 $A^T$ 将行变为列,结果为一个 $2 \\times 3$ 的矩阵:\n",
"\n",
"$$\n",
"A^T = \\begin{bmatrix} 1 & 3 & 5 \\\\ 2 & 4 & 6 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码使用了两种方法来计算转置矩阵:`A.transpose()` 方法和 `A.T` 属性,二者等效。这段代码展示了如何在 NumPy 中对矩阵进行转置操作。"
]
},
{
"cell_type": "markdown",
"id": "4562dc5f-106d-458a-9c21-058f45dc7750",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "492fc116-d1e3-404a-92fc-92bf126234a5",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "58efb734-2fce-475e-a954-2ff78a1bf71f",
"metadata": {},
"source": [
"## 定义矩阵A"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1581437b-c937-42fc-b1f1-c176a48b0a33",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4],\n",
" [5, 6]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([[1, 2], # 定义矩阵A\n",
" [3, 4],\n",
" [5, 6]])\n",
"A"
]
},
{
"cell_type": "markdown",
"id": "13eeb291-b311-4823-a983-c9749695b2e2",
"metadata": {},
"source": [
"## 计算矩阵的转置"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d34daecc-75e6-4f6f-9af7-d506d9d73eb9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 3, 5],\n",
" [2, 4, 6]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_T = A.transpose() # 使用transpose方法计算A的转置\n",
"A_T"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5ac7a53b-8e59-40ee-a2d8-dafe251f386d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 3, 5],\n",
" [2, 4, 6]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_T_2 = A.T # 使用.T属性计算A的转置\n",
"A_T_2"
]
},
{
"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
}

View File

@@ -0,0 +1,183 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 矩阵逆\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "17664ff7-4c6b-4539-80c1-07f98b6ef2c2",
"metadata": {},
"source": [
"该代码定义了一个 $2 \\times 2$ 矩阵 $A$,并计算其逆矩阵 $A^{-1}$。矩阵 $A$ 为:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}\n",
"$$\n",
"\n",
"通过求逆操作得到 $A$ 的逆矩阵:\n",
"\n",
"$$\n",
"A^{-1} = \\begin{bmatrix} -2 & 1 \\\\ 1.5 & -0.5 \\end{bmatrix}\n",
"$$\n",
"\n",
"接下来,代码计算矩阵 $A$ 与其逆矩阵 $A^{-1}$ 的乘积,理论上应得到单位矩阵:\n",
"\n",
"$$\n",
"A @ A^{-1} = \\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix}\n",
"$$\n",
"\n",
"这段代码展示了如何使用 `inv` 函数计算矩阵的逆,并验证矩阵与其逆矩阵的乘积结果。"
]
},
{
"cell_type": "markdown",
"id": "1feaec35-bb29-42b9-a011-ad9135b0c638",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "5d2a42fe-89e9-4736-b847-a7433aece846",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from numpy.linalg import inv # 导入矩阵求逆函数"
]
},
{
"cell_type": "markdown",
"id": "8ed902be-675e-4438-b550-dbbbf9cf6a66",
"metadata": {},
"source": [
"## 定义矩阵A"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d8339162-83a4-47b2-9d41-2203ab292820",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 2.],\n",
" [3., 4.]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([[1., 2.], # 定义矩阵A\n",
" [3., 4.]])\n",
"A"
]
},
{
"cell_type": "markdown",
"id": "fb5a51d4-624e-48fb-b792-54c433bb0df9",
"metadata": {},
"source": [
"## 计算矩阵的逆"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "dee6461e-cf77-4d73-b4a2-552c0bbe4fa7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2. , 1. ],\n",
" [ 1.5, -0.5]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_inverse = inv(A) # 计算矩阵A的逆\n",
"A_inverse"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ff4c814d-c6e7-416a-b027-0a2a28ef8ba1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1.00000000e+00, 1.11022302e-16],\n",
" [0.00000000e+00, 1.00000000e+00]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_times_A_inv = A @ A_inverse # 计算矩阵A与其逆矩阵的乘积\n",
"A_times_A_inv"
]
},
{
"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
}

View File

@@ -0,0 +1,171 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 矩阵逆\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "c83af894-1177-41f1-9e0c-5df36a5e4755",
"metadata": {},
"source": [
"该代码演示了 `np.matrix` 和 `np.array` 在计算矩阵逆时的区别。矩阵 $A$ 被定义为 `np.matrix` 类型,直接使用 `.I` 属性即可求逆;而矩阵 $B$ 被定义为 `np.array` 类型,不支持 `.I` 属性,因此会报错。\n",
"\n",
"矩阵 $A$ 的定义为:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}\n",
"$$\n",
"\n",
"其逆矩阵为:\n",
"\n",
"$$\n",
"A^{-1} = \\begin{bmatrix} -2 & 1 \\\\ 1.5 & -0.5 \\end{bmatrix}\n",
"$$\n",
"\n",
"`np.matrix` 类型允许直接调用 `.I` 属性计算逆矩阵,而 `np.array` 类型则需使用 `numpy.linalg.inv` 函数求逆。"
]
},
{
"cell_type": "markdown",
"id": "a7654b39-8ea4-480f-9b62-949c3e52bbfe",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e3ba8f7c-9319-46f3-a52c-9eeef1ed5ae5",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "b11501ac-e96b-45ab-9563-63977efe46f7",
"metadata": {},
"source": [
"## 定义矩阵A并计算其逆"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "368c1708-7217-4844-9b32-3e9398c15358",
"metadata": {},
"outputs": [],
"source": [
"A = np.matrix([[1, 2], # 定义为np.matrix类型的矩阵A\n",
" [3, 4]])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "56017ddb-fe6d-4d98-ba7a-963a3b854428",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-2. 1. ]\n",
" [ 1.5 -0.5]]\n"
]
}
],
"source": [
"print(A.I) # 打印矩阵A的逆矩阵"
]
},
{
"cell_type": "markdown",
"id": "fb72cfe6-f62d-42a2-ac12-3a551854f4cb",
"metadata": {},
"source": [
"## 定义数组B并尝试计算其逆"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "abc5a48d-5499-41e4-b826-a9b63acfd8ba",
"metadata": {},
"outputs": [],
"source": [
"B = np.array([[1, 2], # 定义为np.array类型的数组B\n",
" [3, 4]])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "afd919b3-7d56-4543-b1fb-a78bca4bbc26",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'numpy.ndarray' object has no attribute 'I'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[6], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(B\u001b[38;5;241m.\u001b[39mI)\n",
"\u001b[1;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'I'"
]
}
],
"source": [
"print(B.I) # 尝试打印数组B的逆,会报错"
]
},
{
"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
}

View File

@@ -0,0 +1,160 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 迹\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "8cc1d765-91db-4530-81ad-0220895c1e85",
"metadata": {},
"source": [
"该代码定义了一个 $3 \\times 3$ 矩阵 $A$,并计算其迹(trace)。矩阵 $A$ 的定义为:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 1 & -1 & 0 \\\\ 3 & 2 & 4 \\\\ -2 & 0 & 3 \\end{bmatrix}\n",
"$$\n",
"\n",
"矩阵的迹是其主对角线元素之和,公式为:\n",
"\n",
"$$\n",
"\\text{tr}(A) = A_{11} + A_{22} + A_{33}\n",
"$$\n",
"\n",
"因此,矩阵 $A$ 的迹为:\n",
"\n",
"$$\n",
"\\text{tr}(A) = 1 + 2 + 3 = 6\n",
"$$\n",
"\n",
"此代码展示了如何使用 `np.trace` 函数计算矩阵的迹。"
]
},
{
"cell_type": "markdown",
"id": "ba8dee01-3887-4a76-93a9-274b079171a1",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "650779c1-a190-44cc-a778-127b2c189962",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "5ff0a2c5-dcea-4891-84d4-7a72436e6772",
"metadata": {},
"source": [
"## 定义矩阵A"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a1a28546-6d6c-4f65-a94f-c8cb27e5ead5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, -1, 0],\n",
" [ 3, 2, 4],\n",
" [-2, 0, 3]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([[1, -1, 0], # 定义矩阵A\n",
" [3, 2, 4],\n",
" [-2, 0, 3]])\n",
"A"
]
},
{
"cell_type": "markdown",
"id": "7b8f3355-005a-4b5f-be18-41ada700fc7b",
"metadata": {},
"source": [
"## 计算矩阵A的迹"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9e5b87ef-8278-4900-b60c-6e96a60e5eef",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tr_A = np.trace(A) # 计算矩阵A的迹\n",
"tr_A"
]
},
{
"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
}

View File

@@ -0,0 +1,175 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 矩阵逐项积\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "cb498579-9952-45fc-93e8-50479104f8f2",
"metadata": {},
"source": [
"\n",
"该代码定义了两个 $2 \\times 2$ 矩阵 $A$ 和 $B$,并计算它们的 Hadamard 积(逐元素乘积)。矩阵 $A$ 和 $B$ 分别为:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 5 & 6 \\\\ 7 & 8 \\end{bmatrix}\n",
"$$\n",
"\n",
"Hadamard 积(逐元素乘积)的结果为每个对应元素相乘,计算公式为:\n",
"\n",
"$$\n",
"A \\odot B = \\begin{bmatrix} 1 \\cdot 5 & 2 \\cdot 6 \\\\ 3 \\cdot 7 & 4 \\cdot 8 \\end{bmatrix} = \\begin{bmatrix} 5 & 12 \\\\ 21 & 32 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码使用 `np.multiply` 函数和 `*` 操作符两种方式来计算逐元素乘积,二者等效。该操作用于生成对应元素相乘的矩阵。"
]
},
{
"cell_type": "markdown",
"id": "46ade452-7ec0-45e9-959c-1276be6d4724",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c90949ae-66bd-4373-ac80-42341574da7c",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "9b8e04f7-6950-42d1-9981-81ee65208cce",
"metadata": {},
"source": [
"## 定义矩阵A和B"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bab49ebe-1142-4a6b-9662-ccc569aaf590",
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1, 2], # 定义矩阵A\n",
" [3, 4]])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c63f1c20-b2b9-4688-ab7d-800ed8510add",
"metadata": {},
"outputs": [],
"source": [
"B = np.array([[5, 6], # 定义矩阵B\n",
" [7, 8]])"
]
},
{
"cell_type": "markdown",
"id": "861a16bd-2946-438e-8aed-1b974e7d99d6",
"metadata": {},
"source": [
"## 计算Hadamard积(逐元素乘积)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f0947846-bc63-482d-a5ac-a6e891794832",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 5, 12],\n",
" [21, 32]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_times_B_piecewise = np.multiply(A, B) # 使用np.multiply计算A和B的逐元素乘积\n",
"A_times_B_piecewise"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "7e81c46d-d82b-45fa-b22b-5f99b812f2e4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 5, 12],\n",
" [21, 32]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_times_B_piecewise_V2 = A * B # 使用*操作符计算A和B的逐元素乘积\n",
"A_times_B_piecewise_V2"
]
},
{
"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
}

View File

@@ -0,0 +1,133 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 04\n",
"\n",
"# 行列式\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "167f6933-14ac-44fb-83e3-24d28b21a894",
"metadata": {},
"source": [
"该代码定义了一个 $2 \\times 2$ 矩阵 $A$,并计算其行列式。矩阵 $A$ 的定义为:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 4 & 2 \\\\ 1 & 3 \\end{bmatrix}\n",
"$$\n",
"\n",
"行列式计算公式为:\n",
"\n",
"$$\n",
"\\det(A) = A_{11} A_{22} - A_{12} A_{21}\n",
"$$\n",
"\n",
"具体计算得:\n",
"\n",
"$$\n",
"\\det(A) = 4 \\cdot 3 - 2 \\cdot 1 = 12 - 2 = 10\n",
"$$\n",
"\n",
"该代码使用 `np.linalg.det` 函数来计算行列式的值。"
]
},
{
"cell_type": "markdown",
"id": "38e823dc-d626-4fba-8ac6-3fc7d73e9486",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1c1d325e-6981-4885-a9c1-d59bdbe0a194",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "cd7164de-4737-4076-89d3-3f7b8e5b988b",
"metadata": {},
"source": [
"## 定义矩阵A"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "194dc241-67d8-4ca1-a752-e6c7d2237c94",
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[4, 2], # 定义矩阵A\n",
" [1, 3]])"
]
},
{
"cell_type": "markdown",
"id": "8bf849ff-6000-4d22-8e57-8d15debbb248",
"metadata": {},
"source": [
"## 计算矩阵A的行列式"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d910437e-4d72-4c09-8618-a809968077a9",
"metadata": {},
"outputs": [],
"source": [
"det_A = np.linalg.det(A) # 计算矩阵A的行列式"
]
},
{
"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
}

View File

@@ -0,0 +1,109 @@
###############
# Authored by Weisheng Jiang
# Book 4 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2025
###############
## 导入必要的库
import plotly.graph_objects as go # 用于创建绘图对象
import numpy as np # 用于数值计算
from plotly.subplots import make_subplots # 用于创建子图
import streamlit as st # 用于构建交互式Web应用
## 定义一个函数,将NumPy数组格式化为LaTeX矩阵
def bmatrix(a):
"""返回一个LaTeX bmatrix格式的字符串
:param a: 输入的NumPy数组
:return: 返回LaTeX矩阵的字符串表示
"""
if len(a.shape) > 2: # 检查数组是否为二维
raise ValueError('bmatrix最多只能展示二维数组') # 如果不是二维数组,抛出异常
lines = str(a).replace('[', '').replace(']', '').splitlines() # 去掉数组表示中的方括号并分行
rv = [r'\begin{bmatrix}'] # 添加LaTeX矩阵的起始标签
rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines] # 将每行元素用&连接并加上换行符
rv += [r'\end{bmatrix}'] # 添加LaTeX矩阵的结束标签
return '\n'.join(rv) # 返回拼接后的字符串
## 初始化网格范围
n = m = 20 # 定义网格的横纵坐标范围
## 创建子图
fig = make_subplots(rows=1, cols=2, horizontal_spacing=0.035) # 创建一个包含两列的子图
## 绘制垂直线的坐标
xv = [] # 存储垂直线的x坐标
yv = [] # 存储垂直线的y坐标
for k in range(-n, n + 1): # 遍历网格范围
xv.extend([k, k, np.nan]) # 每条垂直线的起始和结束x坐标
yv.extend([-m, m, np.nan]) # 每条垂直线的起始和结束y坐标
## 绘制垂直线
lw = 1 # 定义线条宽度
fig.add_trace(go.Scatter(x=xv, y=yv, mode="lines", line_width=lw, line_color='red'), 1, 1) # 添加垂直线到第一个子图
## 绘制水平线的坐标
xh = [] # 存储水平线的x坐标
yh = [] # 存储水平线的y坐标
for k in range(-m, m + 1): # 遍历网格范围
xh.extend([-m, m, np.nan]) # 每条水平线的起始和结束x坐标
yh.extend([k, k, np.nan]) # 每条水平线的起始和结束y坐标
fig.add_trace(go.Scatter(x=xh, y=yh, mode="lines", line_width=lw, line_color='blue'), 1, 1) # 添加水平线到第一个子图
## 创建交互式侧边栏
with st.sidebar: # 定义侧边栏
st.latex(r'''
A = \begin{bmatrix}
a & b\\
c & d
\end{bmatrix}''') # 显示矩阵A的LaTeX格式
a = st.slider('a', -2.0, 2.0, step=0.1, value=1.0) # 创建滑块选择矩阵A的元素a
b = st.slider('b', -2.0, 2.0, step=0.1, value=0.0) # 创建滑块选择矩阵A的元素b
c = st.slider('c', -2.0, 2.0, step=0.1, value=0.0) # 创建滑块选择矩阵A的元素c
d = st.slider('c', -2.0, 2.0, step=0.1, value=1.0) # 创建滑块选择矩阵A的元素d
## 定义矩阵变换
theta = np.pi / 6 # 定义角度theta
A = np.array([[a, b], [c, d]], dtype=float) # 根据用户输入定义矩阵A
## 应用矩阵变换到垂直线
X = np.array(xv) # 垂直线的x坐标
Y = np.array(yv) # 垂直线的y坐标
Txvyv = A @ np.stack((X, Y)) # 对垂直线应用矩阵A的变换
## 应用矩阵变换到水平线
X = np.array(xh) # 水平线的x坐标
Y = np.array(yh) # 水平线的y坐标
Txhyh = A @ np.stack((X, Y)) # 对水平线应用矩阵A的变换
## 显示矩阵及其计算结果
st.latex(r'A = ' + bmatrix(A)) # 显示矩阵A的LaTeX格式
a1 = A[:, 0].reshape((-1, 1)) # 提取矩阵A的第一列
a2 = A[:, 1].reshape((-1, 1)) # 提取矩阵A的第二列
st.latex(r'''
a_1 = Ae_1 = ''' + bmatrix(A) + 'e_1 = ' + bmatrix(a1)) # 显示第一列向量的计算
st.latex(r'''
a_2 = Ae_2 = ''' + bmatrix(A) + 'e_2 = ' + bmatrix(a2)) # 显示第二列向量的计算
st.latex(r'\begin{vmatrix} A \end{vmatrix} = ' + str(np.linalg.det(A))) # 显示矩阵A的行列式
## 定义一个正方形并应用矩阵变换
square_x = np.array([0, 1, 1, 0]) # 定义正方形的x坐标
square_y = np.array([0, 0, 1, 1]) # 定义正方形的y坐标
square_array = np.stack((square_x, square_y)) # 将正方形的坐标堆叠为二维数组
fig.add_trace(go.Scatter(x=square_x, y=square_y, fill="toself", line_color='orange'), 1, 1) # 在第一个子图中绘制原始正方形
A_times_square_array = A @ square_array # 将正方形应用矩阵A的变换
fig.add_trace(go.Scatter(x=A_times_square_array[0, :], y=A_times_square_array[1, :], fill="toself", line_color='orange'), 1, 2) # 绘制变换后的正方形
fig.add_trace(go.Scatter(x=Txvyv[0], y=Txvyv[1], mode="lines", line_width=lw, line_color='red'), 1, 2) # 绘制变换后的垂直线
fig.add_trace(go.Scatter(x=Txhyh[0], y=Txhyh[1], mode="lines", line_width=lw, line_color='blue'), 1, 2) # 绘制变换后的水平线
## 更新图形布局
fig.update_xaxes(range=[-4, 4]) # 设置x轴范围
fig.update_yaxes(range=[-4, 4]) # 设置y轴范围
fig.update_layout(width=800, height=500, showlegend=False, template="none", plot_bgcolor="white", yaxis2_showgrid=False, xaxis2_showgrid=False) # 设置图形布局
## 在Streamlit中显示图形
st.plotly_chart(fig) # 显示绘图