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

174 lines
3.7 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": "0cf4bb7f-f36d-486a-9187-a6f4f7ec18ba",
"metadata": {},
"source": [
"该代码定义了一个标量 $k = 2$ 和一个 $2 \\times 2$ 矩阵 $X$\n",
"\n",
"$$\n",
"X = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码随后计算了标量 $k$ 与矩阵 $X$ 的乘积,得到的结果矩阵 $k \\times X$ 为:\n",
"\n",
"$$\n",
"k \\times X = 2 \\times \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix} = \\begin{bmatrix} 2 & 4 \\\\ 6 & 8 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码展示了使用 `np.dot` 函数和直接标量乘法来实现标量与矩阵乘积的两种方法。"
]
},
{
"cell_type": "markdown",
"id": "9b646351-bc61-4c43-96ed-a265eb25eaac",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "9a74e2ab-fc70-43a4-85f6-3ddeeacfc5ab",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "6bf76475-2000-4adb-82bf-33664ee781da",
"metadata": {},
"source": [
"## 定义标量和矩阵"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e74e9afd-6c86-4ccd-bd9e-4382bfe0c31f",
"metadata": {},
"outputs": [],
"source": [
"k = 2 # 定义标量k"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "db0ca529-60e7-4093-8942-8976a5bc8b29",
"metadata": {},
"outputs": [],
"source": [
"X = [[1, 2], \n",
" [3, 4]] # 定义矩阵X"
]
},
{
"cell_type": "markdown",
"id": "8a306144-ae4b-4b29-9b1a-9a4282ef2f21",
"metadata": {},
"source": [
"## 标量与矩阵的乘法"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "33380485-bdda-4392-8ace-7e11690f111b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 4],\n",
" [6, 8]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k_times_X = np.dot(k, X) # 使用np.dot计算k和X的标量乘法\n",
"k_times_X"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "be39b596-ea34-4108-9447-e3a46d21f91c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[2, 4],\n",
" [6, 8]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k_times_X_2 = k * np.matrix(X) # 使用*操作符计算k和X的标量乘法\n",
"k_times_X_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
}