From 66db4faa2e0657ce87335fce81bbf65cac2faa69 Mon Sep 17 00:00:00 2001 From: agentmarketbot Date: Sun, 26 Jan 2025 22:55:56 +0000 Subject: [PATCH] Update print statements for Python 3 compatibility Update print function calls in Jupyter notebook from Python 2 style to Python 3 style with parentheses. This change maintains compatibility while keeping the notebook's functionality intact. No logic changes were made, only syntax updates for print statements. --- 10_Random_sampling_Solutions.ipynb | 674 ++++++++++++++--------------- 1 file changed, 337 insertions(+), 337 deletions(-) diff --git a/10_Random_sampling_Solutions.ipynb b/10_Random_sampling_Solutions.ipynb index 1cc4b87..5c6b183 100644 --- a/10_Random_sampling_Solutions.ipynb +++ b/10_Random_sampling_Solutions.ipynb @@ -1,337 +1,337 @@ -{ - "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": [ - "np.random.rand(3, 2) \n", - "# Or np.random.random((3,2))" - ] - }, - { - "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": [ - "out1 = np.random.randn(1000, 1000)\n", - "out2 = np.random.standard_normal((1000, 1000))\n", - "out3 = np.random.normal(loc=0.0, scale=1.0, size=(1000, 1000))\n", - "assert np.allclose(np.mean(out1), np.mean(out2), atol=0.1)\n", - "assert np.allclose(np.mean(out1), np.mean(out3), atol=0.1)\n", - "assert np.allclose(np.std(out1), np.std(out2), atol=0.1)\n", - "assert np.allclose(np.std(out1), np.std(out3), atol=0.1)\n", - "print np.mean(out3)\n", - "print np.std(out1)\n" - ] - }, - { - "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": [ - "np.random.randint(0, 4, (3, 2))" - ] - }, - { - "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": 58, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "x = [b'3 out of 10', b'5 out of 10', b'2 out of 10']" - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2 out of 10\n", - "5 out of 10\n", - "3 out of 10\n", - "5 out of 10\n", - "5 out of 10\n", - "5 out of 10\n", - "2 out of 10\n", - "2 out of 10\n", - "5 out of 10\n", - "5 out of 10\n" - ] - } - ], - "source": [ - "for _ in range(10):\n", - " print np.random.choice(x, p=[.3, .5, .2])" - ] - }, - { - "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": [ - "np.random.choice(10, 3, replace=False)" - ] - }, - { - "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": [ - "x = np.arange(10)\n", - "np.random.shuffle(x)\n", - "print x" - ] - }, - { - "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", - "print np.random.permutation(10)" - ] - }, - { - "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": [ - "np.random.seed(10)" - ] - } - ], - "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 -} +{ + "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": [ + "np.random.rand(3, 2) \n", + "# Or np.random.random((3,2))" + ] + }, + { + "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": [ + "out1 = np.random.randn(1000, 1000)\n", + "out2 = np.random.standard_normal((1000, 1000))\n", + "out3 = np.random.normal(loc=0.0, scale=1.0, size=(1000, 1000))\n", + "assert np.allclose(np.mean(out1), np.mean(out2), atol=0.1)\n", + "assert np.allclose(np.mean(out1), np.mean(out3), atol=0.1)\n", + "assert np.allclose(np.std(out1), np.std(out2), atol=0.1)\n", + "assert np.allclose(np.std(out1), np.std(out3), atol=0.1)\n", + "print(np.mean(out3))\n", + "print(np.std(out1))\n" + ] + }, + { + "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": [ + "np.random.randint(0, 4, (3, 2))" + ] + }, + { + "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": 58, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "x = [b'3 out of 10', b'5 out of 10', b'2 out of 10']" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 out of 10\n", + "5 out of 10\n", + "3 out of 10\n", + "5 out of 10\n", + "5 out of 10\n", + "5 out of 10\n", + "2 out of 10\n", + "2 out of 10\n", + "5 out of 10\n", + "5 out of 10\n" + ] + } + ], + "source": [ + "for _ in range(10):\n", + " print(np.random.choice(x, p=[.3, .5, .2]))" + ] + }, + { + "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": [ + "np.random.choice(10, 3, replace=False)" + ] + }, + { + "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": [ + "x = np.arange(10)\n", + "np.random.shuffle(x)\n", + "print(x)" + ] + }, + { + "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", + "print(np.random.permutation(10))" + ] + }, + { + "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": [ + "np.random.seed(10)" + ] + } + ], + "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 +}