{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f66418f6", "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "from sklearn.tree import DecisionTreeClassifier\n", "from sklearn.metrics import accuracy_score\n", "import pandas as pd\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "f5db6619", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS
3411Futrelle, Mrs. Jacques Heath (Lily May Peel)female35.01011380353.1000C123S
4503Allen, Mr. William Henrymale35.0003734508.0500NaNS
.......................................
88688702Montvila, Rev. Juozasmale27.00021153613.0000NaNS
88788811Graham, Miss. Margaret Edithfemale19.00011205330.0000B42S
88888903Johnston, Miss. Catherine Helen \"Carrie\"femaleNaN12W./C. 660723.4500NaNS
88989011Behr, Mr. Karl Howellmale26.00011136930.0000C148C
89089103Dooley, Mr. Patrickmale32.0003703767.7500NaNQ
\n", "

891 rows × 12 columns

\n", "
" ], "text/plain": [ " PassengerId Survived Pclass \\\n", "0 1 0 3 \n", "1 2 1 1 \n", "2 3 1 3 \n", "3 4 1 1 \n", "4 5 0 3 \n", ".. ... ... ... \n", "886 887 0 2 \n", "887 888 1 1 \n", "888 889 0 3 \n", "889 890 1 1 \n", "890 891 0 3 \n", "\n", " Name Sex Age SibSp \\\n", "0 Braund, Mr. Owen Harris male 22.0 1 \n", "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n", "2 Heikkinen, Miss. Laina female 26.0 0 \n", "3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 \n", "4 Allen, Mr. William Henry male 35.0 0 \n", ".. ... ... ... ... \n", "886 Montvila, Rev. Juozas male 27.0 0 \n", "887 Graham, Miss. Margaret Edith female 19.0 0 \n", "888 Johnston, Miss. Catherine Helen \"Carrie\" female NaN 1 \n", "889 Behr, Mr. Karl Howell male 26.0 0 \n", "890 Dooley, Mr. Patrick male 32.0 0 \n", "\n", " Parch Ticket Fare Cabin Embarked \n", "0 0 A/5 21171 7.2500 NaN S \n", "1 0 PC 17599 71.2833 C85 C \n", "2 0 STON/O2. 3101282 7.9250 NaN S \n", "3 0 113803 53.1000 C123 S \n", "4 0 373450 8.0500 NaN S \n", ".. ... ... ... ... ... \n", "886 0 211536 13.0000 NaN S \n", "887 0 112053 30.0000 B42 S \n", "888 2 W./C. 6607 23.4500 NaN S \n", "889 0 111369 30.0000 C148 C \n", "890 0 370376 7.7500 NaN Q \n", "\n", "[891 rows x 12 columns]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.read_csv('/home/graziela/Documents/CCF-gradio/Data-Image_tinanic/train.csv')\n", "data" ] }, { "cell_type": "code", "execution_count": 3, "id": "f43b434c", "metadata": {}, "outputs": [], "source": [ "X = data[[\"Sex\", \"Age\", \"Pclass\"]].copy()\n", "y = data[\"Survived\"]\n", "\n", "X[\"Sex\"] = X[\"Sex\"].map({\"male\": 0, \"female\": 1}) #transforma a variável\n", "X =X.fillna(-1) #insere -1 para todos os valores nulos" ] }, { "cell_type": "code", "execution_count": 4, "id": "1649cd1f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7985074626865671" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=42)\n", "\n", "mdl = DecisionTreeClassifier(criterion= 'entropy', \n", " max_depth= 10, \n", " min_samples_leaf= 10, \n", " min_samples_split= 5, \n", " splitter= 'best', \n", " random_state=0)\n", "\n", "\n", "scores = mdl.fit(X_train, y_train)\n", "\n", "p = mdl.predict(X_test)\n", "accuracy_score(y_test, p)" ] }, { "cell_type": "code", "execution_count": 7, "id": "078f9347", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['titanic/model.pkl']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import joblib\n", "import os\n", "\n", "mdl.fit(X, y)\n", "joblib.dump(mdl, os.path.join(\"titanic\", \"model.pkl\")) #salvar o objetivo do modelo treinado" ] }, { "cell_type": "code", "execution_count": 9, "id": "ef5f1d0d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "README.md model.pkl\r\n" ] } ], "source": [ "!ls titanic" ] }, { "cell_type": "code", "execution_count": 10, "id": "704aeaa8", "metadata": {}, "outputs": [], "source": [ "import gradio as gr\n", "import joblib as jb\n", "\n", "def predict(sex, age, pclass):\n", " model = jb.load('model.pkl')\n", " pclass = int(pclass)\n", " p = model.predict({{sex, age, pclass}})[0]\n", "\n", " return {\"Did not survive\": p[0], \"Survived\": p[1]}\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "4376a675", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7861\n", "Running on public URL: https://8e3ad352c3f4d6da9f.gradio.live\n", "\n", "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" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Keyboard interruption in main thread... closing server.\n", "Killing tunnel 127.0.0.1:7861 <> https://8e3ad352c3f4d6da9f.gradio.live\n" ] }, { "data": { "text/plain": [] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "iface = gr.Interface(fn=predict,\n", " inputs=[gr.Dropdown(choices=[\"male\", \"female\"], type=\"index\"),\n", " \"number\",\n", " gr.Dropdown(choices=[\"1\", \"2\", \"3\"], type=\"value\")\n", " ],\n", " outputs=\"label\")\n", "\n", "iface.launch(debug=True, share=True)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e1a7584a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }