{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# choose 函数实现条件筛选" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "对于数组,我们有时候需要进行类似 `switch` 和 `case` 进行条件选择,此时使用 choose 函数十分方便:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[11, 10, 11],\n", " [12, 11, 10],\n", " [11, 12, 12]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "control = np.array([[1,0,1],\n", " [2,1,0],\n", " [1,2,2]])\n", "\n", "np.choose(control, [10, 11, 12])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在上面的例子中,`choose` 将 `0,1,2` 对应的值映射为了 `10, 11, 12`,这里的 `0,1,2` 表示对应的下标。\n", "\n", "事实上, `choose` 不仅仅能接受下标参数,还可以接受下标所在的位置:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[10, 1, 10],\n", " [23, 10, 5],\n", " [10, 27, 28]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i0 = np.array([[0,1,2],\n", " [3,4,5],\n", " [6,7,8]])\n", "i2 = np.array([[20,21,22],\n", " [23,24,25],\n", " [26,27,28]])\n", "control = np.array([[1,0,1],\n", " [2,1,0],\n", " [1,2,2]])\n", "\n", "np.choose(control, [i0, 10, i2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "这里,`control` 传入第一个 `1` 对应的是 10,传入的第一个 `0` 对应于 `i0` 相应位置的值即 `1`,剩下的以此类推。 \n", "\n", "下面的例子将数组中所有小于 `10` 的值变成了 `10`。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ True, True, True],\n", " [False, False, False],\n", " [False, False, False]], dtype=bool)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[ 0, 1, 2], \n", " [10,11,12], \n", " [20,21,22]])\n", "\n", "a < 10" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[10, 10, 10],\n", " [10, 11, 12],\n", " [20, 21, 22]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.choose(a < 10, (a, 10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "下面的例子将数组中所有小于 10 的值变成了 10,大于 15 的值变成了 15。" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1],\n", " [0, 0, 0],\n", " [2, 2, 2]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[ 0, 1, 2], \n", " [10,11,12], \n", " [20,21,22]])\n", "\n", "lt = a < 10\n", "gt = a > 15\n", "\n", "choice = lt + 2 * gt\n", "choice" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[10, 10, 10],\n", " [10, 11, 12],\n", " [15, 15, 15]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.choose(choice, (a, 10, 15))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }