Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

replace print->print() for python3.x #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
338 changes: 1 addition & 337 deletions 338 10_Random_sampling_Solutions.ipynb
Original file line number Diff line number Diff line change
@@ -1,337 +1 @@
{
"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":1,"metadata":{},"outputs":[],"source":"import numpy as np"},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"data":{"text/plain":"'1.16.5'"},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":"np.__version__"},{"cell_type":"code","execution_count":3,"metadata":{},"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":4,"metadata":{},"outputs":[{"data":{"text/plain":"array([[0.91619879, 0.84402355],\n [0.96187101, 0.63032521],\n [0.87581479, 0.49324904]])"},"execution_count":4,"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":5,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"-0.0007331580720093552\n1.0006913189352846\n"}],"source":"out1 = np.random.randn(1000, 1000)\nout2 = np.random.standard_normal((1000, 1000))\nout3 = np.random.normal(loc=0.0, scale=1.0, size=(1000, 1000))\nassert np.allclose(np.mean(out1), np.mean(out2), atol=0.1)\nassert np.allclose(np.mean(out1), np.mean(out3), atol=0.1)\nassert np.allclose(np.std(out1), np.std(out2), atol=0.1)\nassert np.allclose(np.std(out1), np.std(out3), atol=0.1)\nprint (np.mean(out3))\nprint (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":6,"metadata":{},"outputs":[{"data":{"text/plain":"array([[2, 0],\n [3, 1],\n [2, 0]])"},"execution_count":6,"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":8,"metadata":{},"outputs":[],"source":"x = [b'3 out of 10', b'5 out of 10', b'2 out of 10']"},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"b'3 out of 10'\nb'5 out of 10'\nb'3 out of 10'\nb'5 out of 10'\nb'2 out of 10'\nb'5 out of 10'\nb'2 out of 10'\nb'5 out of 10'\nb'5 out of 10'\nb'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":10,"metadata":{},"outputs":[{"data":{"text/plain":"array([5, 8, 3])"},"execution_count":10,"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":11,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[0 8 9 5 7 4 2 6 1 3]\n"}],"source":"x = np.arange(10)\nnp.random.shuffle(x)\nprint (x)"},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[9 6 3 1 5 2 0 8 7 4]\n"}],"source":"# Or\nprint (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":13,"metadata":{},"outputs":[],"source":"np.random.seed(10)"},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":""}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.