mirror of
https://github.com/Visualize-ML/Book4_Power-of-Matrix.git
synced 2026-02-03 02:24:03 +08:00
179 lines
3.8 KiB
Plaintext
179 lines
3.8 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": "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
|
||
}
|