{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "02_NumPy", "version": "0.3.2", "provenance": [], "collapsed_sections": [], "toc_visible": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "metadata": { "id": "bOChJSNXtC9g", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# NumPy" ] }, { "metadata": { "id": "OLIxEDq6VhvZ", "colab_type": "text" }, "cell_type": "markdown", "source": [ "\n", "\n", "在本课中,我们将学习使用NumPy包进行数值分析的基础知识。\n", "\n", "\n", "\n", "\n" ] }, { "metadata": { "id": "VoMq0eFRvugb", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# NumPy 基础知识" ] }, { "metadata": { "id": "0-dXQiLlTIgz", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "import numpy as np" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "bhaOPJV7WA0m", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "# 使得多次生成的随机数相同\n", "np.random.seed(seed=1234)" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "23tSlin9aWZ8", "colab_type": "code", "outputId": "2ef26177-0cfd-42c8-b9e2-7386e9c5e418", "colab": { "base_uri": "https://localhost:8080/", "height": 101 } }, "cell_type": "code", "source": [ "# 标量(scalars)\n", "x = np.array(6) # scalar\n", "print (\"x: \", x)\n", "print(\"x ndim: \", x.ndim)\n", "print(\"x shape:\", x.shape)\n", "print(\"x size: \", x.size)\n", "print (\"x dtype: \", x.dtype)" ], "execution_count": 3, "outputs": [ { "output_type": "stream", "text": [ "x: 6\n", "x ndim: 0\n", "x shape: ()\n", "x size: 1\n", "x dtype: int64\n" ], "name": "stdout" } ] }, { "metadata": { "id": "ugIZprdIabFF", "colab_type": "code", "outputId": "b3fb3ff6-710c-442d-ca5f-35706a4badc9", "colab": { "base_uri": "https://localhost:8080/", "height": 101 } }, "cell_type": "code", "source": [ "# 一维数组(array)\n", "x = np.array([1.3 , 2.2 , 1.7])\n", "print (\"x: \", x)\n", "print(\"x ndim: \", x.ndim)\n", "print(\"x shape:\", x.shape)\n", "print(\"x size: \", x.size)\n", "print (\"x dtype: \", x.dtype) # notice the float datatype" ], "execution_count": 4, "outputs": [ { "output_type": "stream", "text": [ "x: [1.3 2.2 1.7]\n", "x ndim: 1\n", "x shape: (3,)\n", "x size: 3\n", "x dtype: float64\n" ], "name": "stdout" } ] }, { "metadata": { "id": "SQI-T_4MbE9J", "colab_type": "code", "outputId": "798be5a6-14d4-46f1-a039-97327a70144a", "colab": { "base_uri": "https://localhost:8080/", "height": 152 } }, "cell_type": "code", "source": [ "# 三维数组(矩阵(matrix))\n", "x = np.array([[[1,2,3], [4,5,6], [7,8,9]]])\n", "print (\"x:\\n\", x)\n", "print(\"x ndim: \", x.ndim)\n", "print(\"x shape:\", x.shape)\n", "print(\"x size: \", x.size)\n", "print (\"x dtype: \", x.dtype)" ], "execution_count": 5, "outputs": [ { "output_type": "stream", "text": [ "x:\n", " [[[1 2 3]\n", " [4 5 6]\n", " [7 8 9]]]\n", "x ndim: 3\n", "x shape: (1, 3, 3)\n", "x size: 9\n", "x dtype: int64\n" ], "name": "stdout" } ] }, { "metadata": { "id": "z2Qf8EKZln9j", "colab_type": "code", "outputId": "d8b4e337-a1f3-4b3a-9e85-a0a1b7752ae4", "colab": { "base_uri": "https://localhost:8080/", "height": 220 } }, "cell_type": "code", "source": [ "# 函数\n", "print (\"np.zeros((2,2)):\\n\", np.zeros((2,2)))\n", "print (\"np.ones((2,2)):\\n\", np.ones((2,2)))\n", "print (\"np.eye((2)):\\n\", np.eye((2)))\n", "print (\"np.random.random((2,2)):\\n\", np.random.random((2,2)))" ], "execution_count": 6, "outputs": [ { "output_type": "stream", "text": [ "np.zeros((2,2)):\n", " [[0. 0.]\n", " [0. 0.]]\n", "np.ones((2,2)):\n", " [[1. 1.]\n", " [1. 1.]]\n", "np.eye((2)):\n", " [[1. 0.]\n", " [0. 1.]]\n", "np.random.random((2,2)):\n", " [[0.19151945 0.62210877]\n", " [0.43772774 0.78535858]]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "qVD-MCiCdcV9", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# 索引" ] }, { "metadata": { "id": "vyt36kFOcVDX", "colab_type": "code", "outputId": "c695038f-45c5-4e28-9df6-ead002f40e96", "colab": { "base_uri": "https://localhost:8080/", "height": 51 } }, "cell_type": "code", "source": [ "# 索引(indexing)\n", "x = np.array([1, 2, 3])\n", "print (\"x[0]: \", x[0])\n", "x[0] = 0\n", "print (\"x: \", x)" ], "execution_count": 7, "outputs": [ { "output_type": "stream", "text": [ "x[0]: 1\n", "x: [0 2 3]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "qxHww0didni6", "colab_type": "code", "outputId": "b115a1c3-627e-4259-a6ed-a33f2e65ced4", "colab": { "base_uri": "https://localhost:8080/", "height": 169 } }, "cell_type": "code", "source": [ "# 切片(slicing)\n", "x = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", "print (x)\n", "print (\"x column 1: \", x[:, 1]) \n", "print (\"x row 0: \", x[0, :]) \n", "print (\"x rows 0,1,2 & cols 1,2: \\n\", x[:3, 1:3]) " ], "execution_count": 8, "outputs": [ { "output_type": "stream", "text": [ "[[ 1 2 3 4]\n", " [ 5 6 7 8]\n", " [ 9 10 11 12]]\n", "x column 1: [ 2 6 10]\n", "x row 0: [1 2 3 4]\n", "x rows 0,1,2 & cols 1,2: \n", " [[ 2 3]\n", " [ 6 7]\n", " [10 11]]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "A52pzB9idyDE", "colab_type": "code", "outputId": "f514c2dd-59a2-42ec-d75a-6d26fbd45a6d", "colab": { "base_uri": "https://localhost:8080/", "height": 118 } }, "cell_type": "code", "source": [ "# 整数数组索引\n", "print (x)\n", "rows_to_get = np.arange(len(x))\n", "print (\"rows_to_get: \", rows_to_get)\n", "cols_to_get = np.array([0, 2, 1])\n", "print (\"cols_to_get: \", cols_to_get)\n", "print (\"indexed values: \", x[rows_to_get, cols_to_get])" ], "execution_count": 9, "outputs": [ { "output_type": "stream", "text": [ "[[ 1 2 3 4]\n", " [ 5 6 7 8]\n", " [ 9 10 11 12]]\n", "rows_to_get: [0 1 2]\n", "cols_to_get: [0 2 1]\n", "indexed values: [ 1 7 10]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "_R7O5WsVfDij", "colab_type": "code", "outputId": "4d326bf8-898b-400f-bd98-77f41f4a9a18", "colab": { "base_uri": "https://localhost:8080/", "height": 186 } }, "cell_type": "code", "source": [ "# 布尔数组索引\n", "x = np.array([[1,2], [3, 4], [5, 6]])\n", "print (\"x:\\n\", x)\n", "print (\"x > 2:\\n\", x > 2)\n", "print (\"x[x > 2]:\\n\", x[x > 2])" ], "execution_count": 10, "outputs": [ { "output_type": "stream", "text": [ "x:\n", " [[1 2]\n", " [3 4]\n", " [5 6]]\n", "x > 2:\n", " [[False False]\n", " [ True True]\n", " [ True True]]\n", "x[x > 2]:\n", " [3 4 5 6]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "77RCjrQ8gvYW", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# 数组基础知识" ] }, { "metadata": { "id": "1UJVcNCLfFrV", "colab_type": "code", "outputId": "5428bc3e-51f8-4258-e822-4c62f2407e16", "colab": { "base_uri": "https://localhost:8080/", "height": 169 } }, "cell_type": "code", "source": [ "# 数学基础\n", "x = np.array([[1,2], [3,4]], dtype=np.float64)\n", "y = np.array([[1,2], [3,4]], dtype=np.float64)\n", "print (\"x + y:\\n\", np.add(x, y)) # or x + y\n", "print (\"x - y:\\n\", np.subtract(x, y)) # or x - y\n", "print (\"x * y:\\n\", np.multiply(x, y)) # or x * y" ], "execution_count": 11, "outputs": [ { "output_type": "stream", "text": [ "x + y:\n", " [[2. 4.]\n", " [6. 8.]]\n", "x - y:\n", " [[0. 0.]\n", " [0. 0.]]\n", "x * y:\n", " [[ 1. 4.]\n", " [ 9. 16.]]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "1BV0nSIliMC6", "colab_type": "text" }, "cell_type": "markdown", "source": [ "\n" ] }, { "metadata": { "id": "XyZVF6gXhTWd", "colab_type": "code", "outputId": "be8df5c9-3eea-4b81-9feb-7597edd9bf90", "colab": { "base_uri": "https://localhost:8080/", "height": 51 } }, "cell_type": "code", "source": [ "# 点积\n", "a = np.array([[1,2,3], [4,5,6]], dtype=np.float64) # 我们可以指定dtype\n", "b = np.array([[7,8], [9,10], [11, 12]], dtype=np.float64)\n", "print (a.dot(b))" ], "execution_count": 12, "outputs": [ { "output_type": "stream", "text": [ "[[ 58. 64.]\n", " [139. 154.]]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "7pB-H-7phsku", "colab_type": "code", "outputId": "3b6315de-1432-4623-8569-34ed1b0be37c", "colab": { "base_uri": "https://localhost:8080/", "height": 101 } }, "cell_type": "code", "source": [ "# 跨维度求和\n", "x = np.array([[1,2],[3,4]])\n", "print (x)\n", "print (\"sum all: \", np.sum(x)) # 将所有元素相加\n", "print (\"sum by col: \", np.sum(x, axis=0)) # 逐列将元素相加\n", "print (\"sum by row: \", np.sum(x, axis=1)) # 逐行将元素相加" ], "execution_count": 13, "outputs": [ { "output_type": "stream", "text": [ "[[1 2]\n", " [3 4]]\n", "sum all: 10\n", "sum by col: [4 6]\n", "sum by row: [3 7]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "pLDG49LrijgA", "colab_type": "code", "outputId": "01ecf82a-b31a-4771-f526-3dd903d01f98", "colab": { "base_uri": "https://localhost:8080/", "height": 118 } }, "cell_type": "code", "source": [ "# 转置\n", "print (\"x:\\n\", x)\n", "print (\"x.T:\\n\", x.T)" ], "execution_count": 14, "outputs": [ { "output_type": "stream", "text": [ "x:\n", " [[1 2]\n", " [3 4]]\n", "x.T:\n", " [[1 3]\n", " [2 4]]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "KdPKVKtwkWnw", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# 数组高级知识" ] }, { "metadata": { "id": "U_j2fCcjkEyo", "colab_type": "code", "outputId": "e728bff5-aef3-48f8-ea52-2b8d1d2caf01", "colab": { "base_uri": "https://localhost:8080/", "height": 118 } }, "cell_type": "code", "source": [ "# np.tile:重复维度\n", "x = np.array([[1,2], [3,4]])\n", "y = np.array([5, 6])\n", "addent = np.tile(y, (len(x), 1))\n", "print (\"addent: \\n\", addent)\n", "z = x + addent\n", "print (\"z:\\n\", z)" ], "execution_count": 15, "outputs": [ { "output_type": "stream", "text": [ "addent: \n", " [[5 6]\n", " [5 6]]\n", "z:\n", " [[ 6 8]\n", " [ 8 10]]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "1NsoFVo0mfQ4", "colab_type": "code", "outputId": "82694416-b155-45ca-aa27-9997b914d482", "colab": { "base_uri": "https://localhost:8080/", "height": 68 } }, "cell_type": "code", "source": [ "# 广播(broadcasting)\n", "x = np.array([[1,2], [3,4]])\n", "y = np.array([5, 6])\n", "z = x + y\n", "print (\"z:\\n\", z)" ], "execution_count": 16, "outputs": [ { "output_type": "stream", "text": [ "z:\n", " [[ 6 8]\n", " [ 8 10]]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "RdEHrnMTnO6k", "colab_type": "code", "outputId": "84ac6a75-835a-46d5-d313-78d1d92469f0", "colab": { "base_uri": "https://localhost:8080/", "height": 152 } }, "cell_type": "code", "source": [ "# 改变维度\n", "x = np.array([[1,2], [3,4], [5,6]])\n", "print (x)\n", "print (\"x.shape: \", x.shape)\n", "y = np.reshape(x, (2, 3))\n", "print (\"y.shape: \", y.shape)\n", "print (\"y: \\n\", y)" ], "execution_count": 17, "outputs": [ { "output_type": "stream", "text": [ "[[1 2]\n", " [3 4]\n", " [5 6]]\n", "x.shape: (3, 2)\n", "y.shape: (2, 3)\n", "y: \n", " [[1 2 3]\n", " [4 5 6]]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "tE1BmoJuns70", "colab_type": "code", "outputId": "4d96b9fd-f580-4738-8dd2-33b2486f7c51", "colab": { "base_uri": "https://localhost:8080/", "height": 101 } }, "cell_type": "code", "source": [ "# 删除维度\n", "x = np.array([[[1,2,1]],[[2,2,3]]])\n", "print (\"x.shape: \", x.shape)\n", "y = np.squeeze(x, 1) # 删除维度1\n", "print (\"y.shape: \", y.shape) \n", "print (\"y: \\n\", y)" ], "execution_count": 18, "outputs": [ { "output_type": "stream", "text": [ "x.shape: (2, 1, 3)\n", "y.shape: (2, 3)\n", "y: \n", " [[1 2 1]\n", " [2 2 3]]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "LNYJRMF4qvXN", "colab_type": "code", "outputId": "230bf71a-072c-4f7b-b59b-a22303a23db3", "colab": { "base_uri": "https://localhost:8080/", "height": 118 } }, "cell_type": "code", "source": [ "# 添加维度\n", "x = np.array([[1,2,1],[2,2,3]])\n", "print (\"x.shape: \", x.shape)\n", "y = np.expand_dims(x, 1) # 扩展维度1\n", "print (\"y.shape: \", y.shape) \n", "print (\"y: \\n\", y)" ], "execution_count": 19, "outputs": [ { "output_type": "stream", "text": [ "x.shape: (2, 3)\n", "y.shape: (2, 1, 3)\n", "y: \n", " [[[1 2 1]]\n", "\n", " [[2 2 3]]]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "XthM4y7SotAH", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# 其它资源" ] }, { "metadata": { "id": "3KmESFstrbFS", "colab_type": "text" }, "cell_type": "markdown", "source": [ "你不必熟悉本文中任何东西,因为我们将在后面的课程中仔细研究NumPy。如果你对Numpy感到好奇,可以自行查看 [NumPy reference manual](https://docs.scipy.org/doc/numpy-1.15.1/reference/)." ] }, { "metadata": { "id": "NpI4jhxjwbol", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "" ], "execution_count": 0, "outputs": [] } ] }