depd-v3 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ipynb_checkpoints/dfs-checkpoint.py +20 -0
- package/.ipynb_checkpoints/lab-checkpoint.ipynb +594 -0
- package/astar.py +20 -59
- package/lab.ipynb +24 -70
- package/package.json +1 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
graph = {
|
|
2
|
+
'A': ['B', 'C'],
|
|
3
|
+
'B': ['D', 'E'],
|
|
4
|
+
'C': ['F'],
|
|
5
|
+
'D': [],
|
|
6
|
+
'E': ['F'],
|
|
7
|
+
'F': []
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
visited = set()
|
|
11
|
+
|
|
12
|
+
def dfs(node):
|
|
13
|
+
if node not in visited:
|
|
14
|
+
print(node, end=" ")
|
|
15
|
+
visited.add(node)
|
|
16
|
+
|
|
17
|
+
for neighbour in graph[node]:
|
|
18
|
+
dfs(neighbour)
|
|
19
|
+
|
|
20
|
+
dfs('A')
|
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cells": [
|
|
3
|
+
{
|
|
4
|
+
"cell_type": "code",
|
|
5
|
+
"execution_count": null,
|
|
6
|
+
"id": "56190fb8",
|
|
7
|
+
"metadata": {},
|
|
8
|
+
"outputs": [
|
|
9
|
+
{
|
|
10
|
+
"name": "stdout",
|
|
11
|
+
"output_type": "stream",
|
|
12
|
+
"text": [
|
|
13
|
+
"A B C D E F "
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
],
|
|
17
|
+
"source": [
|
|
18
|
+
"graph = {\n",
|
|
19
|
+
" 'A': ['B', 'C'],\n",
|
|
20
|
+
" 'B': ['D', 'E'],\n",
|
|
21
|
+
" 'C': ['F'],\n",
|
|
22
|
+
" 'D': [],\n",
|
|
23
|
+
" 'E': ['F'],\n",
|
|
24
|
+
" 'F': []\n",
|
|
25
|
+
"}\n",
|
|
26
|
+
"\n",
|
|
27
|
+
"visited = []\n",
|
|
28
|
+
"queue = []\n",
|
|
29
|
+
"\n",
|
|
30
|
+
"def bfs(graph, node):\n",
|
|
31
|
+
" visited.append(node)\n",
|
|
32
|
+
" queue.append(node)\n",
|
|
33
|
+
"\n",
|
|
34
|
+
" while queue:\n",
|
|
35
|
+
" s = queue.pop(0)\n",
|
|
36
|
+
" print(s, end=\" \")\n",
|
|
37
|
+
"\n",
|
|
38
|
+
" for neighbour in graph[s]:\n",
|
|
39
|
+
" if neighbour not in visited:\n",
|
|
40
|
+
" visited.append(neighbour)\n",
|
|
41
|
+
" queue.append(neighbour)\n",
|
|
42
|
+
"\n",
|
|
43
|
+
"bfs(graph, 'A')"
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"cell_type": "code",
|
|
48
|
+
"execution_count": 8,
|
|
49
|
+
"id": "710d1607",
|
|
50
|
+
"metadata": {},
|
|
51
|
+
"outputs": [
|
|
52
|
+
{
|
|
53
|
+
"name": "stdout",
|
|
54
|
+
"output_type": "stream",
|
|
55
|
+
"text": [
|
|
56
|
+
"A B D E F C "
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
],
|
|
60
|
+
"source": [
|
|
61
|
+
"graph = {\n",
|
|
62
|
+
" 'A': ['B', 'C'],\n",
|
|
63
|
+
" 'B': ['D', 'E'],\n",
|
|
64
|
+
" 'C': ['F'],\n",
|
|
65
|
+
" 'D': [],\n",
|
|
66
|
+
" 'E': ['F'],\n",
|
|
67
|
+
" 'F': []\n",
|
|
68
|
+
"}\n",
|
|
69
|
+
"\n",
|
|
70
|
+
"visited = set()\n",
|
|
71
|
+
"\n",
|
|
72
|
+
"def dfs(node):\n",
|
|
73
|
+
" if node not in visited:\n",
|
|
74
|
+
" print(node, end=\" \")\n",
|
|
75
|
+
" visited.add(node)\n",
|
|
76
|
+
"\n",
|
|
77
|
+
" for neighbour in graph[node]:\n",
|
|
78
|
+
" dfs(neighbour)\n",
|
|
79
|
+
"\n",
|
|
80
|
+
"dfs('A')"
|
|
81
|
+
]
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
"cell_type": "code",
|
|
85
|
+
"execution_count": null,
|
|
86
|
+
"id": "66f639f9",
|
|
87
|
+
"metadata": {},
|
|
88
|
+
"outputs": [
|
|
89
|
+
{
|
|
90
|
+
"name": "stdout",
|
|
91
|
+
"output_type": "stream",
|
|
92
|
+
"text": [
|
|
93
|
+
"0 0\n",
|
|
94
|
+
"4 0\n",
|
|
95
|
+
"0 3\n",
|
|
96
|
+
"4 3\n",
|
|
97
|
+
"1 3\n",
|
|
98
|
+
"3 0\n",
|
|
99
|
+
"1 0\n",
|
|
100
|
+
"3 3\n",
|
|
101
|
+
"0 1\n",
|
|
102
|
+
"4 2\n",
|
|
103
|
+
"target reached\n"
|
|
104
|
+
]
|
|
105
|
+
}
|
|
106
|
+
],
|
|
107
|
+
"source": [
|
|
108
|
+
"#water jug bfs\n",
|
|
109
|
+
"from collections import deque\n",
|
|
110
|
+
"\n",
|
|
111
|
+
"def bfs(a, b, target):\n",
|
|
112
|
+
" q = deque([(0, 0)])\n",
|
|
113
|
+
" visited = set()\n",
|
|
114
|
+
"\n",
|
|
115
|
+
" while q:\n",
|
|
116
|
+
" x, y = q.popleft()\n",
|
|
117
|
+
" if (x, y) in visited:\n",
|
|
118
|
+
" continue\n",
|
|
119
|
+
"\n",
|
|
120
|
+
" visited.add((x, y))\n",
|
|
121
|
+
" print(x, y)\n",
|
|
122
|
+
"\n",
|
|
123
|
+
" if x == target or y == target:\n",
|
|
124
|
+
" print(\"target reached\")\n",
|
|
125
|
+
" break\n",
|
|
126
|
+
"\n",
|
|
127
|
+
" q.extend([\n",
|
|
128
|
+
" (a, y), (x, b), (0, y), (x, 0),\n",
|
|
129
|
+
" (x - min(x, b - y), y + min(x, b - y)),\n",
|
|
130
|
+
" (x + min(y, a - x), y - min(y, a - x))\n",
|
|
131
|
+
" ])\n",
|
|
132
|
+
"\n",
|
|
133
|
+
"bfs(4, 3, 2)"
|
|
134
|
+
]
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"cell_type": "code",
|
|
138
|
+
"execution_count": 4,
|
|
139
|
+
"id": "066c71e0",
|
|
140
|
+
"metadata": {},
|
|
141
|
+
"outputs": [
|
|
142
|
+
{
|
|
143
|
+
"name": "stdout",
|
|
144
|
+
"output_type": "stream",
|
|
145
|
+
"text": [
|
|
146
|
+
"0 0\n",
|
|
147
|
+
"0 3\n",
|
|
148
|
+
"3 0\n",
|
|
149
|
+
"3 3\n",
|
|
150
|
+
"4 2\n",
|
|
151
|
+
"target reached\n"
|
|
152
|
+
]
|
|
153
|
+
}
|
|
154
|
+
],
|
|
155
|
+
"source": [
|
|
156
|
+
"#water jug dfs\n",
|
|
157
|
+
"from collections import deque\n",
|
|
158
|
+
"\n",
|
|
159
|
+
"def dfs(a, b, target):\n",
|
|
160
|
+
" q = deque([(0, 0)])\n",
|
|
161
|
+
" visited = set()\n",
|
|
162
|
+
"\n",
|
|
163
|
+
" while q:\n",
|
|
164
|
+
" x, y = q.pop()\n",
|
|
165
|
+
" if (x, y) in visited:\n",
|
|
166
|
+
" continue\n",
|
|
167
|
+
"\n",
|
|
168
|
+
" visited.add((x, y))\n",
|
|
169
|
+
" print(x, y)\n",
|
|
170
|
+
"\n",
|
|
171
|
+
" if x == target or y == target:\n",
|
|
172
|
+
" print(\"target reached\")\n",
|
|
173
|
+
" break\n",
|
|
174
|
+
"\n",
|
|
175
|
+
" q.extend([\n",
|
|
176
|
+
" (a, y), (x, b), (0, y), (x, 0),\n",
|
|
177
|
+
" (x - min(x, b - y), y + min(x, b - y)),\n",
|
|
178
|
+
" (x + min(y, a - x), y - min(y, a - x))\n",
|
|
179
|
+
" ])\n",
|
|
180
|
+
"\n",
|
|
181
|
+
"dfs(4, 3, 2)"
|
|
182
|
+
]
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
"cell_type": "code",
|
|
186
|
+
"execution_count": 3,
|
|
187
|
+
"id": "0474fd2d",
|
|
188
|
+
"metadata": {},
|
|
189
|
+
"outputs": [
|
|
190
|
+
{
|
|
191
|
+
"name": "stdout",
|
|
192
|
+
"output_type": "stream",
|
|
193
|
+
"text": [
|
|
194
|
+
"Path found: ['A', 'F', 'G', 'I', 'J']\n"
|
|
195
|
+
]
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
"data": {
|
|
199
|
+
"text/plain": [
|
|
200
|
+
"['A', 'F', 'G', 'I', 'J']"
|
|
201
|
+
]
|
|
202
|
+
},
|
|
203
|
+
"execution_count": 3,
|
|
204
|
+
"metadata": {},
|
|
205
|
+
"output_type": "execute_result"
|
|
206
|
+
}
|
|
207
|
+
],
|
|
208
|
+
"source": [
|
|
209
|
+
"def aStarAlgo(start_node, stop_node):\n",
|
|
210
|
+
" open_set = set(start_node)\n",
|
|
211
|
+
" closed_set = set()\n",
|
|
212
|
+
" g = {} \n",
|
|
213
|
+
" parents = {} \n",
|
|
214
|
+
" g[start_node] = 0\n",
|
|
215
|
+
"\n",
|
|
216
|
+
" parents[start_node] = start_node\n",
|
|
217
|
+
"\n",
|
|
218
|
+
" while len(open_set) > 0:\n",
|
|
219
|
+
" n = None\n",
|
|
220
|
+
"\n",
|
|
221
|
+
" for v in open_set:\n",
|
|
222
|
+
" if n == None or g[v] + heuristic(v) < g[n] + heuristic(n):\n",
|
|
223
|
+
" n = v\n",
|
|
224
|
+
"\n",
|
|
225
|
+
" if n == stop_node or Graph_nodes[n] == None:\n",
|
|
226
|
+
" pass\n",
|
|
227
|
+
" else:\n",
|
|
228
|
+
" for (m, weight) in get_neighbors(n):\n",
|
|
229
|
+
" if m not in open_set and m not in closed_set:\n",
|
|
230
|
+
" open_set.add(m)\n",
|
|
231
|
+
" parents[m] = n\n",
|
|
232
|
+
" g[m] = g[n] + weight\n",
|
|
233
|
+
"\n",
|
|
234
|
+
" else:\n",
|
|
235
|
+
" if g[m] > g[n] + weight:\n",
|
|
236
|
+
" g[m] = g[n] + weight\n",
|
|
237
|
+
"\n",
|
|
238
|
+
" parents[m] = n\n",
|
|
239
|
+
"\n",
|
|
240
|
+
" if m in closed_set:\n",
|
|
241
|
+
" closed_set.remove(m)\n",
|
|
242
|
+
" open_set.add(m)\n",
|
|
243
|
+
"\n",
|
|
244
|
+
" if n == None:\n",
|
|
245
|
+
" print('Path does not exist!')\n",
|
|
246
|
+
" return None\n",
|
|
247
|
+
"\n",
|
|
248
|
+
" if n == stop_node:\n",
|
|
249
|
+
" path = []\n",
|
|
250
|
+
"\n",
|
|
251
|
+
" while parents[n] != n:\n",
|
|
252
|
+
" path.append(n)\n",
|
|
253
|
+
" n = parents[n]\n",
|
|
254
|
+
"\n",
|
|
255
|
+
" path.append(start_node)\n",
|
|
256
|
+
" path.reverse()\n",
|
|
257
|
+
"\n",
|
|
258
|
+
" print('Path found: {}'.format(path))\n",
|
|
259
|
+
" return path\n",
|
|
260
|
+
"\n",
|
|
261
|
+
" open_set.remove(n)\n",
|
|
262
|
+
" closed_set.add(n)\n",
|
|
263
|
+
"\n",
|
|
264
|
+
" print('Path does not exist!')\n",
|
|
265
|
+
" return None\n",
|
|
266
|
+
"\n",
|
|
267
|
+
"def get_neighbors(v):\n",
|
|
268
|
+
" if v in Graph_nodes:\n",
|
|
269
|
+
" return Graph_nodes[v]\n",
|
|
270
|
+
" else:\n",
|
|
271
|
+
" return None\n",
|
|
272
|
+
"\n",
|
|
273
|
+
"def heuristic(n):\n",
|
|
274
|
+
" H_dist = {\n",
|
|
275
|
+
" 'A': 11,\n",
|
|
276
|
+
" 'B': 6,\n",
|
|
277
|
+
" 'C': 5,\n",
|
|
278
|
+
" 'D': 7,\n",
|
|
279
|
+
" 'E': 3,\n",
|
|
280
|
+
" 'F': 6,\n",
|
|
281
|
+
" 'G': 5,\n",
|
|
282
|
+
" 'H': 3,\n",
|
|
283
|
+
" 'I': 1,\n",
|
|
284
|
+
" 'J': 0\n",
|
|
285
|
+
" }\n",
|
|
286
|
+
"\n",
|
|
287
|
+
" return H_dist[n]\n",
|
|
288
|
+
"\n",
|
|
289
|
+
"Graph_nodes = {\n",
|
|
290
|
+
" 'A': [('B', 6), ('F', 3)],\n",
|
|
291
|
+
" 'B': [('A', 6), ('C', 3), ('D', 2)],\n",
|
|
292
|
+
" 'C': [('B', 3), ('D', 1), ('E', 5)],\n",
|
|
293
|
+
" 'D': [('B', 2), ('C', 1), ('E', 8)],\n",
|
|
294
|
+
" 'E': [('C', 5), ('D', 8), ('I', 5), ('J', 5)],\n",
|
|
295
|
+
" 'F': [('A', 3), ('G', 1), ('H', 7)],\n",
|
|
296
|
+
" 'G': [('F', 1), ('I', 3)],\n",
|
|
297
|
+
" 'H': [('F', 7), ('I', 2)],\n",
|
|
298
|
+
" 'I': [('E', 5), ('G', 3), ('H', 2), ('J', 3)],\n",
|
|
299
|
+
"}\n",
|
|
300
|
+
"\n",
|
|
301
|
+
"aStarAlgo('A', 'J')"
|
|
302
|
+
]
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
"cell_type": "code",
|
|
306
|
+
"execution_count": null,
|
|
307
|
+
"id": "0a182fc4",
|
|
308
|
+
"metadata": {},
|
|
309
|
+
"outputs": [
|
|
310
|
+
{
|
|
311
|
+
"data": {
|
|
312
|
+
"text/plain": [
|
|
313
|
+
"0.9666666666666667"
|
|
314
|
+
]
|
|
315
|
+
},
|
|
316
|
+
"execution_count": 1,
|
|
317
|
+
"metadata": {},
|
|
318
|
+
"output_type": "execute_result"
|
|
319
|
+
}
|
|
320
|
+
],
|
|
321
|
+
"source": [
|
|
322
|
+
"#k nearest\n",
|
|
323
|
+
"from sklearn.neighbors import KNeighborsClassifier\n",
|
|
324
|
+
"from sklearn.model_selection import train_test_split\n",
|
|
325
|
+
"import pandas as pd\n",
|
|
326
|
+
"\n",
|
|
327
|
+
"sample_dataframe_df = pd.read_csv('Iris.csv')\n",
|
|
328
|
+
"\n",
|
|
329
|
+
"sample_dataframe_df.head(5)\n",
|
|
330
|
+
"sample_dataframe_df['Species'].value_counts()\n",
|
|
331
|
+
"\n",
|
|
332
|
+
"x = sample_dataframe_df.drop(['Id', 'Species'], axis=1)\n",
|
|
333
|
+
"y = sample_dataframe_df['Species']\n",
|
|
334
|
+
"\n",
|
|
335
|
+
"x.head(5)\n",
|
|
336
|
+
"y.head(5)\n",
|
|
337
|
+
"\n",
|
|
338
|
+
"train_x, test_x, train_y, test_y = train_test_split(\n",
|
|
339
|
+
" x,\n",
|
|
340
|
+
" y,\n",
|
|
341
|
+
" train_size=0.8,\n",
|
|
342
|
+
" random_state=80\n",
|
|
343
|
+
")\n",
|
|
344
|
+
"\n",
|
|
345
|
+
"knn = KNeighborsClassifier(n_neighbors=5)\n",
|
|
346
|
+
"\n",
|
|
347
|
+
"knn.fit(train_x, train_y)\n",
|
|
348
|
+
"\n",
|
|
349
|
+
"knn.score(test_x, test_y)"
|
|
350
|
+
]
|
|
351
|
+
},
|
|
352
|
+
{
|
|
353
|
+
"cell_type": "code",
|
|
354
|
+
"execution_count": 9,
|
|
355
|
+
"id": "9a277da3",
|
|
356
|
+
"metadata": {},
|
|
357
|
+
"outputs": [
|
|
358
|
+
{
|
|
359
|
+
"name": "stdout",
|
|
360
|
+
"output_type": "stream",
|
|
361
|
+
"text": [
|
|
362
|
+
"[0 0 1 0 0 1 1 1 0 0 0 1 0 1 1 0 1 0 0 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 1\n",
|
|
363
|
+
" 0 0 0]\n",
|
|
364
|
+
"0.75\n"
|
|
365
|
+
]
|
|
366
|
+
}
|
|
367
|
+
],
|
|
368
|
+
"source": [
|
|
369
|
+
"from sklearn.model_selection import train_test_split\n",
|
|
370
|
+
"from sklearn.tree import DecisionTreeClassifier\n",
|
|
371
|
+
"from sklearn.metrics import accuracy_score\n",
|
|
372
|
+
"\n",
|
|
373
|
+
"import pandas as pd\n",
|
|
374
|
+
"\n",
|
|
375
|
+
"sample_dataframe_df = pd.read_csv('Loan_Repay_Dataset.csv')\n",
|
|
376
|
+
"\n",
|
|
377
|
+
"x = sample_dataframe_df.drop(['Result'], axis=1)\n",
|
|
378
|
+
"y = sample_dataframe_df['Result']\n",
|
|
379
|
+
"\n",
|
|
380
|
+
"train_x, test_x, train_y, test_y = train_test_split(\n",
|
|
381
|
+
" x, y, test_size=0.2, random_state=100\n",
|
|
382
|
+
")\n",
|
|
383
|
+
"\n",
|
|
384
|
+
"estimated = DecisionTreeClassifier().fit(train_x, train_y)\n",
|
|
385
|
+
"\n",
|
|
386
|
+
"predict_y = estimated.predict(test_x)\n",
|
|
387
|
+
"\n",
|
|
388
|
+
"print(predict_y)\n",
|
|
389
|
+
"\n",
|
|
390
|
+
"print(accuracy_score(test_y, predict_y))"
|
|
391
|
+
]
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
"cell_type": "code",
|
|
395
|
+
"execution_count": null,
|
|
396
|
+
"id": "d51aef68",
|
|
397
|
+
"metadata": {},
|
|
398
|
+
"outputs": [
|
|
399
|
+
{
|
|
400
|
+
"data": {
|
|
401
|
+
"text/html": [
|
|
402
|
+
"<div>\n",
|
|
403
|
+
"<style scoped>\n",
|
|
404
|
+
" .dataframe tbody tr th:only-of-type {\n",
|
|
405
|
+
" vertical-align: middle;\n",
|
|
406
|
+
" }\n",
|
|
407
|
+
"\n",
|
|
408
|
+
" .dataframe tbody tr th {\n",
|
|
409
|
+
" vertical-align: top;\n",
|
|
410
|
+
" }\n",
|
|
411
|
+
"\n",
|
|
412
|
+
" .dataframe thead th {\n",
|
|
413
|
+
" text-align: right;\n",
|
|
414
|
+
" }\n",
|
|
415
|
+
"</style>\n",
|
|
416
|
+
"<table border=\"1\" class=\"dataframe\">\n",
|
|
417
|
+
" <thead>\n",
|
|
418
|
+
" <tr style=\"text-align: right;\">\n",
|
|
419
|
+
" <th></th>\n",
|
|
420
|
+
" <th>Id</th>\n",
|
|
421
|
+
" <th>SepalLengthCm</th>\n",
|
|
422
|
+
" <th>SepalWidthCm</th>\n",
|
|
423
|
+
" <th>PetalLengthCm</th>\n",
|
|
424
|
+
" <th>PetalWidthCm</th>\n",
|
|
425
|
+
" <th>Species</th>\n",
|
|
426
|
+
" </tr>\n",
|
|
427
|
+
" </thead>\n",
|
|
428
|
+
" <tbody>\n",
|
|
429
|
+
" <tr>\n",
|
|
430
|
+
" <th>105</th>\n",
|
|
431
|
+
" <td>106</td>\n",
|
|
432
|
+
" <td>7.6</td>\n",
|
|
433
|
+
" <td>3.0</td>\n",
|
|
434
|
+
" <td>6.6</td>\n",
|
|
435
|
+
" <td>2.1</td>\n",
|
|
436
|
+
" <td>Iris-virginica</td>\n",
|
|
437
|
+
" </tr>\n",
|
|
438
|
+
" <tr>\n",
|
|
439
|
+
" <th>107</th>\n",
|
|
440
|
+
" <td>108</td>\n",
|
|
441
|
+
" <td>7.3</td>\n",
|
|
442
|
+
" <td>2.9</td>\n",
|
|
443
|
+
" <td>6.3</td>\n",
|
|
444
|
+
" <td>1.8</td>\n",
|
|
445
|
+
" <td>Iris-virginica</td>\n",
|
|
446
|
+
" </tr>\n",
|
|
447
|
+
" <tr>\n",
|
|
448
|
+
" <th>109</th>\n",
|
|
449
|
+
" <td>110</td>\n",
|
|
450
|
+
" <td>7.2</td>\n",
|
|
451
|
+
" <td>3.6</td>\n",
|
|
452
|
+
" <td>6.1</td>\n",
|
|
453
|
+
" <td>2.5</td>\n",
|
|
454
|
+
" <td>Iris-virginica</td>\n",
|
|
455
|
+
" </tr>\n",
|
|
456
|
+
" <tr>\n",
|
|
457
|
+
" <th>117</th>\n",
|
|
458
|
+
" <td>118</td>\n",
|
|
459
|
+
" <td>7.7</td>\n",
|
|
460
|
+
" <td>3.8</td>\n",
|
|
461
|
+
" <td>6.7</td>\n",
|
|
462
|
+
" <td>2.2</td>\n",
|
|
463
|
+
" <td>Iris-virginica</td>\n",
|
|
464
|
+
" </tr>\n",
|
|
465
|
+
" <tr>\n",
|
|
466
|
+
" <th>118</th>\n",
|
|
467
|
+
" <td>119</td>\n",
|
|
468
|
+
" <td>7.7</td>\n",
|
|
469
|
+
" <td>2.6</td>\n",
|
|
470
|
+
" <td>6.9</td>\n",
|
|
471
|
+
" <td>2.3</td>\n",
|
|
472
|
+
" <td>Iris-virginica</td>\n",
|
|
473
|
+
" </tr>\n",
|
|
474
|
+
" <tr>\n",
|
|
475
|
+
" <th>122</th>\n",
|
|
476
|
+
" <td>123</td>\n",
|
|
477
|
+
" <td>7.7</td>\n",
|
|
478
|
+
" <td>2.8</td>\n",
|
|
479
|
+
" <td>6.7</td>\n",
|
|
480
|
+
" <td>2.0</td>\n",
|
|
481
|
+
" <td>Iris-virginica</td>\n",
|
|
482
|
+
" </tr>\n",
|
|
483
|
+
" <tr>\n",
|
|
484
|
+
" <th>130</th>\n",
|
|
485
|
+
" <td>131</td>\n",
|
|
486
|
+
" <td>7.4</td>\n",
|
|
487
|
+
" <td>2.8</td>\n",
|
|
488
|
+
" <td>6.1</td>\n",
|
|
489
|
+
" <td>1.9</td>\n",
|
|
490
|
+
" <td>Iris-virginica</td>\n",
|
|
491
|
+
" </tr>\n",
|
|
492
|
+
" <tr>\n",
|
|
493
|
+
" <th>131</th>\n",
|
|
494
|
+
" <td>132</td>\n",
|
|
495
|
+
" <td>7.9</td>\n",
|
|
496
|
+
" <td>3.8</td>\n",
|
|
497
|
+
" <td>6.4</td>\n",
|
|
498
|
+
" <td>2.0</td>\n",
|
|
499
|
+
" <td>Iris-virginica</td>\n",
|
|
500
|
+
" </tr>\n",
|
|
501
|
+
" <tr>\n",
|
|
502
|
+
" <th>135</th>\n",
|
|
503
|
+
" <td>136</td>\n",
|
|
504
|
+
" <td>7.7</td>\n",
|
|
505
|
+
" <td>3.0</td>\n",
|
|
506
|
+
" <td>6.1</td>\n",
|
|
507
|
+
" <td>2.3</td>\n",
|
|
508
|
+
" <td>Iris-virginica</td>\n",
|
|
509
|
+
" </tr>\n",
|
|
510
|
+
" </tbody>\n",
|
|
511
|
+
"</table>\n",
|
|
512
|
+
"</div>"
|
|
513
|
+
],
|
|
514
|
+
"text/plain": [
|
|
515
|
+
" Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm \\\n",
|
|
516
|
+
"105 106 7.6 3.0 6.6 2.1 \n",
|
|
517
|
+
"107 108 7.3 2.9 6.3 1.8 \n",
|
|
518
|
+
"109 110 7.2 3.6 6.1 2.5 \n",
|
|
519
|
+
"117 118 7.7 3.8 6.7 2.2 \n",
|
|
520
|
+
"118 119 7.7 2.6 6.9 2.3 \n",
|
|
521
|
+
"122 123 7.7 2.8 6.7 2.0 \n",
|
|
522
|
+
"130 131 7.4 2.8 6.1 1.9 \n",
|
|
523
|
+
"131 132 7.9 3.8 6.4 2.0 \n",
|
|
524
|
+
"135 136 7.7 3.0 6.1 2.3 \n",
|
|
525
|
+
"\n",
|
|
526
|
+
" Species \n",
|
|
527
|
+
"105 Iris-virginica \n",
|
|
528
|
+
"107 Iris-virginica \n",
|
|
529
|
+
"109 Iris-virginica \n",
|
|
530
|
+
"117 Iris-virginica \n",
|
|
531
|
+
"118 Iris-virginica \n",
|
|
532
|
+
"122 Iris-virginica \n",
|
|
533
|
+
"130 Iris-virginica \n",
|
|
534
|
+
"131 Iris-virginica \n",
|
|
535
|
+
"135 Iris-virginica "
|
|
536
|
+
]
|
|
537
|
+
},
|
|
538
|
+
"execution_count": 11,
|
|
539
|
+
"metadata": {},
|
|
540
|
+
"output_type": "execute_result"
|
|
541
|
+
}
|
|
542
|
+
],
|
|
543
|
+
"source": [
|
|
544
|
+
"import pandas as pd\n",
|
|
545
|
+
"\n",
|
|
546
|
+
"sample_dataframe_df = pd.read_csv('Iris.csv')\n",
|
|
547
|
+
"\n",
|
|
548
|
+
"type(sample_dataframe_df)\n",
|
|
549
|
+
"\n",
|
|
550
|
+
"sample_dataframe_df.head(5)\n",
|
|
551
|
+
"\n",
|
|
552
|
+
"list(sample_dataframe_df.columns)\n",
|
|
553
|
+
"\n",
|
|
554
|
+
"sample_dataframe_df.head(5).transpose()\n",
|
|
555
|
+
"\n",
|
|
556
|
+
"sample_dataframe_df.shape\n",
|
|
557
|
+
"\n",
|
|
558
|
+
"sample_dataframe_df[0:10]\n",
|
|
559
|
+
"\n",
|
|
560
|
+
"sample_dataframe_df[-5:]\n",
|
|
561
|
+
"\n",
|
|
562
|
+
"sample_dataframe_df['SepalLengthCm']\n",
|
|
563
|
+
"\n",
|
|
564
|
+
"sample_dataframe_df.SepalLengthCm.value_count\n",
|
|
565
|
+
"\n",
|
|
566
|
+
"sample_dataframe_df[sample_dataframe_df['PetalLengthCm']>6]\n",
|
|
567
|
+
"\n",
|
|
568
|
+
"sample_dataframe_df.drop('Si.no', inplace=True, axis=1)\n",
|
|
569
|
+
"list(sample_dataframe_df.columns)"
|
|
570
|
+
]
|
|
571
|
+
}
|
|
572
|
+
],
|
|
573
|
+
"metadata": {
|
|
574
|
+
"kernelspec": {
|
|
575
|
+
"display_name": "Python 3 (ipykernel)",
|
|
576
|
+
"language": "python",
|
|
577
|
+
"name": "python3"
|
|
578
|
+
},
|
|
579
|
+
"language_info": {
|
|
580
|
+
"codemirror_mode": {
|
|
581
|
+
"name": "ipython",
|
|
582
|
+
"version": 3
|
|
583
|
+
},
|
|
584
|
+
"file_extension": ".py",
|
|
585
|
+
"mimetype": "text/x-python",
|
|
586
|
+
"name": "python",
|
|
587
|
+
"nbconvert_exporter": "python",
|
|
588
|
+
"pygments_lexer": "ipython3",
|
|
589
|
+
"version": "3.14.6"
|
|
590
|
+
}
|
|
591
|
+
},
|
|
592
|
+
"nbformat": 4,
|
|
593
|
+
"nbformat_minor": 5
|
|
594
|
+
}
|
package/astar.py
CHANGED
|
@@ -1,66 +1,27 @@
|
|
|
1
|
-
def
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
g = {}
|
|
5
|
-
parents = {}
|
|
6
|
-
g[start_node] = 0
|
|
1
|
+
def astar(start, goal):
|
|
2
|
+
open = [start]
|
|
3
|
+
parent = {start: None}
|
|
4
|
+
g = {start: 0}
|
|
7
5
|
|
|
8
|
-
|
|
6
|
+
while open:
|
|
7
|
+
node = min(open, key=lambda x: g[x] + heuristic(x))
|
|
8
|
+
open.remove(node)
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
n = None
|
|
12
|
-
|
|
13
|
-
for v in open_set:
|
|
14
|
-
if n == None or g[v] + heuristic(v) < g[n] + heuristic(n):
|
|
15
|
-
n = v
|
|
16
|
-
|
|
17
|
-
if n == stop_node or Graph_nodes[n] == None:
|
|
18
|
-
pass
|
|
19
|
-
else:
|
|
20
|
-
for (m, weight) in get_neighbors(n):
|
|
21
|
-
if m not in open_set and m not in closed_set:
|
|
22
|
-
open_set.add(m)
|
|
23
|
-
parents[m] = n
|
|
24
|
-
g[m] = g[n] + weight
|
|
25
|
-
|
|
26
|
-
else:
|
|
27
|
-
if g[m] > g[n] + weight:
|
|
28
|
-
g[m] = g[n] + weight
|
|
29
|
-
|
|
30
|
-
parents[m] = n
|
|
31
|
-
|
|
32
|
-
if m in closed_set:
|
|
33
|
-
closed_set.remove(m)
|
|
34
|
-
open_set.add(m)
|
|
35
|
-
|
|
36
|
-
if n == None:
|
|
37
|
-
print('Path does not exist!')
|
|
38
|
-
return None
|
|
39
|
-
|
|
40
|
-
if n == stop_node:
|
|
10
|
+
if node == goal:
|
|
41
11
|
path = []
|
|
12
|
+
while node:
|
|
13
|
+
path.append(node)
|
|
14
|
+
node = parent[node]
|
|
15
|
+
return path[::-1]
|
|
42
16
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
n = parents[n]
|
|
46
|
-
|
|
47
|
-
path.append(start_node)
|
|
48
|
-
path.reverse()
|
|
49
|
-
|
|
50
|
-
print('Path found: {}'.format(path))
|
|
51
|
-
return path
|
|
52
|
-
|
|
53
|
-
open_set.remove(n)
|
|
54
|
-
closed_set.add(n)
|
|
55
|
-
|
|
56
|
-
print('Path does not exist!')
|
|
57
|
-
return None
|
|
17
|
+
for nxt, w in Graph_nodes[node]:
|
|
18
|
+
new_g = g[node] + w
|
|
58
19
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
20
|
+
if nxt not in g or new_g < g[nxt]:
|
|
21
|
+
g[nxt] = new_g
|
|
22
|
+
parent[nxt] = node
|
|
23
|
+
if nxt not in open:
|
|
24
|
+
open.append(nxt)
|
|
64
25
|
|
|
65
26
|
def heuristic(n):
|
|
66
27
|
H_dist = {
|
|
@@ -90,4 +51,4 @@ Graph_nodes = {
|
|
|
90
51
|
'I': [('E', 5), ('G', 3), ('H', 2), ('J', 3)],
|
|
91
52
|
}
|
|
92
53
|
|
|
93
|
-
|
|
54
|
+
astar('A', 'J')
|
package/lab.ipynb
CHANGED
|
@@ -183,92 +183,46 @@
|
|
|
183
183
|
},
|
|
184
184
|
{
|
|
185
185
|
"cell_type": "code",
|
|
186
|
-
"execution_count":
|
|
186
|
+
"execution_count": null,
|
|
187
187
|
"id": "0474fd2d",
|
|
188
188
|
"metadata": {},
|
|
189
189
|
"outputs": [
|
|
190
|
-
{
|
|
191
|
-
"name": "stdout",
|
|
192
|
-
"output_type": "stream",
|
|
193
|
-
"text": [
|
|
194
|
-
"Path found: ['A', 'F', 'G', 'I', 'J']\n"
|
|
195
|
-
]
|
|
196
|
-
},
|
|
197
190
|
{
|
|
198
191
|
"data": {
|
|
199
192
|
"text/plain": [
|
|
200
193
|
"['A', 'F', 'G', 'I', 'J']"
|
|
201
194
|
]
|
|
202
195
|
},
|
|
203
|
-
"execution_count":
|
|
196
|
+
"execution_count": 13,
|
|
204
197
|
"metadata": {},
|
|
205
198
|
"output_type": "execute_result"
|
|
206
199
|
}
|
|
207
200
|
],
|
|
208
201
|
"source": [
|
|
209
|
-
"def
|
|
210
|
-
"
|
|
211
|
-
"
|
|
212
|
-
" g = {}
|
|
213
|
-
" parents = {} \n",
|
|
214
|
-
" g[start_node] = 0\n",
|
|
215
|
-
"\n",
|
|
216
|
-
" parents[start_node] = start_node\n",
|
|
217
|
-
"\n",
|
|
218
|
-
" while len(open_set) > 0:\n",
|
|
219
|
-
" n = None\n",
|
|
220
|
-
"\n",
|
|
221
|
-
" for v in open_set:\n",
|
|
222
|
-
" if n == None or g[v] + heuristic(v) < g[n] + heuristic(n):\n",
|
|
223
|
-
" n = v\n",
|
|
224
|
-
"\n",
|
|
225
|
-
" if n == stop_node or Graph_nodes[n] == None:\n",
|
|
226
|
-
" pass\n",
|
|
227
|
-
" else:\n",
|
|
228
|
-
" for (m, weight) in get_neighbors(n):\n",
|
|
229
|
-
" if m not in open_set and m not in closed_set:\n",
|
|
230
|
-
" open_set.add(m)\n",
|
|
231
|
-
" parents[m] = n\n",
|
|
232
|
-
" g[m] = g[n] + weight\n",
|
|
233
|
-
"\n",
|
|
234
|
-
" else:\n",
|
|
235
|
-
" if g[m] > g[n] + weight:\n",
|
|
236
|
-
" g[m] = g[n] + weight\n",
|
|
237
|
-
"\n",
|
|
238
|
-
" parents[m] = n\n",
|
|
239
|
-
"\n",
|
|
240
|
-
" if m in closed_set:\n",
|
|
241
|
-
" closed_set.remove(m)\n",
|
|
242
|
-
" open_set.add(m)\n",
|
|
243
|
-
"\n",
|
|
244
|
-
" if n == None:\n",
|
|
245
|
-
" print('Path does not exist!')\n",
|
|
246
|
-
" return None\n",
|
|
247
|
-
"\n",
|
|
248
|
-
" if n == stop_node:\n",
|
|
249
|
-
" path = []\n",
|
|
250
|
-
"\n",
|
|
251
|
-
" while parents[n] != n:\n",
|
|
252
|
-
" path.append(n)\n",
|
|
253
|
-
" n = parents[n]\n",
|
|
202
|
+
"def astar(start, goal):\n",
|
|
203
|
+
" open = [start]\n",
|
|
204
|
+
" parent = {start: None}\n",
|
|
205
|
+
" g = {start: 0}\n",
|
|
254
206
|
"\n",
|
|
255
|
-
"
|
|
256
|
-
"
|
|
207
|
+
" while open:\n",
|
|
208
|
+
" node = min(open, key=lambda x: g[x] + heuristic(x))\n",
|
|
209
|
+
" open.remove(node)\n",
|
|
257
210
|
"\n",
|
|
258
|
-
"
|
|
259
|
-
"
|
|
260
|
-
"
|
|
261
|
-
"
|
|
262
|
-
"
|
|
211
|
+
" if node == goal:\n",
|
|
212
|
+
" path = []\n",
|
|
213
|
+
" while node:\n",
|
|
214
|
+
" path.append(node)\n",
|
|
215
|
+
" node = parent[node]\n",
|
|
216
|
+
" return path[::-1]\n",
|
|
263
217
|
"\n",
|
|
264
|
-
"
|
|
265
|
-
"
|
|
218
|
+
" for nxt, w in Graph_nodes[node]:\n",
|
|
219
|
+
" new_g = g[node] + w\n",
|
|
266
220
|
"\n",
|
|
267
|
-
"
|
|
268
|
-
"
|
|
269
|
-
"
|
|
270
|
-
"
|
|
271
|
-
"
|
|
221
|
+
" if nxt not in g or new_g < g[nxt]:\n",
|
|
222
|
+
" g[nxt] = new_g\n",
|
|
223
|
+
" parent[nxt] = node\n",
|
|
224
|
+
" if nxt not in open:\n",
|
|
225
|
+
" open.append(nxt)\n",
|
|
272
226
|
"\n",
|
|
273
227
|
"def heuristic(n):\n",
|
|
274
228
|
" H_dist = {\n",
|
|
@@ -298,7 +252,7 @@
|
|
|
298
252
|
" 'I': [('E', 5), ('G', 3), ('H', 2), ('J', 3)],\n",
|
|
299
253
|
"}\n",
|
|
300
254
|
"\n",
|
|
301
|
-
"
|
|
255
|
+
"astar('A', 'J')"
|
|
302
256
|
]
|
|
303
257
|
},
|
|
304
258
|
{
|
|
@@ -572,7 +526,7 @@
|
|
|
572
526
|
],
|
|
573
527
|
"metadata": {
|
|
574
528
|
"kernelspec": {
|
|
575
|
-
"display_name": "
|
|
529
|
+
"display_name": "Python 3 (ipykernel)",
|
|
576
530
|
"language": "python",
|
|
577
531
|
"name": "python3"
|
|
578
532
|
},
|