{ "cells": [ { "cell_type": "markdown", "id": "73bd968b-d970-4a05-94ef-4e7abf990827", "metadata": {}, "source": [ "Chapter 02\n", "\n", "# 向量内积\n", "Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)" ] }, { "cell_type": "markdown", "id": "90d57742-54b5-4967-9de6-2b97f3ff13ce", "metadata": {}, "source": [ "该代码定义了两个二维向量 $a$ 和 $b$,并计算它们的内积。向量 $a$ 和 $b$ 的定义分别为:\n", "\n", "$$\n", "a = \\begin{bmatrix} 4 & 3 \\end{bmatrix}, \\quad b = \\begin{bmatrix} 5 & -2 \\end{bmatrix}\n", "$$\n", "\n", "代码首先通过 `np.inner` 函数计算行向量的内积,其计算公式为:\n", "\n", "$$\n", "a \\cdot b = 4 \\cdot 5 + 3 \\cdot (-2) = 20 - 6 = 14\n", "$$\n", "\n", "接着,代码将 $a$ 和 $b$ 定义为列向量形式 $a_2$ 和 $b_2$,并通过矩阵转置与矩阵乘法计算内积:\n", "\n", "$$\n", "a_2^T \\cdot b_2 = \\begin{bmatrix} 4 & 3 \\end{bmatrix} \\cdot \\begin{bmatrix} 5 \\\\ -2 \\end{bmatrix} = 14\n", "$$\n", "\n", "该过程展示了内积计算的不同实现方式,包括使用 `np.inner` 和转置矩阵乘法。" ] }, { "cell_type": "markdown", "id": "a869bcd3-b5b0-48a6-8473-e1af9efa124c", "metadata": {}, "source": [ "## 导入所需库" ] }, { "cell_type": "code", "execution_count": 1, "id": "52854f8c-f415-452c-8b0f-ba216cedb4a3", "metadata": {}, "outputs": [], "source": [ "import numpy as np # 导入NumPy库,用于数值计算" ] }, { "cell_type": "markdown", "id": "a895dedd-943b-4b5e-a9d8-6c958e1ec18e", "metadata": {}, "source": [ "## 定义两个行向量" ] }, { "cell_type": "code", "execution_count": 2, "id": "13d6cc5a-824f-4537-8d80-5e4a0c6146a7", "metadata": {}, "outputs": [], "source": [ "a = np.array([[4, 3]]) # 定义向量a,值为[4, 3]" ] }, { "cell_type": "code", "execution_count": 3, "id": "89d1799e-2c61-4410-83fd-401db971b8fd", "metadata": {}, "outputs": [], "source": [ "b = np.array([[5, -2]]) # 定义向量b,值为[5, -2]" ] }, { "cell_type": "markdown", "id": "3277e077-e975-4354-b79a-5bc9e77d5666", "metadata": {}, "source": [ "## 计算内积" ] }, { "cell_type": "code", "execution_count": 4, "id": "e4008c0e-94ff-4cb5-8139-432ed0048692", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[14]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_dot_b = np.inner(a, b) # 使用np.inner计算a和b的内积\n", "a_dot_b" ] }, { "cell_type": "markdown", "id": "b6a1c7e0-b5e4-4303-b2a8-19e7a0d72cbb", "metadata": {}, "source": [ "## 定义两个列向量" ] }, { "cell_type": "code", "execution_count": 5, "id": "c30e546b-1f82-43c7-b4d5-163394f406c3", "metadata": {}, "outputs": [], "source": [ "a_2 = np.array([[4], [3]]) # 定义列向量a_2,值为[4, 3]" ] }, { "cell_type": "code", "execution_count": 6, "id": "41ee49f6-1d62-4f0e-895e-671ae32ad301", "metadata": {}, "outputs": [], "source": [ "b_2 = np.array([[5], [-2]]) # 定义列向量b_2,值为[5, -2]" ] }, { "cell_type": "markdown", "id": "d6a74fd1-5de1-486c-98fb-ff21a16d5158", "metadata": {}, "source": [ "## 计算转置后内积" ] }, { "cell_type": "code", "execution_count": 7, "id": "ce1247f9-be4e-40aa-be27-9b9b4d8f9b03", "metadata": {}, "outputs": [], "source": [ "a_dot_b_2 = a_2.T @ b_2 # 使用矩阵乘法计算a_2转置和b_2的内积" ] }, { "cell_type": "code", "execution_count": 8, "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[14]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_dot_b_2" ] }, { "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 }