{ "cells": [ { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [], "source": [ "#全部行都能输出\n", "from IPython.core.interactiveshell import InteractiveShell\n", "InteractiveShell.ast_node_interactivity = \"all\"" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 排序函数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## np.sort(a, 要排序的轴) \n", "a是一个ndarray对象,可以是多维的数组, 但是高维的排序非常复杂, 我们这里只讲二维数组。 \n", "按照指定的轴进行升序。" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4, 2, 8],\n", " [1, 5, 6],\n", " [9, 3, 7],\n", " [1, 5, 2]])" ] }, "execution_count": 251, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[4, 2, 8],[1, 5, 6], [9, 3, 7], [1, 5, 2]])\n", "a" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 4, 8],\n", " [1, 5, 6],\n", " [3, 7, 9],\n", " [1, 2, 5]])" ] }, "execution_count": 252, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sort(a) # 不指定的时候, 对行进行升序" ] }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 2],\n", " [1, 3, 6],\n", " [4, 5, 7],\n", " [9, 5, 8]])" ] }, "execution_count": 253, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sort(a, axis=0) # axis=0代表对列进行升序" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 4, 8],\n", " [1, 5, 6],\n", " [3, 7, 9],\n", " [1, 2, 5]])" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sort(a, 1) # axis=0对列进行升序" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## np.argsort(a, 排序的轴) \n", "返回的是数组值从小到大的索引值" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4, 2, 8],\n", " [1, 5, 6],\n", " [9, 3, 7],\n", " [1, 5, 2]])" ] }, "execution_count": 255, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[4, 2, 8],[1, 5, 6], [9, 3, 7], [1, 5, 2]])\n", "a" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 0, 2],\n", " [0, 1, 2],\n", " [1, 2, 0],\n", " [0, 2, 1]], dtype=int64)" ] }, "execution_count": 256, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.argsort(a) # 默认对行进行升序" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "array([[4, 2, 8], ==> 从小到大排序 2 4 8 ,对应的索引 1 0 2\n", " [1, 5, 6], ==> 从小到大排序 1 5 6 ,对应的索引 0 1 2\n", " [9, 3, 7], ==> 从小到大排序 3 7 9 ,对应的索引 1 2 0\n", " [1, 5, 2]]) ==> 从小到大排序 1 2 5 ,对应的索引 0 2 1\n", "```" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 0, 3],\n", " [3, 2, 1],\n", " [0, 1, 2],\n", " [2, 3, 0]], dtype=int64)" ] }, "execution_count": 257, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.argsort(a, 0) #axis=0队列进行升序" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "array([[4, 2, 8],\n", " [1, 5, 6],\n", " [9, 3, 7],\n", " [1, 5, 2]])\n", " 第一列 4 1 9 1 ==> 从小到大排序 1 1 4 9 ,对应的索引 1 3 0 2\n", " 第二列 2 5 3 5 ==> 从小到大排序 2 3 5 5 ,对应的索引 0 2 1 3\n", " 第三列 3 1 2 0 ==> 从小到大排序 0 1 2 3 ,对应的索引 3 1 2 0\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## np.lexsort((a, b)" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 5, 1, 4, 3, 4, 4])" ] }, "execution_count": 258, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "array([9, 4, 0, 4, 0, 2, 1])" ] }, "execution_count": 258, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1,5,1,4,3,4,4] # First column # 升序是 1 1 3 4 4 4 5 \n", "b = [9,4,0,4,0,2,1] # Second column\n", "a1 = np.array(a)\n", "b1 = np.array(b)\n", "a1\n", "b1" ] }, { "cell_type": "code", "execution_count": 259, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 0, 4, 6, 5, 3, 1], dtype=int64)" ] }, "execution_count": 259, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ind = np.lexsort((b1, a1)) # Sort by a, then by b\n", "ind" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "解释:\n", "```python\n", "1 首先对a进行从小到大的排序 1 1 3 4 4 4 5 ==》对应的索引 0 2 4 3 5 6 1\n", "2 但是, 我们发现有2个1 , 3个4, 如果是argsort()的话会按原本的位置进行排序, 但是lexsort()不会\n", "3 lex()会利用b对应位置上的元素来进行排序\n", "4 再来利用b来排序2个1, 两个1的索引是 0 2, 在b中对应的元素为 9 0,从小到大0 9 , 则索引会变成 2 0 4 3 5 6 1\n", "5 再来利用b来排序3个4, 两个1的索引是 3 5 6, 在b中对应的元素为 4 2 1,从小到大1 2 4, 则索引会变成 2 0 4 6 5 3 1\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## np.argmax(a, 指定的轴) \n", "返回指定轴上最大元素的索引\n" ] }, { "cell_type": "code", "execution_count": 260, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[30, 40, 70, 4],\n", " [80, 20, 10, 5],\n", " [50, 90, 60, 2]])" ] }, "execution_count": 260, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[30,40,70, 4],[80,20,10, 5],[50,90,60, 2]])\n", "a" ] }, { "cell_type": "code", "execution_count": 261, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 261, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.argmax(a) # 返回最大元素的所在的整体位置,相当于进行压缩成一维以后的位置" ] }, { "cell_type": "code", "execution_count": 262, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([30, 40, 70, 4, 80, 20, 10, 5, 50, 90, 60, 2])" ] }, "execution_count": 262, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.flatten() # 压缩成一维以后最大元素90的索引为7" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "90" ] }, "execution_count": 263, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.flatten()[9]" ] }, { "cell_type": "code", "execution_count": 264, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 0, 1], dtype=int64)" ] }, "execution_count": 264, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "array([2, 0, 1], dtype=int64)" ] }, "execution_count": 264, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.argmax(a, axis = 0) #按列来求\n", "np.argmax(a, axis = 1) #按行来求" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## np.argmin(a, 指定的轴) \n", "返回指定的轴上最小的索引" ] }, { "cell_type": "code", "execution_count": 265, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[30, 40, 70, 4],\n", " [80, 20, 10, 5],\n", " [50, 90, 60, 2]])" ] }, "execution_count": 265, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[30,40,70, 4],[80,20,10, 5],[50,90,60, 2]])\n", "a" ] }, { "cell_type": "code", "execution_count": 266, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 266, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.argmin(a) # 返回最大元素的所在的整体位置,相当于进行压缩成一维以后的位置" ] }, { "cell_type": "code", "execution_count": 267, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([30, 40, 70, 4, 80, 20, 10, 5, 50, 90, 60, 2])" ] }, "execution_count": 267, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.flatten()" ] }, { "cell_type": "code", "execution_count": 268, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 268, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.flatten()[11]" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 1, 2], dtype=int64)" ] }, "execution_count": 269, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "array([3, 3, 3], dtype=int64)" ] }, "execution_count": 269, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.argmin(a, axis = 0) #按列来求\n", "np.argmin(a, axis = 1) #按行来求" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## np.nonzero(a) \n", "返回数组中非0元素的索引" ] }, { "cell_type": "code", "execution_count": 270, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[30, 40, 0, 0],\n", " [ 0, 20, 0, 10],\n", " [50, 0, 60, 0]])" ] }, "execution_count": 270, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[30,40,0,0],[0,20,0,10],[50,0,60, 0]])\n", "a" ] }, { "cell_type": "code", "execution_count": 271, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([0, 0, 1, 1, 2, 2], dtype=int64),\n", " array([0, 1, 1, 3, 0, 2], dtype=int64))" ] }, "execution_count": 271, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.nonzero(a) # #两个是分别按照axis=0,axis=1返回的索引 第一个数组是行,第二个数组是列!" ] }, { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([30, 40, 20, 10, 50, 60])" ] }, "execution_count": 272, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[[0, 0, 1, 1, 2, 2], [0, 1, 1, 3, 0, 2]]" ] }, { "cell_type": "code", "execution_count": 273, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([30, 40, 20, 10, 50, 60])" ] }, "execution_count": 273, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[np.nonzero(a)] # 可以通过这种方法找到所有的非0元素" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## np.where(条件) \n", " 返回输入数组中满足给定条件的元素的索引" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 1., 2.],\n", " [3., 4., 5.],\n", " [6., 7., 8.]])" ] }, "execution_count": 274, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.arange(9.).reshape(3, 3)\n", "x" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([1, 1, 2, 2, 2], dtype=int64), array([1, 2, 0, 1, 2], dtype=int64))" ] }, "execution_count": 245, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.where(x>3)" ] }, { "cell_type": "code", "execution_count": 246, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4., 5., 6., 7., 8.])" ] }, "execution_count": 246, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[np.where(x>3)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## np.unique(a) \n", "对数组元素进行去重" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 41, 2, 2, 1, 2],\n", " [ 1, 234, 3, 41, 2, 2, 1, 2],\n", " [ 1, 2, 3, 41, 2, 2, 1, 2]])" ] }, "execution_count": 247, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[1, 2, 3, 41, 2, 2, 1, 2], [1, 234, 3, 41, 2, 2, 1, 2], [1, 2, 3, 41, 2, 2, 1, 2]])\n", "a" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 3, 41, 234])" ] }, "execution_count": 248, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.unique(a) # 相当于先压缩成一维在进行去重" ] }, { "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.7.4" }, "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": "223.576px" }, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 2 }