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