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

Commit 38b5300

Browse filesBrowse files
authored
Add files via upload
1 parent 2718c16 commit 38b5300
Copy full SHA for 38b5300

File tree

Expand file treeCollapse file tree

2 files changed

+547
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+547
-0
lines changed

‎Set_routines.ipynb

Copy file name to clipboard
+270Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Set routines"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 4,
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": 5,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [
28+
{
29+
"data": {
30+
"text/plain": [
31+
"'1.11.2'"
32+
]
33+
},
34+
"execution_count": 5,
35+
"metadata": {},
36+
"output_type": "execute_result"
37+
}
38+
],
39+
"source": [
40+
"np.__version__"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 6,
46+
"metadata": {
47+
"collapsed": false
48+
},
49+
"outputs": [],
50+
"source": [
51+
"author = 'kyubyong. longinglove@nate.com'"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"## Making proper sets"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"Q1. Get unique elements and reconstruction indices from x. And reconstruct x."
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 15,
71+
"metadata": {
72+
"collapsed": false
73+
},
74+
"outputs": [
75+
{
76+
"name": "stdout",
77+
"output_type": "stream",
78+
"text": [
79+
"unique elements = [1 2 3 4 6]\n",
80+
"reconstruction indices = [0 1 4 3 1 2 1]\n",
81+
"reconstructed = [1 2 6 4 2 3 2]\n"
82+
]
83+
}
84+
],
85+
"source": [
86+
"x = np.array([1, 2, 6, 4, 2, 3, 2])\n",
87+
"\n"
88+
]
89+
},
90+
{
91+
"cell_type": "markdown",
92+
"metadata": {},
93+
"source": [
94+
"## Boolean operations"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {},
100+
"source": [
101+
"Q2. Create a boolean array of the same shape as x. If each element of x is present in y, the result will be True, otherwise False."
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": 19,
107+
"metadata": {
108+
"collapsed": false
109+
},
110+
"outputs": [
111+
{
112+
"name": "stdout",
113+
"output_type": "stream",
114+
"text": [
115+
"[ True True False False True]\n"
116+
]
117+
}
118+
],
119+
"source": [
120+
"x = np.array([0, 1, 2, 5, 0])\n",
121+
"y = np.array([0, 1])\n"
122+
]
123+
},
124+
{
125+
"cell_type": "markdown",
126+
"metadata": {},
127+
"source": [
128+
"Q3. Find the unique intersection of x and y."
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": 20,
134+
"metadata": {
135+
"collapsed": false
136+
},
137+
"outputs": [
138+
{
139+
"name": "stdout",
140+
"output_type": "stream",
141+
"text": [
142+
"[0 1]\n"
143+
]
144+
}
145+
],
146+
"source": [
147+
"x = np.array([0, 1, 2, 5, 0])\n",
148+
"y = np.array([0, 1, 4])\n"
149+
]
150+
},
151+
{
152+
"cell_type": "markdown",
153+
"metadata": {},
154+
"source": [
155+
"Q4. Find the unique elements of x that are not present in y."
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": 21,
161+
"metadata": {
162+
"collapsed": false
163+
},
164+
"outputs": [
165+
{
166+
"name": "stdout",
167+
"output_type": "stream",
168+
"text": [
169+
"[2 5]\n"
170+
]
171+
}
172+
],
173+
"source": [
174+
"x = np.array([0, 1, 2, 5, 0])\n",
175+
"y = np.array([0, 1, 4])\n"
176+
]
177+
},
178+
{
179+
"cell_type": "markdown",
180+
"metadata": {},
181+
"source": [
182+
"Q5. Find the xor elements of x and y."
183+
]
184+
},
185+
{
186+
"cell_type": "code",
187+
"execution_count": 40,
188+
"metadata": {
189+
"collapsed": false
190+
},
191+
"outputs": [
192+
{
193+
"name": "stdout",
194+
"output_type": "stream",
195+
"text": [
196+
"[2 4 5]\n"
197+
]
198+
}
199+
],
200+
"source": [
201+
"x = np.array([0, 1, 2, 5, 0])\n",
202+
"y = np.array([0, 1, 4])\n",
203+
"out1 = np.setxor1d(x, y)\n",
204+
"out2 = np.sort(np.concatenate((np.setdiff1d(x, y), np.setdiff1d(y, x))))\n",
205+
"assert np.allclose(out1, out2)\n",
206+
"\n"
207+
]
208+
},
209+
{
210+
"cell_type": "markdown",
211+
"metadata": {},
212+
"source": [
213+
"Q6. Find the union of x and y."
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": 42,
219+
"metadata": {
220+
"collapsed": false
221+
},
222+
"outputs": [
223+
{
224+
"name": "stdout",
225+
"output_type": "stream",
226+
"text": [
227+
"[0 1 2 4 5]\n"
228+
]
229+
}
230+
],
231+
"source": [
232+
"x = np.array([0, 1, 2, 5, 0])\n",
233+
"y = np.array([0, 1, 4])\n",
234+
"out1 = np.union1d(x, y)\n",
235+
"out2 = np.sort(np.unique(np.concatenate((x, y))))\n",
236+
"assert np.allclose(out1, out2)\n"
237+
]
238+
},
239+
{
240+
"cell_type": "code",
241+
"execution_count": null,
242+
"metadata": {
243+
"collapsed": true
244+
},
245+
"outputs": [],
246+
"source": []
247+
}
248+
],
249+
"metadata": {
250+
"kernelspec": {
251+
"display_name": "Python 2",
252+
"language": "python",
253+
"name": "python2"
254+
},
255+
"language_info": {
256+
"codemirror_mode": {
257+
"name": "ipython",
258+
"version": 2
259+
},
260+
"file_extension": ".py",
261+
"mimetype": "text/x-python",
262+
"name": "python",
263+
"nbconvert_exporter": "python",
264+
"pygments_lexer": "ipython2",
265+
"version": "2.7.10"
266+
}
267+
},
268+
"nbformat": 4,
269+
"nbformat_minor": 0
270+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.