mirror of
https://github.com/Visualize-ML/Book4_Power-of-Matrix.git
synced 2026-02-03 10:33:18 +08:00
203 lines
4.4 KiB
Plaintext
203 lines
4.4 KiB
Plaintext
{
|
||
"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
|
||
}
|