{ "cells": [ { "cell_type": "markdown", "id": "73bd968b-d970-4a05-94ef-4e7abf990827", "metadata": {}, "source": [ "Chapter 04\n", "\n", "# NumPy数组\n", "Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)" ] }, { "cell_type": "markdown", "id": "e8ae51ee-958b-484b-8ddc-fa7aad6bd2a5", "metadata": {}, "source": [ "这段代码演示了如何在 NumPy 中定义不同维度的数组和矩阵,并输出每种数据结构的形状和类型。首先定义一个 $2 \\times 2$ 的二维矩阵 $A_{\\text{matrix}}$:\n", "\n", "$$\n", "A_{\\text{matrix}} = \\begin{bmatrix} 2 & 4 \\\\ 6 & 8 \\end{bmatrix}\n", "$$\n", "\n", "其类型为 `np.matrix`,形状为 $(2, 2)$。\n", "\n", "接下来,定义了一维数组 $A_{\\text{1d}} = [2, 4]$,其形状为 $(2,)$,类型为 `np.ndarray`。然后定义一个二维数组 $A_{\\text{2d}}$,与 $A_{\\text{matrix}}$ 具有相同的数据内容和形状 $(2, 2)$,类型为 `np.ndarray`:\n", "\n", "$$\n", "A_{\\text{2d}} = \\begin{bmatrix} 2 & 4 \\\\ 6 & 8 \\end{bmatrix}\n", "$$\n", "\n", "最后,通过三个 $2 \\times 2$ 数组 $A1$、$A2$ 和 $A3$ 构建了一个三维数组 $A_{\\text{3d}}$:\n", "\n", "$$\n", "A_{\\text{3d}} = \\begin{bmatrix} \\begin{bmatrix} 2 & 4 \\\\ 6 & 8 \\end{bmatrix}, \\begin{bmatrix} 1 & 3 \\\\ 5 & 7 \\end{bmatrix}, \\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix} \\end{bmatrix}\n", "$$\n", "\n", "三维数组的形状为 $(3, 2, 2)$,类型为 `np.ndarray`。这些定义展示了 NumPy 中矩阵和数组在一维、二维和三维空间的不同表示方式及其对应的形状信息。" ] }, { "cell_type": "markdown", "id": "361fb77e-857e-4843-bfed-2b91753d3173", "metadata": {}, "source": [ "## 导入所需库" ] }, { "cell_type": "code", "execution_count": 1, "id": "99f7bc75-95de-4ac9-8641-2f2d6446d5de", "metadata": {}, "outputs": [], "source": [ "import numpy as np # 导入NumPy库,用于数值计算" ] }, { "cell_type": "markdown", "id": "3dad65f7-ba8f-4402-bbfc-379396517927", "metadata": {}, "source": [ "## 定义二维矩阵" ] }, { "cell_type": "code", "execution_count": 2, "id": "ce746ee1-6041-49c9-964c-4ebc513ceda0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[2, 4],\n", " [6, 8]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_matrix = np.matrix([[2, 4], # 定义矩阵A_matrix\n", " [6, 8]])\n", "A_matrix" ] }, { "cell_type": "code", "execution_count": 3, "id": "5eb2aa30-3537-48b7-9cd8-375b126ece82", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 2)\n" ] } ], "source": [ "print(A_matrix.shape) # 输出矩阵的形状" ] }, { "cell_type": "code", "execution_count": 4, "id": "28d1ad2b-e3e0-47c3-9e45-e47eaebf38e7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(A_matrix)) # 输出矩阵的类型" ] }, { "cell_type": "markdown", "id": "8c571ccc-746f-4da0-9a4e-1b9868033465", "metadata": {}, "source": [ "## 定义一维数组" ] }, { "cell_type": "code", "execution_count": 5, "id": "96121e32-ab22-4c8e-882e-698348bdd152", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 4])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_1d = np.array([2, 4]) # 定义一维数组A_1d\n", "A_1d" ] }, { "cell_type": "code", "execution_count": 6, "id": "730a99d6-1623-4078-8cf2-90b22f28d966", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2,)\n" ] } ], "source": [ "print(A_1d.shape) # 输出数组的形状" ] }, { "cell_type": "code", "execution_count": 7, "id": "56457d26-1eca-4cec-b29b-7896f99ec064", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(A_1d)) # 输出数组的类型" ] }, { "cell_type": "markdown", "id": "367612cb-9b72-405f-a55e-2fb950889246", "metadata": {}, "source": [ "## 定义二维数组" ] }, { "cell_type": "code", "execution_count": 8, "id": "3196ea94-71bd-44a6-8079-812dd3c3757c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 4],\n", " [6, 8]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_2d = np.array([[2, 4], # 定义二维数组A_2d\n", " [6, 8]])\n", "A_2d" ] }, { "cell_type": "code", "execution_count": 9, "id": "b61ac04b-4060-44fb-ad4d-a171536b5411", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 2)\n" ] } ], "source": [ "print(A_2d.shape) # 输出数组的形状" ] }, { "cell_type": "code", "execution_count": 10, "id": "3a011d02-b22c-42a5-97e7-b652bc543ec0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(A_2d)) # 输出数组的类型" ] }, { "cell_type": "markdown", "id": "3bd3a633-c4f6-46cb-a325-fe0d9b28cf92", "metadata": {}, "source": [ "## 定义三维数组" ] }, { "cell_type": "code", "execution_count": 11, "id": "16daa0c8-c544-46ba-be31-eaa40ba2dbe4", "metadata": {}, "outputs": [], "source": [ "A1 = [[2, 4], # 定义第一个二维数组A1\n", " [6, 8]]" ] }, { "cell_type": "code", "execution_count": 12, "id": "fc2603f8-e6e9-4f81-9918-fbbe49bf3127", "metadata": {}, "outputs": [], "source": [ "A2 = [[1, 3], # 定义第二个二维数组A2\n", " [5, 7]]" ] }, { "cell_type": "code", "execution_count": 13, "id": "51c45098-6ebd-44ea-8360-190b70b9ce41", "metadata": {}, "outputs": [], "source": [ "A3 = [[1, 0], # 定义第三个二维数组A3\n", " [0, 1]]" ] }, { "cell_type": "code", "execution_count": 14, "id": "ba92b36b-bd5c-4df2-a829-982b292f0368", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[2, 4],\n", " [6, 8]],\n", "\n", " [[1, 3],\n", " [5, 7]],\n", "\n", " [[1, 0],\n", " [0, 1]]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_3d = np.array([A1, A2, A3]) # 将A1、A2和A3组合为三维数组A_3d\n", "A_3d" ] }, { "cell_type": "code", "execution_count": 15, "id": "8687bb73-81ac-4ad4-9722-1bbbdc29fd28", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(3, 2, 2)\n" ] } ], "source": [ "print(A_3d.shape) # 输出三维数组的形状" ] }, { "cell_type": "code", "execution_count": 16, "id": "6e36a674-efa1-4584-8037-2b117ceece6f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(A_3d)) # 输出数组的类型" ] }, { "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 }