Skip to content

Commit 42ed2b2

Browse files
authored
Add files via upload
1 parent ec03273 commit 42ed2b2

File tree

1 file changed

+328
-0
lines changed

1 file changed

+328
-0
lines changed

Random_sampling.ipynb

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

0 commit comments

Comments
 (0)