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