Files
Iris Series: Visualize Math -- From Arithmetic Basics to Machine Learning 79be5dda7d Add files via upload
2025-02-01 17:06:45 +08:00

182 lines
4.3 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"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
}