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

253 lines
5.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 02\n",
"\n",
"# 逐项积\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "ce4a4893-3f8c-402f-92c3-79d73b81d380",
"metadata": {},
"source": [
"\n",
"此代码定义了两个三维向量 $a$ 和 $b$,并计算了它们的逐元素积(即对应元素相乘)。代码中既使用了行向量形式也使用了列向量形式的 $a$ 和 $b$ 来演示逐元素乘法的计算。\n",
"\n",
"### 逐元素乘法公式\n",
"对于两个向量 $a = \\begin{bmatrix} a_1 \\\\ a_2 \\\\ a_3 \\end{bmatrix}$ 和 $b = \\begin{bmatrix} b_1 \\\\ b_2 \\\\ b_3 \\end{bmatrix}$,逐元素积定义为:\n",
"\n",
"$$\n",
"a \\odot b = \\begin{bmatrix} a_1 \\cdot b_1 \\\\ a_2 \\cdot b_2 \\\\ a_3 \\cdot b_3 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码中的计算展示了逐元素乘法的两种实现方式:使用 `np.multiply` 函数和直接使用 `*` 操作符。结果得到一个新向量,其中每个元素为对应元素相乘的值。"
]
},
{
"cell_type": "markdown",
"id": "370c070f-d704-40b0-90c9-ef2e2bee22ce",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "b92befcd-0d09-4301-aecc-cb3172ce2cd9",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "25572211-3dd7-487d-ab16-d62682d66f56",
"metadata": {},
"source": [
"## 定义两个行向量"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "04b0208a-df81-48a4-8e85-f2ef347708c1",
"metadata": {},
"outputs": [],
"source": [
"a = np.array([-2, 1, 1]) # 定义向量a"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "557a8b24-f13d-4995-9bc2-c8bc3fa19389",
"metadata": {},
"outputs": [],
"source": [
"b = np.array([1, -2, -1]) # 定义向量b"
]
},
{
"cell_type": "markdown",
"id": "4a95a30f-b3fc-4e2f-b2e7-ccd49cc48ed1",
"metadata": {},
"source": [
"## 计算行向量的逐元素积"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "29eca8ec-ed59-4e60-90bf-8405a4a084e3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-2, -2, -1])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_times_b = np.multiply(a, b) # 计算a和b的逐元素积\n",
"a_times_b"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "91a581bf-ca56-44b2-8d21-8ae4a19ee309",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-2, -2, -1])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_times_b_2 = a * b # 使用*操作符计算a和b的逐元素积\n",
"a_times_b_2"
]
},
{
"cell_type": "markdown",
"id": "65964628-e9f9-4626-9a49-adc10d2f4ecf",
"metadata": {},
"source": [
"## 定义两个列向量"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "42eec7be-ccdc-4a9a-be68-0e074793c2a4",
"metadata": {},
"outputs": [],
"source": [
"a_col = np.array([[-2], [1], [1]]) # 定义列向量a_col"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b8757d8c-776a-400c-be13-0b7cac7ffdac",
"metadata": {},
"outputs": [],
"source": [
"b_col = np.array([[1], [-2], [-1]]) # 定义列向量b_col"
]
},
{
"cell_type": "markdown",
"id": "2269b17d-2fc2-47dc-b7eb-4f4ec992502f",
"metadata": {},
"source": [
"## 计算列向量的逐元素积"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c9f74e22-8432-4c40-bb9b-36f0a2d5e064",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2],\n",
" [-2],\n",
" [-1]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_times_b_col = np.multiply(a_col, b_col) # 计算a_col和b_col的逐元素积\n",
"a_times_b_col"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "7cd5c6c6-b2e3-4027-8eaa-7656594db66b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2],\n",
" [-2],\n",
" [-1]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_times_b_col_2 = a_col * b_col # 使用*操作符计算a_col和b_col的逐元素积\n",
"a_times_b_col_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
}