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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "f66418f6",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "from sklearn.model_selection import train_test_split\n",
11
+ "from sklearn.tree import DecisionTreeClassifier\n",
12
+ "from sklearn.metrics import accuracy_score\n",
13
+ "import pandas as pd\n",
14
+ "\n"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 2,
20
+ "id": "f5db6619",
21
+ "metadata": {},
22
+ "outputs": [
23
+ {
24
+ "data": {
25
+ "text/html": [
26
+ "<div>\n",
27
+ "<style scoped>\n",
28
+ " .dataframe tbody tr th:only-of-type {\n",
29
+ " vertical-align: middle;\n",
30
+ " }\n",
31
+ "\n",
32
+ " .dataframe tbody tr th {\n",
33
+ " vertical-align: top;\n",
34
+ " }\n",
35
+ "\n",
36
+ " .dataframe thead th {\n",
37
+ " text-align: right;\n",
38
+ " }\n",
39
+ "</style>\n",
40
+ "<table border=\"1\" class=\"dataframe\">\n",
41
+ " <thead>\n",
42
+ " <tr style=\"text-align: right;\">\n",
43
+ " <th></th>\n",
44
+ " <th>PassengerId</th>\n",
45
+ " <th>Survived</th>\n",
46
+ " <th>Pclass</th>\n",
47
+ " <th>Name</th>\n",
48
+ " <th>Sex</th>\n",
49
+ " <th>Age</th>\n",
50
+ " <th>SibSp</th>\n",
51
+ " <th>Parch</th>\n",
52
+ " <th>Ticket</th>\n",
53
+ " <th>Fare</th>\n",
54
+ " <th>Cabin</th>\n",
55
+ " <th>Embarked</th>\n",
56
+ " </tr>\n",
57
+ " </thead>\n",
58
+ " <tbody>\n",
59
+ " <tr>\n",
60
+ " <th>0</th>\n",
61
+ " <td>1</td>\n",
62
+ " <td>0</td>\n",
63
+ " <td>3</td>\n",
64
+ " <td>Braund, Mr. Owen Harris</td>\n",
65
+ " <td>male</td>\n",
66
+ " <td>22.0</td>\n",
67
+ " <td>1</td>\n",
68
+ " <td>0</td>\n",
69
+ " <td>A/5 21171</td>\n",
70
+ " <td>7.2500</td>\n",
71
+ " <td>NaN</td>\n",
72
+ " <td>S</td>\n",
73
+ " </tr>\n",
74
+ " <tr>\n",
75
+ " <th>1</th>\n",
76
+ " <td>2</td>\n",
77
+ " <td>1</td>\n",
78
+ " <td>1</td>\n",
79
+ " <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
80
+ " <td>female</td>\n",
81
+ " <td>38.0</td>\n",
82
+ " <td>1</td>\n",
83
+ " <td>0</td>\n",
84
+ " <td>PC 17599</td>\n",
85
+ " <td>71.2833</td>\n",
86
+ " <td>C85</td>\n",
87
+ " <td>C</td>\n",
88
+ " </tr>\n",
89
+ " <tr>\n",
90
+ " <th>2</th>\n",
91
+ " <td>3</td>\n",
92
+ " <td>1</td>\n",
93
+ " <td>3</td>\n",
94
+ " <td>Heikkinen, Miss. Laina</td>\n",
95
+ " <td>female</td>\n",
96
+ " <td>26.0</td>\n",
97
+ " <td>0</td>\n",
98
+ " <td>0</td>\n",
99
+ " <td>STON/O2. 3101282</td>\n",
100
+ " <td>7.9250</td>\n",
101
+ " <td>NaN</td>\n",
102
+ " <td>S</td>\n",
103
+ " </tr>\n",
104
+ " <tr>\n",
105
+ " <th>3</th>\n",
106
+ " <td>4</td>\n",
107
+ " <td>1</td>\n",
108
+ " <td>1</td>\n",
109
+ " <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
110
+ " <td>female</td>\n",
111
+ " <td>35.0</td>\n",
112
+ " <td>1</td>\n",
113
+ " <td>0</td>\n",
114
+ " <td>113803</td>\n",
115
+ " <td>53.1000</td>\n",
116
+ " <td>C123</td>\n",
117
+ " <td>S</td>\n",
118
+ " </tr>\n",
119
+ " <tr>\n",
120
+ " <th>4</th>\n",
121
+ " <td>5</td>\n",
122
+ " <td>0</td>\n",
123
+ " <td>3</td>\n",
124
+ " <td>Allen, Mr. William Henry</td>\n",
125
+ " <td>male</td>\n",
126
+ " <td>35.0</td>\n",
127
+ " <td>0</td>\n",
128
+ " <td>0</td>\n",
129
+ " <td>373450</td>\n",
130
+ " <td>8.0500</td>\n",
131
+ " <td>NaN</td>\n",
132
+ " <td>S</td>\n",
133
+ " </tr>\n",
134
+ " <tr>\n",
135
+ " <th>...</th>\n",
136
+ " <td>...</td>\n",
137
+ " <td>...</td>\n",
138
+ " <td>...</td>\n",
139
+ " <td>...</td>\n",
140
+ " <td>...</td>\n",
141
+ " <td>...</td>\n",
142
+ " <td>...</td>\n",
143
+ " <td>...</td>\n",
144
+ " <td>...</td>\n",
145
+ " <td>...</td>\n",
146
+ " <td>...</td>\n",
147
+ " <td>...</td>\n",
148
+ " </tr>\n",
149
+ " <tr>\n",
150
+ " <th>886</th>\n",
151
+ " <td>887</td>\n",
152
+ " <td>0</td>\n",
153
+ " <td>2</td>\n",
154
+ " <td>Montvila, Rev. Juozas</td>\n",
155
+ " <td>male</td>\n",
156
+ " <td>27.0</td>\n",
157
+ " <td>0</td>\n",
158
+ " <td>0</td>\n",
159
+ " <td>211536</td>\n",
160
+ " <td>13.0000</td>\n",
161
+ " <td>NaN</td>\n",
162
+ " <td>S</td>\n",
163
+ " </tr>\n",
164
+ " <tr>\n",
165
+ " <th>887</th>\n",
166
+ " <td>888</td>\n",
167
+ " <td>1</td>\n",
168
+ " <td>1</td>\n",
169
+ " <td>Graham, Miss. Margaret Edith</td>\n",
170
+ " <td>female</td>\n",
171
+ " <td>19.0</td>\n",
172
+ " <td>0</td>\n",
173
+ " <td>0</td>\n",
174
+ " <td>112053</td>\n",
175
+ " <td>30.0000</td>\n",
176
+ " <td>B42</td>\n",
177
+ " <td>S</td>\n",
178
+ " </tr>\n",
179
+ " <tr>\n",
180
+ " <th>888</th>\n",
181
+ " <td>889</td>\n",
182
+ " <td>0</td>\n",
183
+ " <td>3</td>\n",
184
+ " <td>Johnston, Miss. Catherine Helen \"Carrie\"</td>\n",
185
+ " <td>female</td>\n",
186
+ " <td>NaN</td>\n",
187
+ " <td>1</td>\n",
188
+ " <td>2</td>\n",
189
+ " <td>W./C. 6607</td>\n",
190
+ " <td>23.4500</td>\n",
191
+ " <td>NaN</td>\n",
192
+ " <td>S</td>\n",
193
+ " </tr>\n",
194
+ " <tr>\n",
195
+ " <th>889</th>\n",
196
+ " <td>890</td>\n",
197
+ " <td>1</td>\n",
198
+ " <td>1</td>\n",
199
+ " <td>Behr, Mr. Karl Howell</td>\n",
200
+ " <td>male</td>\n",
201
+ " <td>26.0</td>\n",
202
+ " <td>0</td>\n",
203
+ " <td>0</td>\n",
204
+ " <td>111369</td>\n",
205
+ " <td>30.0000</td>\n",
206
+ " <td>C148</td>\n",
207
+ " <td>C</td>\n",
208
+ " </tr>\n",
209
+ " <tr>\n",
210
+ " <th>890</th>\n",
211
+ " <td>891</td>\n",
212
+ " <td>0</td>\n",
213
+ " <td>3</td>\n",
214
+ " <td>Dooley, Mr. Patrick</td>\n",
215
+ " <td>male</td>\n",
216
+ " <td>32.0</td>\n",
217
+ " <td>0</td>\n",
218
+ " <td>0</td>\n",
219
+ " <td>370376</td>\n",
220
+ " <td>7.7500</td>\n",
221
+ " <td>NaN</td>\n",
222
+ " <td>Q</td>\n",
223
+ " </tr>\n",
224
+ " </tbody>\n",
225
+ "</table>\n",
226
+ "<p>891 rows × 12 columns</p>\n",
227
+ "</div>"
228
+ ],
229
+ "text/plain": [
230
+ " PassengerId Survived Pclass \\\n",
231
+ "0 1 0 3 \n",
232
+ "1 2 1 1 \n",
233
+ "2 3 1 3 \n",
234
+ "3 4 1 1 \n",
235
+ "4 5 0 3 \n",
236
+ ".. ... ... ... \n",
237
+ "886 887 0 2 \n",
238
+ "887 888 1 1 \n",
239
+ "888 889 0 3 \n",
240
+ "889 890 1 1 \n",
241
+ "890 891 0 3 \n",
242
+ "\n",
243
+ " Name Sex Age SibSp \\\n",
244
+ "0 Braund, Mr. Owen Harris male 22.0 1 \n",
245
+ "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n",
246
+ "2 Heikkinen, Miss. Laina female 26.0 0 \n",
247
+ "3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 \n",
248
+ "4 Allen, Mr. William Henry male 35.0 0 \n",
249
+ ".. ... ... ... ... \n",
250
+ "886 Montvila, Rev. Juozas male 27.0 0 \n",
251
+ "887 Graham, Miss. Margaret Edith female 19.0 0 \n",
252
+ "888 Johnston, Miss. Catherine Helen \"Carrie\" female NaN 1 \n",
253
+ "889 Behr, Mr. Karl Howell male 26.0 0 \n",
254
+ "890 Dooley, Mr. Patrick male 32.0 0 \n",
255
+ "\n",
256
+ " Parch Ticket Fare Cabin Embarked \n",
257
+ "0 0 A/5 21171 7.2500 NaN S \n",
258
+ "1 0 PC 17599 71.2833 C85 C \n",
259
+ "2 0 STON/O2. 3101282 7.9250 NaN S \n",
260
+ "3 0 113803 53.1000 C123 S \n",
261
+ "4 0 373450 8.0500 NaN S \n",
262
+ ".. ... ... ... ... ... \n",
263
+ "886 0 211536 13.0000 NaN S \n",
264
+ "887 0 112053 30.0000 B42 S \n",
265
+ "888 2 W./C. 6607 23.4500 NaN S \n",
266
+ "889 0 111369 30.0000 C148 C \n",
267
+ "890 0 370376 7.7500 NaN Q \n",
268
+ "\n",
269
+ "[891 rows x 12 columns]"
270
+ ]
271
+ },
272
+ "execution_count": 2,
273
+ "metadata": {},
274
+ "output_type": "execute_result"
275
+ }
276
+ ],
277
+ "source": [
278
+ "data = pd.read_csv('/home/graziela/Documents/CCF-gradio/Data-Image_tinanic/train.csv')\n",
279
+ "data"
280
+ ]
281
+ },
282
+ {
283
+ "cell_type": "code",
284
+ "execution_count": 3,
285
+ "id": "f43b434c",
286
+ "metadata": {},
287
+ "outputs": [],
288
+ "source": [
289
+ "X = data[[\"Sex\", \"Age\", \"Pclass\"]].copy()\n",
290
+ "y = data[\"Survived\"]\n",
291
+ "\n",
292
+ "X[\"Sex\"] = X[\"Sex\"].map({\"male\": 0, \"female\": 1}) #transforma a variável\n",
293
+ "X =X.fillna(-1) #insere -1 para todos os valores nulos"
294
+ ]
295
+ },
296
+ {
297
+ "cell_type": "code",
298
+ "execution_count": 4,
299
+ "id": "1649cd1f",
300
+ "metadata": {},
301
+ "outputs": [
302
+ {
303
+ "data": {
304
+ "text/plain": [
305
+ "0.7985074626865671"
306
+ ]
307
+ },
308
+ "execution_count": 4,
309
+ "metadata": {},
310
+ "output_type": "execute_result"
311
+ }
312
+ ],
313
+ "source": [
314
+ "X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=42)\n",
315
+ "\n",
316
+ "mdl = DecisionTreeClassifier(criterion= 'entropy', \n",
317
+ " max_depth= 10, \n",
318
+ " min_samples_leaf= 10, \n",
319
+ " min_samples_split= 5, \n",
320
+ " splitter= 'best', \n",
321
+ " random_state=0)\n",
322
+ "\n",
323
+ "\n",
324
+ "scores = mdl.fit(X_train, y_train)\n",
325
+ "\n",
326
+ "p = mdl.predict(X_test)\n",
327
+ "accuracy_score(y_test, p)"
328
+ ]
329
+ },
330
+ {
331
+ "cell_type": "code",
332
+ "execution_count": 7,
333
+ "id": "078f9347",
334
+ "metadata": {},
335
+ "outputs": [
336
+ {
337
+ "data": {
338
+ "text/plain": [
339
+ "['titanic/model.pkl']"
340
+ ]
341
+ },
342
+ "execution_count": 7,
343
+ "metadata": {},
344
+ "output_type": "execute_result"
345
+ }
346
+ ],
347
+ "source": [
348
+ "import joblib\n",
349
+ "import os\n",
350
+ "\n",
351
+ "mdl.fit(X, y)\n",
352
+ "joblib.dump(mdl, os.path.join(\"titanic\", \"model.pkl\")) #salvar o objetivo do modelo treinado"
353
+ ]
354
+ },
355
+ {
356
+ "cell_type": "code",
357
+ "execution_count": 9,
358
+ "id": "ef5f1d0d",
359
+ "metadata": {},
360
+ "outputs": [
361
+ {
362
+ "name": "stdout",
363
+ "output_type": "stream",
364
+ "text": [
365
+ "README.md model.pkl\r\n"
366
+ ]
367
+ }
368
+ ],
369
+ "source": [
370
+ "!ls titanic"
371
+ ]
372
+ },
373
+ {
374
+ "cell_type": "code",
375
+ "execution_count": 10,
376
+ "id": "704aeaa8",
377
+ "metadata": {},
378
+ "outputs": [],
379
+ "source": [
380
+ "import gradio as gr\n",
381
+ "import joblib as jb\n",
382
+ "\n",
383
+ "def predict(sex, age, pclass):\n",
384
+ " model = jb.load('model.pkl')\n",
385
+ " pclass = int(pclass)\n",
386
+ " p = model.predict({{sex, age, pclass}})[0]\n",
387
+ "\n",
388
+ " return {\"Did not survive\": p[0], \"Survived\": p[1]}\n",
389
+ "\n",
390
+ "\n"
391
+ ]
392
+ },
393
+ {
394
+ "cell_type": "code",
395
+ "execution_count": 12,
396
+ "id": "4376a675",
397
+ "metadata": {},
398
+ "outputs": [
399
+ {
400
+ "name": "stdout",
401
+ "output_type": "stream",
402
+ "text": [
403
+ "Running on local URL: http://127.0.0.1:7861\n",
404
+ "Running on public URL: https://8e3ad352c3f4d6da9f.gradio.live\n",
405
+ "\n",
406
+ "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
407
+ ]
408
+ },
409
+ {
410
+ "data": {
411
+ "text/html": [
412
+ "<div><iframe src=\"https://8e3ad352c3f4d6da9f.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
413
+ ],
414
+ "text/plain": [
415
+ "<IPython.core.display.HTML object>"
416
+ ]
417
+ },
418
+ "metadata": {},
419
+ "output_type": "display_data"
420
+ },
421
+ {
422
+ "name": "stdout",
423
+ "output_type": "stream",
424
+ "text": [
425
+ "Keyboard interruption in main thread... closing server.\n",
426
+ "Killing tunnel 127.0.0.1:7861 <> https://8e3ad352c3f4d6da9f.gradio.live\n"
427
+ ]
428
+ },
429
+ {
430
+ "data": {
431
+ "text/plain": []
432
+ },
433
+ "execution_count": 12,
434
+ "metadata": {},
435
+ "output_type": "execute_result"
436
+ }
437
+ ],
438
+ "source": [
439
+ "\n",
440
+ "iface = gr.Interface(fn=predict,\n",
441
+ " inputs=[gr.Dropdown(choices=[\"male\", \"female\"], type=\"index\"),\n",
442
+ " \"number\",\n",
443
+ " gr.Dropdown(choices=[\"1\", \"2\", \"3\"], type=\"value\")\n",
444
+ " ],\n",
445
+ " outputs=\"label\")\n",
446
+ "\n",
447
+ "iface.launch(debug=True, share=True)\n",
448
+ "\n"
449
+ ]
450
+ },
451
+ {
452
+ "cell_type": "code",
453
+ "execution_count": null,
454
+ "id": "e1a7584a",
455
+ "metadata": {},
456
+ "outputs": [],
457
+ "source": []
458
+ }
459
+ ],
460
+ "metadata": {
461
+ "kernelspec": {
462
+ "display_name": "Python 3 (ipykernel)",
463
+ "language": "python",
464
+ "name": "python3"
465
+ },
466
+ "language_info": {
467
+ "codemirror_mode": {
468
+ "name": "ipython",
469
+ "version": 3
470
+ },
471
+ "file_extension": ".py",
472
+ "mimetype": "text/x-python",
473
+ "name": "python",
474
+ "nbconvert_exporter": "python",
475
+ "pygments_lexer": "ipython3",
476
+ "version": "3.10.9"
477
+ }
478
+ },
479
+ "nbformat": 4,
480
+ "nbformat_minor": 5
481
+ }