{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "numpy.random模块提供了很多生成随机数的函数,可以选择生成符合某种概率分布的随机数。比如我们可以用normal得到一个4 x 4的,符合标准正态分布的数组:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.5382462 , -0.79452471, -0.07993797, 0.72243531],\n", " [ 0.87180676, 1.61663011, -0.62169955, 1.73880636],\n", " [ 1.88294218, 0.07432438, 1.63474848, 0.23519213],\n", " [ 0.92847885, -0.45791646, 0.63965317, -0.23654448]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "samples = np.random.normal(size=(4, 4))\n", "samples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "相对的,python内建的random模块一次只能生成一个样本。在生成大量样本方法,numpy.random是非常快的:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from random import normalvariate\n", "\n", "N = 1000000" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 loop, best of 3: 1.25 s per loop\n" ] } ], "source": [ "%timeit sample = [normalvariate(0, 1) for _ in range(N)]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 loops, best of 3: 49.1 ms per loop\n" ] } ], "source": [ "%timeit np.random.normal(size=N)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "之所以称之为伪随机数,是因为随机数生成算法是根据seed来生成的。也就是说,只要seed设置一样,每次生成的随机数是相同的:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.random.seed(1234)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当然,这个seed是全局的,如果想要避免全局状态,可以用numpy.random.RandomState来创建一个独立的生成器:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rng = np.random.RandomState(1234)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.47143516, -1.19097569, 1.43270697, -0.3126519 , -0.72058873,\n", " 0.88716294, 0.85958841, -0.6365235 , 0.01569637, -2.24268495])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rng.randn(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "下面是是写numpy.random里的函数:\n", "\n", "![](http://oydgk2hgw.bkt.clouddn.com/pydata-book/rzcuf.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [py35]", "language": "python", "name": "Python [py35]" }, "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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }