{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Random Sampling" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'1.11.2'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.__version__" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "__author__ = 'kyubyong. longinglove@nate.com'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simple random data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Q1. Create an array of shape (3, 2) and populate it with random samples from a uniform distribution over [0, 1)." ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.13879034, 0.71300174],\n", " [ 0.08121322, 0.00393554],\n", " [ 0.02349471, 0.56677474]])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Q2. Create an array of shape (1000, 1000) and populate it with random samples from a standard normal distribution. And verify that the mean and standard deviation is close enough to 0 and 1 repectively." ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.00110028519551\n", "0.999683483393\n" ] } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Q3. Create an array of shape (3, 2) and populate it with random integers ranging from 0 to 3 (inclusive) from a discrete uniform distribution." ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 3],\n", " [3, 0],\n", " [0, 0]])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Q4. Extract 1 elements from x randomly such that each of them would be associated with probabilities .3, .5, .2. Then print the result 10 times." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 out of 10\n", "2 out of 10\n", "3 out of 10\n", "5 out of 10\n", "2 out of 10\n", "5 out of 10\n", "2 out of 10\n", "2 out of 10\n", "2 out of 10\n", "5 out of 10\n" ] } ], "source": [ "x = [b'3 out of 10', b'5 out of 10', b'2 out of 10']\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Q5. Extract 3 different integers from 0 to 9 randomly with the same probabilities." ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([5, 4, 0])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Permutations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Q6. Shuffle numbers between 0 and 9 (inclusive)." ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 3 8 4 5 1 0 6 9 7]\n" ] } ], "source": [] }, { "cell_type": "code", "execution_count": 88, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5 2 7 4 1 0 6 8 9 3]\n" ] } ], "source": [ "# Or\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Random generator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Q7. Assign number 10 to the seed of the random generator so that you can get the same value next time." ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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 }