{ "cells": [ { "cell_type": "markdown", "id": "73bd968b-d970-4a05-94ef-4e7abf990827", "metadata": {}, "source": [ "Chapter 04\n", "\n", "# 矩阵乘法操作\n", "Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)" ] }, { "cell_type": "markdown", "id": "d134e1be-222a-4fec-b5d9-78dfd4fcb3cc", "metadata": {}, "source": [ "该代码演示了 NumPy 中数组和矩阵的乘法操作,分别展示了逐元素乘法和矩阵乘法的不同结果。\n", "\n", "1. **逐元素乘法**:在第一个操作中,$A$ 和 $B$ 都是 `np.array` 类型。按逐元素方式进行乘法时,$A$ 将被广播到与 $B$ 形状相匹配,结果为:\n", "\n", " $$\n", " A \\odot B = \\begin{bmatrix} 1 \\times 5 & 2 \\times 6 \\\\ 1 \\times 8 & 2 \\times 9 \\end{bmatrix} = \\begin{bmatrix} 5 & 12 \\\\ 8 & 18 \\end{bmatrix}\n", " $$\n", "\n", "2. **矩阵乘法**:在第二个操作中,$A$ 为 `np.array` 类型,而 $B$ 为 `np.matrix` 类型。此时 `*` 操作符按矩阵乘法执行,计算结果为:\n", "\n", " $$\n", " A \\times B = \\begin{bmatrix} 1 \\times 5 + 2 \\times 8 & 1 \\times 6 + 2 \\times 9 \\end{bmatrix} = \\begin{bmatrix} 21 & 24 \\end{bmatrix}\n", " $$\n", "\n", "3. **矩阵乘法**:在第三个操作中,$A$ 和 $B$ 都是 `np.matrix` 类型,再次执行矩阵乘法,结果与第二个操作相同:\n", "\n", " $$\n", " A \\times B = \\begin{bmatrix} 21 & 24 \\end{bmatrix}\n", " $$\n", "\n", "此代码展示了在 NumPy 中 `np.array` 和 `np.matrix` 类型的不同行为及 `*` 操作符在逐元素和矩阵乘法间的区别。" ] }, { "cell_type": "markdown", "id": "e694f73c-5da9-404b-930b-1b15c38c149e", "metadata": {}, "source": [ "## 导入所需库" ] }, { "cell_type": "code", "execution_count": 1, "id": "04dd0c40-af12-47f6-8f60-1bba1b377b51", "metadata": {}, "outputs": [], "source": [ "import numpy as np # 导入NumPy库,用于数值计算" ] }, { "cell_type": "markdown", "id": "9dab05d3-2be2-47c9-97a5-e1b779132a1f", "metadata": {}, "source": [ "## 定义数组A和B并进行逐元素乘法" ] }, { "cell_type": "code", "execution_count": 2, "id": "87bb0d5c-67d8-4a10-95c8-c407a32932ad", "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, 2]]) # 定义数组A" ] }, { "cell_type": "code", "execution_count": 3, "id": "8b9a1b34-090e-4432-a096-d45934e8c0aa", "metadata": {}, "outputs": [], "source": [ "B = np.array([[5, 6], # 定义数组B\n", " [8, 9]])" ] }, { "cell_type": "code", "execution_count": 4, "id": "ef365f07-ebfe-432e-9ea3-bc45cecc0335", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 5, 12],\n", " [ 8, 18]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A * B # 打印A和B的逐元素乘法结果" ] }, { "cell_type": "markdown", "id": "00486c88-927e-4c24-b2be-d6f9e8672e0f", "metadata": {}, "source": [ "## 定义数组A和矩阵B并进行矩阵乘法" ] }, { "cell_type": "code", "execution_count": 5, "id": "82d3c241-4790-40c4-aa1f-3a383250ba74", "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, 2]]) # 定义数组A" ] }, { "cell_type": "code", "execution_count": 6, "id": "e6298ff4-cf44-4851-a835-d22563bd4d1d", "metadata": {}, "outputs": [], "source": [ "B = np.matrix([[5, 6], # 定义矩阵B\n", " [8, 9]])" ] }, { "cell_type": "code", "execution_count": 7, "id": "c036b953-8d3d-443e-87eb-1fe46fcd6014", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[21, 24]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A * B # 打印A和B的矩阵乘法结果" ] }, { "cell_type": "markdown", "id": "ab903f71-3eb5-4290-a835-ae58622e69a4", "metadata": {}, "source": [ "## 定义矩阵A和矩阵B并进行矩阵乘法" ] }, { "cell_type": "code", "execution_count": 8, "id": "ab60983c-0ad3-4263-ad82-84986df908d0", "metadata": {}, "outputs": [], "source": [ "A = np.matrix([[1, 2]]) # 定义矩阵A" ] }, { "cell_type": "code", "execution_count": 9, "id": "6ccade11-529f-4962-8eab-139e08a62592", "metadata": {}, "outputs": [], "source": [ "B = np.matrix([[5, 6], # 定义矩阵B\n", " [8, 9]])" ] }, { "cell_type": "code", "execution_count": 10, "id": "e60a1889-600c-4982-9b61-3001d9fbd32f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[21, 24]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A * B # 打印A和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 }