{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 一般函数" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 三角函数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " sin(x)\n", " cos(x)\n", " tan(x)\n", " sinh(x)\n", " conh(x)\n", " tanh(x)\n", " arccos(x)\n", " arctan(x)\n", " arcsin(x)\n", " arccosh(x)\n", " arctanh(x)\n", " arcsinh(x)\n", " arctan2(x,y)\n", "\n", "`arctan2(x,y)` 返回 `arctan(x/y)` 。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 向量操作" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " dot(x,y)\n", " inner(x,y)\n", " cross(x,y)\n", " vdot(x,y)\n", " outer(x,y)\n", " kron(x,y)\n", " tensordot(x,y[,axis])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 其他操作" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " exp(x)\n", " log(x)\n", " log10(x)\n", " sqrt(x)\n", " absolute(x)\n", " conjugate(x)\n", " negative(x)\n", " ceil(x)\n", " floor(x)\n", " fabs(x)\n", " hypot(x)\n", " fmod(x)\n", " maximum(x,y)\n", " minimum(x,y)\n", "\n", "`hypot` 返回对应点 `(x,y)` 到原点的距离。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 4.12310563, 5.38516481, 6.70820393])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([1,2,3])\n", "y = np.array([4,5,6])\n", "np.hypot(x,y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 类型处理" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " iscomplexobj\n", " iscomplex\n", " isrealobj\n", " isreal\n", " imag\n", " real\n", " real_if_close\n", " isscalar\n", " isneginf\n", " isposinf\n", " isinf\n", " isfinite\n", " isnan\n", " nan_to_num\n", " common_type\n", " typename\n", "\n", "正无穷:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "inf" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.inf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "负无穷:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-inf" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-np.inf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "非法值(Not a number):" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "nan" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.nan" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "检查是否为无穷:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.isinf(1.0)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.isinf(np.inf)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.isinf(-np.inf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "非法值:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Miniconda\\lib\\site-packages\\IPython\\kernel\\__main__.py:1: RuntimeWarning: invalid value encountered in divide\n", " if __name__ == '__main__':\n" ] }, { "data": { "text/plain": [ "array([ nan])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([0]) / 0.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "这并不会报错,而是返回一个非法值。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "只有 `0/0` 会得到 `nan`,非0值除以0会得到无穷:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Miniconda\\lib\\site-packages\\IPython\\kernel\\__main__.py:2: RuntimeWarning: divide by zero encountered in divide\n", " from IPython.kernel.zmq import kernelapp as app\n", "c:\\Miniconda\\lib\\site-packages\\IPython\\kernel\\__main__.py:2: RuntimeWarning: invalid value encountered in divide\n", " from IPython.kernel.zmq import kernelapp as app\n" ] }, { "data": { "text/plain": [ "array([ nan, inf, inf, inf, inf])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(5.0)\n", "b = a / 0.0\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`nan` 与任何数进行比较都是 `False`:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, False, False], dtype=bool)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b == np.nan" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "想要找出 `nan` 值需要使用 `isnan`:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ True, False, False, False, False], dtype=bool)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.isnan(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 修改形状" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " atleast_1d\n", " atleast_2d\n", " atleast_3d\n", " expand_dims\n", " apply_over_axes\n", " apply_along_axis\n", " hstack\n", " vstack\n", " dstack\n", " column_stack\n", " hsplit\n", " vsplit\n", " dsplit\n", " split\n", " squeeze" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 其他有用函数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " fix\n", " mod\n", " amax\n", " amin\n", " ptp\n", " sum\n", " cumsum\n", " prod\n", " cumprod\n", " diff\n", " angle\n", "\n", " unwrap\n", " sort_complex\n", " trim_zeros\n", " fliplr\n", " flipud\n", " rot90\n", " diag\n", " eye\n", " select\n", " extract\n", " insert\n", "\n", " roots\n", " poly\n", " any\n", " all\n", " disp\n", " unique\n", " nansum\n", " nanmax\n", " nanargmax\n", " nanargmin\n", " nanmin\n", "\n", "`nan` 开头的函数会进行相应的操作,但是忽略 `nan` 值。" ] } ], "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 }