diff --git a/IntroductionToNumpy/task01 数据类型及数组创建/01常量-checkpoint.ipynb b/IntroductionToNumpy/task01 数据类型及数组创建/.ipynb_checkpoints/01常量-checkpoint.ipynb similarity index 100% rename from IntroductionToNumpy/task01 数据类型及数组创建/01常量-checkpoint.ipynb rename to IntroductionToNumpy/task01 数据类型及数组创建/.ipynb_checkpoints/01常量-checkpoint.ipynb diff --git a/IntroductionToNumpy/task01 数据类型及数组创建/02数据类型-checkpoint.ipynb b/IntroductionToNumpy/task01 数据类型及数组创建/.ipynb_checkpoints/02数据类型-checkpoint.ipynb similarity index 100% rename from IntroductionToNumpy/task01 数据类型及数组创建/02数据类型-checkpoint.ipynb rename to IntroductionToNumpy/task01 数据类型及数组创建/.ipynb_checkpoints/02数据类型-checkpoint.ipynb diff --git a/IntroductionToNumpy/task01 数据类型及数组创建/03时间日期和时间增量-checkpoint.ipynb b/IntroductionToNumpy/task01 数据类型及数组创建/.ipynb_checkpoints/03时间日期和时间增量-checkpoint.ipynb similarity index 100% rename from IntroductionToNumpy/task01 数据类型及数组创建/03时间日期和时间增量-checkpoint.ipynb rename to IntroductionToNumpy/task01 数据类型及数组创建/.ipynb_checkpoints/03时间日期和时间增量-checkpoint.ipynb diff --git a/IntroductionToNumpy/task01-2天-数据类型及数组创建/04数组的创建.ipynb b/IntroductionToNumpy/task01-2天-数据类型及数组创建/04数组的创建.ipynb deleted file mode 100644 index 5235bd5..0000000 --- a/IntroductionToNumpy/task01-2天-数据类型及数组创建/04数组的创建.ipynb +++ /dev/null @@ -1,617 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "[toc]\n", - "\n", - "---\n", - "# 数组的创建\n", - "\n", - "导入 numpy。\n", - "\n", - "```\n", - "import numpy as np\n", - "```\n", - "\n", - "numpy 提供的最重要的数据结构是`ndarray`,它是 python 中`list`的扩展。\n", - "\n", - "## 1. 依据现有数据来创建 ndarray\n", - "\n", - "### **(a)通过array()函数进行创建。**\n", - "\n", - "```python\n", - "def array(p_object, dtype=None, copy=True, order='K', subok=False, ndmin=0): \n", - "``` \n", - "\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "# 创建一维数组\n", - "a = np.array([0, 1, 2, 3, 4])\n", - "b = np.array((0, 1, 2, 3, 4))\n", - "print(a, type(a))\n", - "# [0 1 2 3 4] \n", - "print(b, type(b))\n", - "# [0 1 2 3 4] \n", - "\n", - "# 创建二维数组\n", - "c = np.array([[11, 12, 13, 14, 15],\n", - " [16, 17, 18, 19, 20],\n", - " [21, 22, 23, 24, 25],\n", - " [26, 27, 28, 29, 30],\n", - " [31, 32, 33, 34, 35]])\n", - "print(c, type(c))\n", - "# [[11 12 13 14 15]\n", - "# [16 17 18 19 20]\n", - "# [21 22 23 24 25]\n", - "# [26 27 28 29 30]\n", - "# [31 32 33 34 35]] \n", - "\n", - "# 创建三维数组\n", - "d = np.array([[(1.5, 2, 3), (4, 5, 6)],\n", - " [(3, 2, 1), (4, 5, 6)]])\n", - "print(d, type(d))\n", - "# [[[1.5 2. 3. ]\n", - "# [4. 5. 6. ]]\n", - "#\n", - "# [[3. 2. 1. ]\n", - "# [4. 5. 6. ]]] \n", - "```\n", - "\n", - "### **(b)通过asarray()函数进行创建**\n", - "\n", - "`array()`和`asarray()`都可以将结构数据转化为 ndarray,但是`array()`和`asarray()`主要区别就是当数据源是**ndarray** 时,`array()`仍然会 copy 出一个副本,占用新的内存,但不改变 dtype 时 `asarray()`不会。\n", - "\n", - "```python\n", - "def asarray(a, dtype=None, order=None):\n", - " return array(a, dtype, copy=False, order=order)\n", - "```\n", - "\n", - "【例】`array()`和`asarray()`都可以将结构数据转化为 ndarray\n", - "```python\n", - "import numpy as np\n", - "\n", - "x = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]\n", - "y = np.array(x)\n", - "z = np.asarray(x)\n", - "x[1][2] = 2\n", - "print(x,type(x))\n", - "# [[1, 1, 1], [1, 1, 2], [1, 1, 1]] \n", - "\n", - "print(y,type(y))\n", - "# [[1 1 1]\n", - "# [1 1 1]\n", - "# [1 1 1]] \n", - "\n", - "print(z,type(z))\n", - "# [[1 1 1]\n", - "# [1 1 1]\n", - "# [1 1 1]] \n", - "```\n", - "\n", - "【例】`array()`和`asarray()`的区别。(`array()`和`asarray()`主要区别就是当数据源是**ndarray** 时,`array()`仍然会 copy 出一个副本,占用新的内存,但不改变 dtype 时 `asarray()`不会。)\n", - "```python\n", - "import numpy as np\n", - "\n", - "x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])\n", - "y = np.array(x)\n", - "z = np.asarray(x)\n", - "w = np.asarray(x, dtype=np.int)\n", - "x[1][2] = 2\n", - "print(x,type(x),x.dtype)\n", - "# [[1 1 1]\n", - "# [1 1 2]\n", - "# [1 1 1]] int32\n", - "\n", - "print(y,type(y),y.dtype)\n", - "# [[1 1 1]\n", - "# [1 1 1]\n", - "# [1 1 1]] int32\n", - "\n", - "print(z,type(z),z.dtype)\n", - "# [[1 1 1]\n", - "# [1 1 2]\n", - "# [1 1 1]] int32\n", - "\n", - "print(w,type(w),w.dtype)\n", - "# [[1 1 1]\n", - "# [1 1 2]\n", - "# [1 1 1]] int32\n", - "```\n", - "\n", - "\n", - "【例】更改为较大的dtype时,其大小必须是array的最后一个axis的总大小(以字节为单位)的除数\n", - "```python\n", - "import numpy as np\n", - "\n", - "x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])\n", - "print(x, x.dtype)\n", - "# [[1 1 1]\n", - "# [1 1 1]\n", - "# [1 1 1]] int32\n", - "x.dtype = np.float\n", - "\n", - "# ValueError: When changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array.\n", - "```\n", - "\n", - "### **(c)通过fromfunction()函数进行创建**\n", - "\n", - "给函数绘图的时候可能会用到`fromfunction()`,该函数可从函数中创建数组。\n", - "\n", - "```python\n", - "def fromfunction(function, shape, **kwargs):\n", - "```\n", - "\n", - "\n", - "【例】通过在每个坐标上执行一个函数来构造数组。\n", - "\n", - "```python\n", - "import numpy as np\n", - "\n", - "def f(x, y):\n", - " return 10 * x + y\n", - "\n", - "x = np.fromfunction(f, (5, 4), dtype=int)\n", - "print(x)\n", - "# [[ 0 1 2 3]\n", - "# [10 11 12 13]\n", - "# [20 21 22 23]\n", - "# [30 31 32 33]\n", - "# [40 41 42 43]]\n", - "\n", - "x = np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)\n", - "print(x)\n", - "# [[ True False False]\n", - "# [False True False]\n", - "# [False False True]]\n", - "\n", - "x = np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)\n", - "print(x)\n", - "# [[0 1 2]\n", - "# [1 2 3]\n", - "# [2 3 4]]\n", - "```\n", - "\n", - "\n", - "\n", - "## 2. 依据 ones 和 zeros 填充方式\n", - "\n", - "在机器学习任务中经常做的一件事就是初始化参数,需要用常数值或者随机值来创建一个固定大小的矩阵。\n", - "\n", - "### **(a)零数组**\n", - "\n", - "- `zeros()`函数:返回给定形状和类型的零数组。\n", - "- `zeros_like()`函数:返回与给定数组形状和类型相同的零数组。\n", - "\n", - "```python\n", - "def zeros(shape, dtype=None, order='C'):\n", - "def zeros_like(a, dtype=None, order='K', subok=True, shape=None):\n", - "```\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "x = np.zeros(5)\n", - "print(x) # [0. 0. 0. 0. 0.]\n", - "x = np.zeros([2, 3])\n", - "print(x)\n", - "# [[0. 0. 0.]\n", - "# [0. 0. 0.]]\n", - "\n", - "x = np.array([[1, 2, 3], [4, 5, 6]])\n", - "y = np.zeros_like(x)\n", - "print(y)\n", - "# [[0 0 0]\n", - "# [0 0 0]]\n", - "```\n", - "\n", - "### **(b)1数组**\n", - "\n", - "- `ones()`函数:返回给定形状和类型的1数组。\n", - "- `ones_like()`函数:返回与给定数组形状和类型相同的1数组。\n", - "\n", - "```python\n", - "def ones(shape, dtype=None, order='C'):\n", - "def ones_like(a, dtype=None, order='K', subok=True, shape=None):\n", - "```\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "x = np.ones(5)\n", - "print(x) # [1. 1. 1. 1. 1.]\n", - "x = np.ones([2, 3])\n", - "print(x)\n", - "# [[1. 1. 1.]\n", - "# [1. 1. 1.]]\n", - "\n", - "x = np.array([[1, 2, 3], [4, 5, 6]])\n", - "y = np.ones_like(x)\n", - "print(y)\n", - "# [[1 1 1]\n", - "# [1 1 1]]\n", - "```\n", - "\n", - "### **(c)空数组**\n", - "\n", - "- `empty()`函数:返回一个空数组,数组元素为随机数。\n", - "- `empty_like`函数:返回与给定数组具有相同形状和类型的新数组。\n", - "\n", - "```python\n", - "def empty(shape, dtype=None, order='C'): \n", - "def empty_like(prototype, dtype=None, order='K', subok=True, shape=None):\n", - "```\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "x = np.empty(5)\n", - "print(x)\n", - "# [1.95821574e-306 1.60219035e-306 1.37961506e-306 \n", - "# 9.34609790e-307 1.24610383e-306]\n", - "\n", - "x = np.empty((3, 2))\n", - "print(x)\n", - "# [[1.60220393e-306 9.34587382e-307]\n", - "# [8.45599367e-307 7.56598449e-307]\n", - "# [1.33509389e-306 3.59412896e-317]]\n", - "\n", - "x = np.array([[1, 2, 3], [4, 5, 6]])\n", - "y = np.empty_like(x)\n", - "print(y)\n", - "# [[ 7209029 6422625 6619244]\n", - "# [ 100 707539280 504]]\n", - "```\n", - "\n", - "### **(d)单位数组**\n", - "\n", - "- `eye()`函数:返回一个对角线上为1,其它地方为零的单位数组。\n", - "- `identity()`函数:返回一个方的单位数组。\n", - "\n", - "```python\n", - "def eye(N, M=None, k=0, dtype=float, order='C'):\n", - "def identity(n, dtype=None):\n", - "```\n", - "\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "x = np.eye(4)\n", - "print(x)\n", - "# [[1. 0. 0. 0.]\n", - "# [0. 1. 0. 0.]\n", - "# [0. 0. 1. 0.]\n", - "# [0. 0. 0. 1.]]\n", - "\n", - "x = np.eye(2, 3)\n", - "print(x)\n", - "# [[1. 0. 0.]\n", - "# [0. 1. 0.]]\n", - "\n", - "x = np.identity(4)\n", - "print(x)\n", - "# [[1. 0. 0. 0.]\n", - "# [0. 1. 0. 0.]\n", - "# [0. 0. 1. 0.]\n", - "# [0. 0. 0. 1.]]\n", - "```\n", - "\n", - "### **(e)对角数组**\n", - "\n", - "- `diag()`函数:提取对角线或构造对角数组。\n", - "\n", - "```python\n", - "def diag(v, k=0):\n", - "```\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "x = np.arange(9).reshape((3, 3))\n", - "print(x)\n", - "# [[0 1 2]\n", - "# [3 4 5]\n", - "# [6 7 8]]\n", - "print(np.diag(x)) # [0 4 8]\n", - "print(np.diag(x, k=1)) # [1 5]\n", - "print(np.diag(x, k=-1)) # [3 7]\n", - "\n", - "v = [1, 3, 5, 7]\n", - "x = np.diag(v)\n", - "print(x)\n", - "# [[1 0 0 0]\n", - "# [0 3 0 0]\n", - "# [0 0 5 0]\n", - "# [0 0 0 7]]\n", - "```\n", - "\n", - "### **(f)常数数组**\n", - "\n", - "- `full()`函数:返回一个常数数组。\n", - "- `full_like()`函数:返回与给定数组具有相同形状和类型的常数数组。\n", - "\n", - "```python\n", - "def full(shape, fill_value, dtype=None, order='C'):\n", - "def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None):\n", - "```\n", - "\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "x = np.full((2,), 7)\n", - "print(x)\n", - "# [7 7]\n", - "\n", - "x = np.full(2, 7)\n", - "print(x)\n", - "# [7 7]\n", - "\n", - "x = np.full((2, 7), 7)\n", - "print(x)\n", - "# [[7 7 7 7 7 7 7]\n", - "# [7 7 7 7 7 7 7]]\n", - "\n", - "x = np.array([[1, 2, 3], [4, 5, 6]])\n", - "y = np.full_like(x, 7)\n", - "print(y)\n", - "# [[7 7 7]\n", - "# [7 7 7]]\n", - "```\n", - "\n", - "\n", - "\n", - "## 3. 利用数值范围来创建ndarray\n", - " - `arange()`函数:返回给定间隔内的均匀间隔的值。\n", - " - `linspace()`函数:返回指定间隔内的等间隔数字。\n", - " - `logspace()`函数:返回数以对数刻度均匀分布。\n", - " - `numpy.random.rand()` 返回一个由[0,1)内的随机数组成的数组。\n", - "\n", - "```python\n", - "def arange([start,] stop[, step,], dtype=None): \n", - "def linspace(start, stop, num=50, endpoint=True, retstep=False, \n", - " dtype=None, axis=0):\n", - "def logspace(start, stop, num=50, endpoint=True, base=10.0, \n", - " dtype=None, axis=0):\n", - "def rand(d0, d1, ..., dn): \n", - "```\n", - "\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "x = np.arange(5)\n", - "print(x) # [0 1 2 3 4]\n", - "\n", - "x = np.arange(3, 7, 2)\n", - "print(x) # [3 5]\n", - "\n", - "x = np.linspace(start=0, stop=2, num=9)\n", - "print(x) \n", - "# [0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ]\n", - "\n", - "x = np.logspace(0, 1, 5)\n", - "print(np.around(x, 2))\n", - "# [ 1. 1.78 3.16 5.62 10. ] \n", - " #np.around 返回四舍五入后的值,可指定精度。\n", - " # around(a, decimals=0, out=None)\n", - " # a 输入数组\n", - " # decimals 要舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置\n", - "\n", - "\n", - "x = np.linspace(start=0, stop=1, num=5)\n", - "x = [10 ** i for i in x]\n", - "print(np.around(x, 2))\n", - "# [ 1. 1.78 3.16 5.62 10. ]\n", - "\n", - "x = np.random.random(5)\n", - "print(x)\n", - "# [0.41768753 0.16315577 0.80167915 0.99690199 0.11812291]\n", - "\n", - "x = np.random.random([2, 3])\n", - "print(x)\n", - "# [[0.41151858 0.93785153 0.57031309]\n", - "# [0.13482333 0.20583516 0.45429181]]\n", - "```\n", - "\n", - "\n", - "\n", - "---\n", - "## 4. 结构数组的创建\n", - "\n", - "结构数组,首先需要定义结构,然后利用`np.array()`来创建数组,其参数`dtype`为定义的结构。\n", - "\n", - "### **(a)利用字典来定义结构**\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "personType = np.dtype({\n", - " 'names': ['name', 'age', 'weight'],\n", - " 'formats': ['U30', 'i8', 'f8']})\n", - "\n", - "a = np.array([('Liming', 24, 63.9), ('Mike', 15, 67.), ('Jan', 34, 45.8)],\n", - " dtype=personType)\n", - "print(a, type(a))\n", - "# [('Liming', 24, 63.9) ('Mike', 15, 67. ) ('Jan', 34, 45.8)]\n", - "# \n", - "```\n", - "\n", - "### **(b)利用包含多个元组的列表来定义结构**\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "personType = np.dtype([('name', 'U30'), ('age', 'i8'), ('weight', 'f8')])\n", - "a = np.array([('Liming', 24, 63.9), ('Mike', 15, 67.), ('Jan', 34, 45.8)],\n", - " dtype=personType)\n", - "print(a, type(a))\n", - "# [('Liming', 24, 63.9) ('Mike', 15, 67. ) ('Jan', 34, 45.8)]\n", - "# \n", - "\n", - "# 结构数组的取值方式和一般数组差不多,可以通过下标取得元素:\n", - "print(a[0])\n", - "# ('Liming', 24, 63.9)\n", - "\n", - "print(a[-2:])\n", - "# [('Mike', 15, 67. ) ('Jan', 34, 45.8)]\n", - "\n", - "# 我们可以使用字段名作为下标获取对应的值\n", - "print(a['name'])\n", - "# ['Liming' 'Mike' 'Jan']\n", - "print(a['age'])\n", - "# [24 15 34]\n", - "print(a['weight'])\n", - "# [63.9 67. 45.8]\n", - "```\n", - "\n", - "---\n", - "# 数组的属性\n", - "\n", - "在使用 numpy 时,你会想知道数组的某些信息。很幸运,在这个包里边包含了很多便捷的方法,可以给你想要的信息。\n", - "\n", - "\n", - "\n", - " - `numpy.ndarray.ndim`用于返回数组的维数(轴的个数)也称为秩,一维数组的秩为 1,二维数组的秩为 2,以此类推。\n", - " - `numpy.ndarray.shape`表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 `ndim` 属性(秩)。\n", - " - `numpy.ndarray.size`数组中所有元素的总量,相当于数组的`shape`中所有元素的乘积,例如矩阵的元素总量为行与列的乘积。\n", - " - `numpy.ndarray.dtype` `ndarray` 对象的元素类型。\n", - " - `numpy.ndarray.itemsize`以字节的形式返回数组中每一个元素的大小。\n", - "\n", - "```python\n", - "class ndarray(object):\n", - " shape = property(lambda self: object(), lambda self, v: None, lambda self: None)\n", - " dtype = property(lambda self: object(), lambda self, v: None, lambda self: None)\n", - " size = property(lambda self: object(), lambda self, v: None, lambda self: None)\n", - " ndim = property(lambda self: object(), lambda self, v: None, lambda self: None)\n", - " itemsize = property(lambda self: object(), lambda self, v: None, lambda self: None)\n", - "```\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "a = np.array([1, 2, 3, 4, 5])\n", - "print(a.shape) # (5,)\n", - "print(a.dtype) # int32\n", - "print(a.size) # 5\n", - "print(a.ndim) # 1\n", - "print(a.itemsize) # 4\n", - "\n", - "b = np.array([[1, 2, 3], [4, 5, 6.0]])\n", - "print(b.shape) # (2, 3)\n", - "print(b.dtype) # float64\n", - "print(b.size) # 6\n", - "print(b.ndim) # 2\n", - "print(b.itemsize) # 8\n", - "```\n", - "\n", - "在`ndarray`中所有元素必须是同一类型,否则会自动向下转换,`int->float->str`。\n", - "\n", - "【例】\n", - "```python\n", - "import numpy as np\n", - "\n", - "a = np.array([1, 2, 3, 4, 5])\n", - "print(a) # [1 2 3 4 5]\n", - "b = np.array([1, 2, 3, 4, '5'])\n", - "print(b) # ['1' '2' '3' '4' '5']\n", - "c = np.array([1, 2, 3, 4, 5.0])\n", - "print(c) # [1. 2. 3. 4. 5.]\n", - "```\n", - "\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "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.8.3" - }, - "toc": { - "base_numbering": 1, - "nav_menu": {}, - "number_sections": true, - "sideBar": true, - "skip_h1_title": false, - "title_cell": "Table of Contents", - "title_sidebar": "Contents", - "toc_cell": false, - "toc_position": { - "height": "calc(100% - 180px)", - "left": "10px", - "top": "150px", - "width": "307.188px" - }, - "toc_section_display": true, - "toc_window_display": true - }, - "varInspector": { - "cols": { - "lenName": 16, - "lenType": 16, - "lenVar": 40 - }, - "kernels_config": { - "python": { - "delete_cmd_postfix": "", - "delete_cmd_prefix": "del ", - "library": "var_list.py", - "varRefreshCmd": "print(var_dic_list())" - }, - "r": { - "delete_cmd_postfix": ") ", - "delete_cmd_prefix": "rm(", - "library": "var_list.r", - "varRefreshCmd": "cat(var_dic_list()) " - } - }, - "types_to_exclude": [ - "module", - "function", - "builtin_function_or_method", - "instance", - "_Feature" - ], - "window_display": false - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/IntroductionToNumpy/task05 排序搜索计数及集合操作/练习-排序搜索计数,集合操作-答案.ipynb b/IntroductionToNumpy/task05 排序搜索计数及集合操作/练习-排序搜索计数,集合操作-答案.ipynb index bfab1a1..26e1127 100644 --- a/IntroductionToNumpy/task05 排序搜索计数及集合操作/练习-排序搜索计数,集合操作-答案.ipynb +++ b/IntroductionToNumpy/task05 排序搜索计数及集合操作/练习-排序搜索计数,集合操作-答案.ipynb @@ -68,6 +68,176 @@ "print (Z[Z[:,2].argsort()])" ] }, + { + "cell_type": "markdown", + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-11T12:42:05.895859Z", + "start_time": "2020-10-11T12:42:05.891836Z" + } + }, + "source": [ + "**对x的列进行排序,先按照最后一行排序,假如两数相等则按照倒数第二行排序**\n", + "\n", + "【知识点:排序】\n", + "\n", + "- (提示: lexsort)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-11T12:43:13.585247Z", + "start_time": "2020-10-11T12:43:13.582255Z" + } + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "x=np.array([[1,2,3,4],[1,4,3,3]])" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-11T12:43:21.756991Z", + "start_time": "2020-10-11T12:43:21.750043Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0, 2, 3, 1], dtype=int64)" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "array([[1, 3, 4, 2],\n", + " [1, 3, 3, 4]])" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "y=np.lexsort(x,axis=0)\n", + "display(y)\n", + "display(x[:,y])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**取出每一列比第三大的数字小的数**\n", + "【知识点:排序】\n", + "- (提示:np.argpartition/np.partition)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-11T13:42:41.515755Z", + "start_time": "2020-10-11T13:42:41.511766Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 9 25 4]\n", + " [ 8 24 16]\n", + " [17 11 21]\n", + " [ 3 22 3]\n", + " [ 3 15 3]\n", + " [18 17 25]\n", + " [16 5 12]\n", + " [29 27 17]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "\n", + "np.random.seed(100)\n", + "x = np.random.randint(1, 30, [8, 3])\n", + "print(x)\n", + "# [[ 9 25 4]\n", + "# [ 8 24 16]\n", + "# [17 11 21]\n", + "# [ 3 22 3]\n", + "# [ 3 15 3]\n", + "# [18 17 25]\n", + "# [16 5 12]\n", + "# [29 27 17]]\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-11T13:42:42.122927Z", + "start_time": "2020-10-11T13:42:42.117940Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 3 5 3]\n", + " [ 3 11 3]]\n" + ] + } + ], + "source": [ + "z = np.argpartition(x, kth=2, axis=0)\n", + "y=np.array([[ x[z[i,j],j]for j in range(3)]for i in range(2)])\n", + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-11T13:19:25.176370Z", + "start_time": "2020-10-11T13:19:25.172381Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 3, 5, 3],\n", + " [ 3, 11, 3]])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.partition(x, kth=2, axis=0)[:2]" + ] + }, { "cell_type": "markdown", "metadata": { @@ -330,6 +500,52 @@ "# [1. 2. 3. 5. 6. 7.]" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**获取给定数组a中比7大的数有多少。**\n", + "\n", + "- `a = np.random.uniform(1, 50, 20)`\n", + "\n", + "【知识点:搜索】\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-11T14:06:10.903242Z", + "start_time": "2020-10-11T14:06:10.897219Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[35.37289863 19.89303398 1.85370469 3.49737665 22.31851519 48.86534949\n", + " 5.59538215 28.0244556 32.512054 22.572477 ]\n" + ] + }, + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a=np.random.uniform(1, 50, 10)\n", + "print(a)\n", + "len(a)-np.searchsorted(a,7,side='right')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -425,20 +641,6 @@ "x = np.setdiff1d(a, b)\n", "print(x) # [1 2 3 4]" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/IntroductionToNumpy/task05 排序搜索计数及集合操作/练习-排序搜索计数,集合操作.ipynb b/IntroductionToNumpy/task05 排序搜索计数及集合操作/练习-排序搜索计数,集合操作.ipynb index 2dad162..f8bc5da 100644 --- a/IntroductionToNumpy/task05 排序搜索计数及集合操作/练习-排序搜索计数,集合操作.ipynb +++ b/IntroductionToNumpy/task05 排序搜索计数及集合操作/练习-排序搜索计数,集合操作.ipynb @@ -19,6 +19,26 @@ "- (提示: argsort)\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### **对x的列进行排序,先按照最后一行排序,假如两数相等则按照倒数第二行排序**\n", + "\n", + "【知识点:排序】\n", + "\n", + "- (提示: lexsort)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### **取出每一列比第三大的数字小的数**\n", + "【知识点:排序】\n", + "- (提示:np.argpartition/np.partition)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -78,6 +98,17 @@ "- 如何删除numpy数组中的缺失值?" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### **获取给定数组a中比7大的数有多少。**\n", + "\n", + "- `a = np.random.uniform(1, 50, 20)`\n", + "\n", + "【知识点:搜索】" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -112,9 +143,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 1, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-13T13:49:02.928408Z", + "start_time": "2020-10-13T13:49:02.919452Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1e-06" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [] } ],