{"cells":[{"cell_type":"code","source":"from google.colab import drive\nimport os\ndrive.mount('/content/gdrive')\n# Establecer ruta de acceso en dr\nimport os\nprint(os.getcwd())\nos.chdir(\"/content/gdrive/My Drive\")","metadata":{"id":"v-W2fBsNGd43","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"a0c35107755b4d6fb892c64979c8b66b","outputId":"8fa9c105-d9ea-4b5f-8416-5b0800a8b676","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":80733,"user_tz":240,"timestamp":1652699812552},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"Mounted at /content/gdrive\n/content\n"}],"execution_count":1},{"cell_type":"code","source":"import pandas as pd \nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nimport xgboost as xgb\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.model_selection import RandomizedSearchCV\nfrom sklearn.experimental import enable_halving_search_cv\nfrom sklearn.model_selection import HalvingGridSearchCV\nfrom sklearn.model_selection import HalvingRandomSearchCV","metadata":{"id":"rjeVWoRPIx7Y","cell_id":"0654523854594147942823056a428873","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":309,"user_tz":240,"timestamp":1652699975119},"deepnote_cell_type":"code"},"outputs":[],"execution_count":3},{"cell_type":"markdown","source":"**Datos**\n\nhttps://www.kaggle.com/datasets/mlg-ulb/creditcardfraudv\n\n\n**Contexto**\n\nEs importante que las compañías de tarjetas de crédito puedan reconocer las transacciones de tarjetas de crédito fraudulentas para que a los clientes no se les cobre por artículos que no compraron.\n\n**Contenido**\n\nEl conjunto de datos contiene transacciones realizadas con tarjetas de crédito en septiembre de 2013 por titulares de tarjetas europeos.\n\nEste conjunto de datos presenta transacciones que ocurrieron en dos días, donde tenemos 492 fraudes de 284,807 transacciones. El conjunto de datos está muy desequilibrado, la clase positiva (fraudes) representa el 0,172 % de todas las transacciones.\n\nContiene solo variables de entrada numéricas que son el resultado de una transformación PCA.\n\nDesafortunadamente, debido a problemas de confidencialidad, no se pueden proporcionar las características originales ni más información general sobre los datos. Las características V1, V2, … V28 son los principales componentes obtenidos con PCA, las únicas características que no han sido transformadas con PCA son **Time y Amount**. \n\n\nLa característica **Time** contiene los segundos transcurridos entre cada transacción y la primera transacción en el conjunto de datos. La variable **Amount** es la cantidad de la transacción, esta función se puede utilizar para el aprendizaje sensible a los costos dependiente del ejemplo. **Class** es la variable de respuesta y toma valor 1 en caso de fraude y 0 en caso contrario.","metadata":{"id":"RmVL0VPNGwXq","cell_id":"7099cdbf2a0646b2ac94d340aa1dcd86","deepnote_cell_type":"markdown"}},{"cell_type":"code","source":"# Lectura de DF\ndf=pd.read_csv('creditcard.csv')\n# Vamos a eliminar la columna tiempo\ndf= df.drop(columns='Time')\n# Estandarizamos la columna Amount\ndf['Amount']=(df['Amount']- np.mean(df['Amount']))/np.std(df.Amount)\ndf.head()\n# El problema es muy desbalanceado tranajaremos con una muestra para resolver el problema\ndf_ones=df[df['Class']==1] # Filtro de caracteristica\nprint(df_ones.shape)\ndf_zeros=df[df['Class']==0] # Filtro de NO caracteristica\ndf_zeros= df_zeros.sample(3*df_ones.shape[0]) # Tamaño de muestra 3 veces el de la caracteristica\nprint(df_zeros.shape)\n# Concatenar\ndf_final=pd.DataFrame(np.concatenate([df_ones, df_zeros],axis=0), columns=df.columns)\nprint(df_final.shape)\ndf_final.head()","metadata":{"id":"QKk22YSxH8zF","colab":{"height":355,"base_uri":"https://localhost:8080/"},"cell_id":"959c5e1482bb4806bee3e2915d405d89","outputId":"b4e8227a-d2f2-481a-ff0f-ae1b27ea558e","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":4480,"user_tz":240,"timestamp":1652700346636},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"(492, 30)\n(1476, 30)\n(1968, 30)\n"},{"output_type":"execute_result","data":{"text/plain":" V1 V2 V3 V4 V5 V6 V7 \\\n0 -2.312227 1.951992 -1.609851 3.997906 -0.522188 -1.426545 -2.537387 \n1 -3.043541 -3.157307 1.088463 2.288644 1.359805 -1.064823 0.325574 \n2 -2.303350 1.759247 -0.359745 2.330243 -0.821628 -0.075788 0.562320 \n3 -4.397974 1.358367 -2.592844 2.679787 -1.128131 -1.706536 -3.496197 \n4 1.234235 3.019740 -4.304597 4.732795 3.624201 -1.357746 1.713445 \n\n V8 V9 V10 ... V21 V22 V23 V24 \\\n0 1.391657 -2.770089 -2.772272 ... 0.517232 -0.035049 -0.465211 0.320198 \n1 -0.067794 -0.270953 -0.838587 ... 0.661696 0.435477 1.375966 -0.293803 \n2 -0.399147 -0.238253 -1.525412 ... -0.294166 -0.932391 0.172726 -0.087330 \n3 -0.248778 -0.247768 -4.801637 ... 0.573574 0.176968 -0.436207 -0.053502 \n4 -0.496358 -1.282858 -2.447469 ... -0.379068 -0.704181 -0.656805 -1.632653 \n\n V25 V26 V27 V28 Amount Class \n0 0.044519 0.177840 0.261145 -0.143276 -0.353229 1.0 \n1 0.279798 -0.145362 -0.252773 0.035764 1.761758 1.0 \n2 -0.156114 -0.542628 0.039566 -0.153029 0.606031 1.0 \n3 0.252405 -0.657488 -0.827136 0.849573 -0.117342 1.0 \n4 1.488901 0.566797 -0.010016 0.146793 -0.349231 1.0 \n\n[5 rows x 30 columns]","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
V1V2V3V4V5V6V7V8V9V10...V21V22V23V24V25V26V27V28AmountClass
0-2.3122271.951992-1.6098513.997906-0.522188-1.426545-2.5373871.391657-2.770089-2.772272...0.517232-0.035049-0.4652110.3201980.0445190.1778400.261145-0.143276-0.3532291.0
1-3.043541-3.1573071.0884632.2886441.359805-1.0648230.325574-0.067794-0.270953-0.838587...0.6616960.4354771.375966-0.2938030.279798-0.145362-0.2527730.0357641.7617581.0
2-2.3033501.759247-0.3597452.330243-0.821628-0.0757880.562320-0.399147-0.238253-1.525412...-0.294166-0.9323910.172726-0.087330-0.156114-0.5426280.039566-0.1530290.6060311.0
3-4.3979741.358367-2.5928442.679787-1.128131-1.706536-3.496197-0.248778-0.247768-4.801637...0.5735740.176968-0.436207-0.0535020.252405-0.657488-0.8271360.849573-0.1173421.0
41.2342353.019740-4.3045974.7327953.624201-1.3577461.713445-0.496358-1.282858-2.447469...-0.379068-0.704181-0.656805-1.6326531.4889010.566797-0.0100160.146793-0.3492311.0
\n

5 rows × 30 columns

\n
\n \n \n \n\n \n
\n
\n "},"metadata":{},"execution_count":18}],"execution_count":18},{"cell_type":"code","source":"df_final.shape","metadata":{"id":"Vnytb51MIUdY","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"9fda5b8134f8450b8890d01643d6a201","outputId":"c4c6f046-1ef9-4574-eb9f-5a1fddcfa527","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":245,"user_tz":240,"timestamp":1652700369916},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"(1968, 30)"},"metadata":{},"execution_count":19}],"execution_count":19},{"cell_type":"code","source":"df_final.isnull().sum()","metadata":{"id":"qPH0EIl1IVD2","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"1a3ff18874034e36980a343187b20a3f","outputId":"50fecac5-c4ce-4560-9d1b-9fa776ad78fe","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":4,"user_tz":240,"timestamp":1652700373824},"deepnote_cell_type":"code"},"outputs":[{"output_type":"execute_result","data":{"text/plain":"V1 0\nV2 0\nV3 0\nV4 0\nV5 0\nV6 0\nV7 0\nV8 0\nV9 0\nV10 0\nV11 0\nV12 0\nV13 0\nV14 0\nV15 0\nV16 0\nV17 0\nV18 0\nV19 0\nV20 0\nV21 0\nV22 0\nV23 0\nV24 0\nV25 0\nV26 0\nV27 0\nV28 0\nAmount 0\nClass 0\ndtype: int64"},"metadata":{},"execution_count":20}],"execution_count":20},{"cell_type":"code","source":"# Separar en X y y\ny= df_final.Class\nX= df_final.drop(columns='Class', axis=1)\nprint(X.shape, y.shape)","metadata":{"id":"60dySlkgIccY","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"d87cdfffb4df4e4a99d8ece7b0c361e8","outputId":"1dc7035f-2205-4bd4-f339-c4284b889075","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":260,"user_tz":240,"timestamp":1652700385705},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"(1968, 29) (1968,)\n"}],"execution_count":21},{"cell_type":"code","source":"# Separar en train y test\nX_train,X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\nprint(X_train.shape, X_test.shape)","metadata":{"id":"NU2hJN4sIk04","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"601dc0624604449581c237bf0294724b","outputId":"10c9a63b-fd3c-4e75-a834-2b65fe324d1f","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":4,"user_tz":240,"timestamp":1652700392478},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"(1377, 29) (591, 29)\n"}],"execution_count":22},{"cell_type":"markdown","source":"# Seleccion manual","metadata":{"id":"a3XoTC9lI4OI","cell_id":"90df3b3d85844fb5a1c87635ba1b561b","deepnote_cell_type":"markdown"}},{"cell_type":"code","source":"model= xgb.XGBClassifier(learning_rate=0.001)\n# Lista de hiperparametros\nparams_1 = {'criterion': 'gini', 'splitter': 'best', 'max_depth': 5}\nparams_2 = {'criterion': 'entropy', 'splitter': 'random', 'max_depth': 7}\nparams_3 = {'criterion': 'gini', 'splitter': 'random', 'max_depth': 10}","metadata":{"id":"921EyDXNJnEF","cell_id":"34b71f31d2d44aadb435d0d307b5c60b","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":350,"user_tz":240,"timestamp":1652700459323},"deepnote_cell_type":"code"},"outputs":[],"execution_count":27},{"cell_type":"markdown","source":"## Estos procesos pueden demorar bastante !!","metadata":{"id":"1iNH9SrlQCmp","cell_id":"6773accfeff342b98d75e589a2dc695a","deepnote_cell_type":"markdown"}},{"cell_type":"code","source":"# Modelo 1\nmodel.set_params(**params_1).fit(X_train, y_train)\nprint(f'Accuracy para Modelo 1 = {round(accuracy_score(y_test, model.predict(X_test)), 5)}')\n# Modelo 2\nmodel.set_params(**params_2).fit(X_train, y_train)\nprint(f'Accuracy para Modelo 2 = {round(accuracy_score(y_test, model.predict(X_test)), 5)}')\n# Modelo 3\nmodel.set_params(**params_3).fit(X_train, y_train)\nprint(f'Accuracy para Modelo 3 = {round(accuracy_score(y_test, model.predict(X_test)), 5)}')","metadata":{"id":"5o6PX8a2JxHc","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"edc0ef12797e4780a863ec0164f4ae5a","outputId":"57424759-31a0-4fdd-fd2b-3bd5128f9239","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":2193,"user_tz":240,"timestamp":1652700463348},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"Accuracy para Modelo 1 = 0.95431\nAccuracy para Modelo 2 = 0.95431\nAccuracy para Modelo 3 = 0.94924\n"}],"execution_count":28},{"cell_type":"markdown","source":"# Grid Search","metadata":{"id":"5NWzmFF0KeLk","cell_id":"1c6b0da02c6d47f4ae14d7ef836c840c","deepnote_cell_type":"markdown"}},{"cell_type":"code","source":"params_grid = {\n 'min_child_weight': [1, 5, 10],\n 'gamma': [0.5, 1, 1.5, 2, 5],\n 'subsample': [0.6, 0.8, 1.0],\n 'colsample_bytree': [0.6, 0.8, 1.0],\n 'max_depth': [5,6,7],\n 'criterion':['entropy','gini']\n }","metadata":{"id":"F4qNVEE_K57r","cell_id":"63d0b11beb4b4ef8ad82bb29a36f4b93","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":312,"user_tz":240,"timestamp":1652700485592},"deepnote_cell_type":"code"},"outputs":[],"execution_count":29},{"cell_type":"code","source":"# tiempo de ejecucuon 424 seg\ngrid_cv = GridSearchCV(model, params_grid, scoring=\"accuracy\", n_jobs=-1, cv=3)\ngrid_cv.fit(X_train, y_train)\n\nprint(\"Mejores Parametros\", grid_cv.best_params_)\nprint(\"Mejor CV score\", grid_cv.best_score_)\nprint(f'Accuracy del modelo = {round(accuracy_score(y_test, grid_cv.predict(X_test)), 5)}')","metadata":{"id":"3MEwTT4ZKfkx","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"34a18bc4fba4480a9d782579fe5c3bdf","outputId":"1cc4b2f1-f8d2-464f-83dc-68afabb471ca","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":424709,"user_tz":240,"timestamp":1652700916458},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"Mejores Parametros {'colsample_bytree': 0.6, 'criterion': 'entropy', 'gamma': 0.5, 'max_depth': 5, 'min_child_weight': 1, 'subsample': 1.0}\nMejor CV score 0.9600580973129992\nAccuracy del modelo = 0.96277\n"}],"execution_count":30},{"cell_type":"markdown","source":"# Randomized Search CV","metadata":{"id":"EitMBeV5Mk9X","cell_id":"99c29dc4beec445cb8277f853094b0fc","deepnote_cell_type":"markdown"}},{"cell_type":"code","source":"# tiempo de ejecucuon 7 seg\ngrid_cv = RandomizedSearchCV(model, params_grid, scoring=\"accuracy\", n_jobs=-1, cv=3)\ngrid_cv.fit(X_train, y_train)\n\nprint(\"Mejores parametros\", grid_cv.best_params_)\nprint(\"Mejor score de CV\", grid_cv.best_score_)\nprint(f'Accuracy del modelo = {round(accuracy_score(y_test, grid_cv.predict(X_test)), 5)}')","metadata":{"id":"zCtvX8fJMmbt","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"39aade54ac2d4889ac2e760cfdd08a64","outputId":"c8227c31-e713-4e49-93f1-66b331dd2788","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":6972,"user_tz":240,"timestamp":1652701035673},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"Mejores parametros {'subsample': 0.8, 'min_child_weight': 5, 'max_depth': 5, 'gamma': 2, 'criterion': 'gini', 'colsample_bytree': 0.6}\nMejor score de CV 0.9578794480755265\nAccuracy del modelo = 0.95939\n"}],"execution_count":31},{"cell_type":"markdown","source":"# Halving Grid Search","metadata":{"id":"fi9ydI3-M0uk","cell_id":"1af13cd3222e4cc59837f265983bde71","deepnote_cell_type":"markdown"}},{"cell_type":"code","source":"# tiempo de ejecucuon 125 seg\nhalving_cv = HalvingGridSearchCV(model, params_grid, scoring=\"accuracy\", factor=3)\nhalving_cv.fit(X_train, y_train)\n\nprint(\"Mejores parametros\", halving_cv.best_params_)\nprint(\"Mejor Score CV\", halving_cv.best_score_)\nprint(f'Accuracy del modelo = {round(accuracy_score(y_test, halving_cv.predict(X_test)), 5)}')","metadata":{"id":"LSdd0H21M2fE","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"2d8fdc490ddc4e76b8b88102ac0e92f3","outputId":"7d0aa95b-bccd-481d-ab40-c323356fa412","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":125851,"user_tz":240,"timestamp":1652701171066},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"Mejores parametros {'colsample_bytree': 0.6, 'criterion': 'gini', 'gamma': 2, 'max_depth': 5, 'min_child_weight': 1, 'subsample': 0.8}\nMejor Score CV 0.9702319141571479\nAccuracy del modelo = 0.96616\n"}],"execution_count":32},{"cell_type":"markdown","source":"# Halving Randomized Search","metadata":{"id":"rPn3PZioNGib","cell_id":"72409a6121d34f54ba96a7cc21d03299","deepnote_cell_type":"markdown"}},{"cell_type":"code","source":"# tiempo de ejecucuon 13 seg\nhalving_cv = HalvingRandomSearchCV(model, params_grid, scoring=\"accuracy\", factor=3)\nhalving_cv.fit(X_train, y_train)\n\nprint(\"Mejores parametros\", halving_cv.best_params_)\nprint(\"Mejor CV score\", halving_cv.best_score_)\nprint(f'Accuracy del modelo = {round(accuracy_score(y_test, halving_cv.predict(X_test)), 5)}')","metadata":{"id":"C0uAEp5pNIrC","colab":{"base_uri":"https://localhost:8080/"},"cell_id":"d3c67c5be0884f69aa5be9e0851fea6b","outputId":"a689a44c-f5d2-4e28-9e74-61ccdf57b965","executionInfo":{"user":{"userId":"09471607480253994520","displayName":"David Francisco Bustos Usta"},"status":"ok","elapsed":13828,"user_tz":240,"timestamp":1652701386624},"deepnote_cell_type":"code"},"outputs":[{"output_type":"stream","name":"stdout","text":"Mejores parametros {'subsample': 0.8, 'min_child_weight': 1, 'max_depth': 7, 'gamma': 1, 'criterion': 'entropy', 'colsample_bytree': 0.6}\nMejor CV score 0.9516787815853236\nAccuracy del modelo = 0.96616\n"}],"execution_count":33},{"cell_type":"markdown","source":"\nCreated in deepnote.com \nCreated in Deepnote","metadata":{"created_in_deepnote_cell":true,"deepnote_cell_type":"markdown"}}],"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Ejemplo Hypertuning Parametros.ipynb","provenance":[],"authorship_tag":"ABX9TyPr4L4WgGfT8hGnm1zis+9d","collapsed_sections":[]},"deepnote":{},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"deepnote_notebook_id":"e58738671d55433da3c760cb52673373","deepnote_execution_queue":[]}}