|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Random Sampling" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 2, |
| 13 | + "metadata": { |
| 14 | + "collapsed": true |
| 15 | + }, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import numpy as np" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": 3, |
| 24 | + "metadata": { |
| 25 | + "collapsed": false |
| 26 | + }, |
| 27 | + "outputs": [ |
| 28 | + { |
| 29 | + "data": { |
| 30 | + "text/plain": [ |
| 31 | + "'1.11.2'" |
| 32 | + ] |
| 33 | + }, |
| 34 | + "execution_count": 3, |
| 35 | + "metadata": {}, |
| 36 | + "output_type": "execute_result" |
| 37 | + } |
| 38 | + ], |
| 39 | + "source": [ |
| 40 | + "np.__version__" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 5, |
| 46 | + "metadata": { |
| 47 | + "collapsed": false |
| 48 | + }, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "__author__ = 'kyubyong. [email protected]'" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "## Simple random data" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "markdown", |
| 63 | + "metadata": {}, |
| 64 | + "source": [ |
| 65 | + "Q1. Create an array of shape (3, 2) and populate it with random samples from a uniform distribution over [0, 1)." |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 49, |
| 71 | + "metadata": { |
| 72 | + "collapsed": false |
| 73 | + }, |
| 74 | + "outputs": [ |
| 75 | + { |
| 76 | + "data": { |
| 77 | + "text/plain": [ |
| 78 | + "array([[ 0.13879034, 0.71300174],\n", |
| 79 | + " [ 0.08121322, 0.00393554],\n", |
| 80 | + " [ 0.02349471, 0.56677474]])" |
| 81 | + ] |
| 82 | + }, |
| 83 | + "execution_count": 49, |
| 84 | + "metadata": {}, |
| 85 | + "output_type": "execute_result" |
| 86 | + } |
| 87 | + ], |
| 88 | + "source": [] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "markdown", |
| 92 | + "metadata": {}, |
| 93 | + "source": [ |
| 94 | + "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." |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 42, |
| 100 | + "metadata": { |
| 101 | + "collapsed": false |
| 102 | + }, |
| 103 | + "outputs": [ |
| 104 | + { |
| 105 | + "name": "stdout", |
| 106 | + "output_type": "stream", |
| 107 | + "text": [ |
| 108 | + "-0.00110028519551\n", |
| 109 | + "0.999683483393\n" |
| 110 | + ] |
| 111 | + } |
| 112 | + ], |
| 113 | + "source": [] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "markdown", |
| 117 | + "metadata": {}, |
| 118 | + "source": [ |
| 119 | + "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." |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": 44, |
| 125 | + "metadata": { |
| 126 | + "collapsed": false |
| 127 | + }, |
| 128 | + "outputs": [ |
| 129 | + { |
| 130 | + "data": { |
| 131 | + "text/plain": [ |
| 132 | + "array([[1, 3],\n", |
| 133 | + " [3, 0],\n", |
| 134 | + " [0, 0]])" |
| 135 | + ] |
| 136 | + }, |
| 137 | + "execution_count": 44, |
| 138 | + "metadata": {}, |
| 139 | + "output_type": "execute_result" |
| 140 | + } |
| 141 | + ], |
| 142 | + "source": [] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "markdown", |
| 146 | + "metadata": {}, |
| 147 | + "source": [ |
| 148 | + "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." |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "code", |
| 153 | + "execution_count": 3, |
| 154 | + "metadata": { |
| 155 | + "collapsed": false |
| 156 | + }, |
| 157 | + "outputs": [ |
| 158 | + { |
| 159 | + "name": "stdout", |
| 160 | + "output_type": "stream", |
| 161 | + "text": [ |
| 162 | + "5 out of 10\n", |
| 163 | + "2 out of 10\n", |
| 164 | + "3 out of 10\n", |
| 165 | + "5 out of 10\n", |
| 166 | + "2 out of 10\n", |
| 167 | + "5 out of 10\n", |
| 168 | + "2 out of 10\n", |
| 169 | + "2 out of 10\n", |
| 170 | + "2 out of 10\n", |
| 171 | + "5 out of 10\n" |
| 172 | + ] |
| 173 | + } |
| 174 | + ], |
| 175 | + "source": [ |
| 176 | + "x = [b'3 out of 10', b'5 out of 10', b'2 out of 10']\n" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "markdown", |
| 181 | + "metadata": {}, |
| 182 | + "source": [ |
| 183 | + "Q5. Extract 3 different integers from 0 to 9 randomly with the same probabilities." |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "code", |
| 188 | + "execution_count": 66, |
| 189 | + "metadata": { |
| 190 | + "collapsed": false |
| 191 | + }, |
| 192 | + "outputs": [ |
| 193 | + { |
| 194 | + "data": { |
| 195 | + "text/plain": [ |
| 196 | + "array([5, 4, 0])" |
| 197 | + ] |
| 198 | + }, |
| 199 | + "execution_count": 66, |
| 200 | + "metadata": {}, |
| 201 | + "output_type": "execute_result" |
| 202 | + } |
| 203 | + ], |
| 204 | + "source": [] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "markdown", |
| 208 | + "metadata": {}, |
| 209 | + "source": [ |
| 210 | + "## Permutations" |
| 211 | + ] |
| 212 | + }, |
| 213 | + { |
| 214 | + "cell_type": "markdown", |
| 215 | + "metadata": {}, |
| 216 | + "source": [ |
| 217 | + "Q6. Shuffle numbers between 0 and 9 (inclusive)." |
| 218 | + ] |
| 219 | + }, |
| 220 | + { |
| 221 | + "cell_type": "code", |
| 222 | + "execution_count": 86, |
| 223 | + "metadata": { |
| 224 | + "collapsed": false |
| 225 | + }, |
| 226 | + "outputs": [ |
| 227 | + { |
| 228 | + "name": "stdout", |
| 229 | + "output_type": "stream", |
| 230 | + "text": [ |
| 231 | + "[2 3 8 4 5 1 0 6 9 7]\n" |
| 232 | + ] |
| 233 | + } |
| 234 | + ], |
| 235 | + "source": [] |
| 236 | + }, |
| 237 | + { |
| 238 | + "cell_type": "code", |
| 239 | + "execution_count": 88, |
| 240 | + "metadata": { |
| 241 | + "collapsed": false |
| 242 | + }, |
| 243 | + "outputs": [ |
| 244 | + { |
| 245 | + "name": "stdout", |
| 246 | + "output_type": "stream", |
| 247 | + "text": [ |
| 248 | + "[5 2 7 4 1 0 6 8 9 3]\n" |
| 249 | + ] |
| 250 | + } |
| 251 | + ], |
| 252 | + "source": [ |
| 253 | + "# Or\n" |
| 254 | + ] |
| 255 | + }, |
| 256 | + { |
| 257 | + "cell_type": "markdown", |
| 258 | + "metadata": {}, |
| 259 | + "source": [ |
| 260 | + "## Random generator" |
| 261 | + ] |
| 262 | + }, |
| 263 | + { |
| 264 | + "cell_type": "markdown", |
| 265 | + "metadata": {}, |
| 266 | + "source": [ |
| 267 | + "Q7. Assign number 10 to the seed of the random generator so that you can get the same value next time." |
| 268 | + ] |
| 269 | + }, |
| 270 | + { |
| 271 | + "cell_type": "code", |
| 272 | + "execution_count": 91, |
| 273 | + "metadata": { |
| 274 | + "collapsed": true |
| 275 | + }, |
| 276 | + "outputs": [], |
| 277 | + "source": [] |
| 278 | + } |
| 279 | + ], |
| 280 | + "metadata": { |
| 281 | + "kernelspec": { |
| 282 | + "display_name": "Python 2", |
| 283 | + "language": "python", |
| 284 | + "name": "python2" |
| 285 | + }, |
| 286 | + "language_info": { |
| 287 | + "codemirror_mode": { |
| 288 | + "name": "ipython", |
| 289 | + "version": 2 |
| 290 | + }, |
| 291 | + "file_extension": ".py", |
| 292 | + "mimetype": "text/x-python", |
| 293 | + "name": "python", |
| 294 | + "nbconvert_exporter": "python", |
| 295 | + "pygments_lexer": "ipython2", |
| 296 | + "version": "2.7.10" |
| 297 | + } |
| 298 | + }, |
| 299 | + "nbformat": 4, |
| 300 | + "nbformat_minor": 0 |
| 301 | +} |
0 commit comments