{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":77090,"databundleVersionId":8412854,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport re\nimport string\nimport warnings \nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix,classification_report,roc_auc_score,f1_score,recall_score,precision_score\nimport gc\n\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:57:17.769482Z","iopub.execute_input":"2024-05-06T16:57:17.769890Z","iopub.status.idle":"2024-05-06T16:57:22.207566Z","shell.execute_reply.started":"2024-05-06T16:57:17.769854Z","shell.execute_reply":"2024-05-06T16:57:22.206473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LECTURA DE DATOS","metadata":{}},{"cell_type":"code","source":"data=pd.read_csv(\"/kaggle/input/desafo-clasificacin-de-proveedores-activos/train_202201-202402.csv\",sep=',')\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:57:30.176053Z","iopub.execute_input":"2024-05-06T16:57:30.176458Z","iopub.status.idle":"2024-05-06T16:57:34.339891Z","shell.execute_reply.started":"2024-05-06T16:57:30.176427Z","shell.execute_reply":"2024-05-06T16:57:34.337718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:57:58.340041Z","iopub.execute_input":"2024-05-06T16:57:58.340458Z","iopub.status.idle":"2024-05-06T16:57:58.374937Z","shell.execute_reply.started":"2024-05-06T16:57:58.340418Z","shell.execute_reply":"2024-05-06T16:57:58.373837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CREACIÓN VARIABLE RESPUESTA","metadata":{}},{"cell_type":"code","source":"data_train=data.copy()\ndata_train['FECHA_FORMALIZACIÓN']=pd.to_datetime(data_train['FECHA_FORMALIZACIÓN'], format='mixed', dayfirst=True, errors='coerce')\ndata_train['codmes_ult_est']=data_train['FECHA_FORMALIZACIÓN'].dt.strftime('%Y-%m-%d').astype('datetime64[ns]')\ndata_train['codmes']=data_train['FECHA_FORMALIZACIÓN'].dt.strftime('%Y%m')","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:58:01.062694Z","iopub.execute_input":"2024-05-06T16:58:01.063070Z","iopub.status.idle":"2024-05-06T16:58:02.877693Z","shell.execute_reply.started":"2024-05-06T16:58:01.063041Z","shell.execute_reply":"2024-05-06T16:58:02.876240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lis_fechas=list(data_train['codmes'].unique())","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:58:06.167205Z","iopub.execute_input":"2024-05-06T16:58:06.167642Z","iopub.status.idle":"2024-05-06T16:58:06.190147Z","shell.execute_reply.started":"2024-05-06T16:58:06.167609Z","shell.execute_reply":"2024-05-06T16:58:06.188864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rucmes=pd.crosstab(data_train['RUC_PROVEEDOR'],data_train['codmes'])\nfor i in [\"202403\",\"202404\"]:df_rucmes[i]=0","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:58:06.986783Z","iopub.execute_input":"2024-05-06T16:58:06.987179Z","iopub.status.idle":"2024-05-06T16:58:07.552114Z","shell.execute_reply.started":"2024-05-06T16:58:06.987147Z","shell.execute_reply":"2024-05-06T16:58:07.550888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rucmes","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:58:08.924855Z","iopub.execute_input":"2024-05-06T16:58:08.925330Z","iopub.status.idle":"2024-05-06T16:58:08.952295Z","shell.execute_reply.started":"2024-05-06T16:58:08.925294Z","shell.execute_reply":"2024-05-06T16:58:08.951172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ructarget=df_rucmes[lis_fechas+[\"202403\",\"202404\"]].stack().apply(lambda x:int(x>0))\ndf_ructarget=df_ructarget.reset_index(name='target')","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:58:09.839249Z","iopub.execute_input":"2024-05-06T16:58:09.839844Z","iopub.status.idle":"2024-05-06T16:58:09.895104Z","shell.execute_reply.started":"2024-05-06T16:58:09.839805Z","shell.execute_reply":"2024-05-06T16:58:09.893655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ructarget.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:58:10.666218Z","iopub.execute_input":"2024-05-06T16:58:10.666608Z","iopub.status.idle":"2024-05-06T16:58:10.677057Z","shell.execute_reply.started":"2024-05-06T16:58:10.666579Z","shell.execute_reply":"2024-05-06T16:58:10.676199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Visualizacion variable respuesta","metadata":{}},{"cell_type":"code","source":"sns.countplot(x=df_ructarget[df_ructarget.codmes<=\"202402\"][\"target\"].astype(str))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:58:12.167194Z","iopub.execute_input":"2024-05-06T16:58:12.167607Z","iopub.status.idle":"2024-05-06T16:58:12.465412Z","shell.execute_reply.started":"2024-05-06T16:58:12.167580Z","shell.execute_reply":"2024-05-06T16:58:12.463904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Proporción de proveedores por mes ","metadata":{}},{"cell_type":"code","source":"sns.countplot(y=df_ructarget[\"codmes\"].astype(str))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:58:54.363588Z","iopub.execute_input":"2024-05-06T16:58:54.363976Z","iopub.status.idle":"2024-05-06T16:58:54.809132Z","shell.execute_reply.started":"2024-05-06T16:58:54.363944Z","shell.execute_reply":"2024-05-06T16:58:54.807907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"No todos los proveedores tienen información en los meses observados, se considera como inicio de sus historias como primera formalización o transacción,\nsin embargo, por la presencia de proveedores nuevo se va considerar de 5 a 6 meses antes desde su primera transacción, con la finalidad de manejar predicciones con los nuevos que poseen poca historia.\n","metadata":{}},{"cell_type":"code","source":"def fu_mes_primerv(x,split_historia=3):\n   \n    val_corte=x[\"target\"].tolist().index(1)-split_historia\n    if val_corte<0:val_corte=0\n    valores_index=np.array(range(0,len(x)))\n    return pd.DataFrame(np.where(valores_index<val_corte,0,1))","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:59:00.374642Z","iopub.execute_input":"2024-05-06T16:59:00.375053Z","iopub.status.idle":"2024-05-06T16:59:00.381990Z","shell.execute_reply.started":"2024-05-06T16:59:00.375020Z","shell.execute_reply":"2024-05-06T16:59:00.380986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ructarget[\"primera_venta\"]=df_ructarget.groupby([\"RUC_PROVEEDOR\"]).apply(fu_mes_primerv,split_historia=6).values","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:59:01.942873Z","iopub.execute_input":"2024-05-06T16:59:01.943955Z","iopub.status.idle":"2024-05-06T16:59:02.564022Z","shell.execute_reply.started":"2024-05-06T16:59:01.943908Z","shell.execute_reply":"2024-05-06T16:59:02.562825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### TARGET VERSIÓN 2","metadata":{}},{"cell_type":"code","source":"df_ructargetv2=df_ructarget[df_ructarget[\"primera_venta\"]==1].copy() ### solo se selecciona el flag 1 para tener las primeras ventas y su anterio\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:59:04.175419Z","iopub.execute_input":"2024-05-06T16:59:04.175832Z","iopub.status.idle":"2024-05-06T16:59:04.189037Z","shell.execute_reply.started":"2024-05-06T16:59:04.175797Z","shell.execute_reply":"2024-05-06T16:59:04.187570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ructargetv2","metadata":{"execution":{"iopub.status.busy":"2024-05-06T16:59:14.235957Z","iopub.execute_input":"2024-05-06T16:59:14.238706Z","iopub.status.idle":"2024-05-06T16:59:14.250485Z","shell.execute_reply.started":"2024-05-06T16:59:14.238655Z","shell.execute_reply":"2024-05-06T16:59:14.249216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### VISUALIZACIÓN TARGET VERSIÓN 2","metadata":{}},{"cell_type":"code","source":"sns.countplot(x=df_ructargetv2[df_ructargetv2.codmes<=\"202402\"][\"target\"].astype(str))\nplt.show()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-05-06T16:59:16.534496Z","iopub.execute_input":"2024-05-06T16:59:16.534868Z","iopub.status.idle":"2024-05-06T16:59:16.803077Z","shell.execute_reply.started":"2024-05-06T16:59:16.534839Z","shell.execute_reply":"2024-05-06T16:59:16.801219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Proporción de proveedores por mes","metadata":{}},{"cell_type":"code","source":"sns.countplot(y=df_ructargetv2[\"codmes\"].astype(str).sort_values())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:00:21.386640Z","iopub.execute_input":"2024-05-06T17:00:21.387106Z","iopub.status.idle":"2024-05-06T17:00:21.860997Z","shell.execute_reply.started":"2024-05-06T17:00:21.387075Z","shell.execute_reply":"2024-05-06T17:00:21.859610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ructargetv2.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### INGENIERIA DE VARIABLES ","metadata":{}},{"cell_type":"code","source":"data_train[\"log_total\"]=data_train[\"TOTAL\"].transform(np.log1p)\ndf_train1=data_train.groupby(['RUC_PROVEEDOR','codmes']).agg({'log_total':['sum','mean'],                                                              \n                                                    'RUC_ENTIDAD':[\"nunique\"],\"RUC_PROVEEDOR\":[\"count\"]})##\ndf_train1=df_train1.fillna(0)\ndf_train1.columns=[a+\"_\"+b for a,b in df_train1.columns]\ndf_train1=df_train1.reset_index()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:07.369748Z","iopub.execute_input":"2024-05-06T17:01:07.370175Z","iopub.status.idle":"2024-05-06T17:01:07.462322Z","shell.execute_reply.started":"2024-05-06T17:01:07.370143Z","shell.execute_reply":"2024-05-06T17:01:07.461328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train1.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:08.456052Z","iopub.execute_input":"2024-05-06T17:01:08.457119Z","iopub.status.idle":"2024-05-06T17:01:08.464113Z","shell.execute_reply.started":"2024-05-06T17:01:08.457063Z","shell.execute_reply":"2024-05-06T17:01:08.462817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_mergeruc=pd.merge(df_ructargetv2,df_train1,on=[\"RUC_PROVEEDOR\",\"codmes\"],how=\"left\").fillna(0)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:09.713203Z","iopub.execute_input":"2024-05-06T17:01:09.713651Z","iopub.status.idle":"2024-05-06T17:01:09.759896Z","shell.execute_reply.started":"2024-05-06T17:01:09.713621Z","shell.execute_reply":"2024-05-06T17:01:09.758896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feat_roll=[a for a in df_mergeruc.columns if a not in ['RUC_PROVEEDOR', 'codmes',\"primera_venta\"]]","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:12.660756Z","iopub.execute_input":"2024-05-06T17:01:12.661170Z","iopub.status.idle":"2024-05-06T17:01:12.667761Z","shell.execute_reply.started":"2024-05-06T17:01:12.661137Z","shell.execute_reply":"2024-05-06T17:01:12.665888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feat_roll","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:14.313190Z","iopub.execute_input":"2024-05-06T17:01:14.313631Z","iopub.status.idle":"2024-05-06T17:01:14.320721Z","shell.execute_reply.started":"2024-05-06T17:01:14.313598Z","shell.execute_reply":"2024-05-06T17:01:14.319508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CALCULAMOS EL DESFASE TEMPORAL DE DOS MESES","metadata":{}},{"cell_type":"code","source":"df_merge_desfase=df_mergeruc.copy()\nshift=2\nfor j in list(feat_roll):\n    df_merge_desfase[f\"{j}_shift{shift}\"]=df_merge_desfase.groupby(['RUC_PROVEEDOR'])[j].shift(2)\n  #  df_merge2[f\"{j}_diff{shift}\"]=df_merge2.groupby(['RUC_PROVEEDOR'])[j].shift(2).diff()\n \n\n    for i in ['mean','sum'][:2 if j==\"target\" else None]:   \n    \n        df_merge_desfase[f'roll-{j}_{i}_{2}']=df_merge_desfase.groupby(['RUC_PROVEEDOR'])[j].shift(2).\\\n        rolling(window=2).agg(i).values\n           # if roll==3:df_merge2[f'roll-{j}_{i}_{roll}']=df_merge2[f'roll-{j}_{i}_{roll}'].fillna(0) \n        \n    if j!=\"target\":\n        del df_merge_desfase[j]\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:29.897199Z","iopub.execute_input":"2024-05-06T17:01:29.897621Z","iopub.status.idle":"2024-05-06T17:01:30.000885Z","shell.execute_reply.started":"2024-05-06T17:01:29.897590Z","shell.execute_reply":"2024-05-06T17:01:29.999444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_merge_desfase.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:32.717465Z","iopub.execute_input":"2024-05-06T17:01:32.717860Z","iopub.status.idle":"2024-05-06T17:01:32.728702Z","shell.execute_reply.started":"2024-05-06T17:01:32.717830Z","shell.execute_reply":"2024-05-06T17:01:32.727041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### DATOS DE ENTRENAMIENTO PRUEBA Y PREDICCIÓN","metadata":{}},{"cell_type":"code","source":"df_merge_desfase=df_merge_desfase.dropna().copy()#dropna().copy()\ndf_merge_train=df_merge_desfase[~df_merge_desfase[\"codmes\"].isin([\"202403\",\"202404\"])].copy().reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:34.476632Z","iopub.execute_input":"2024-05-06T17:01:34.477150Z","iopub.status.idle":"2024-05-06T17:01:34.534638Z","shell.execute_reply.started":"2024-05-06T17:01:34.477111Z","shell.execute_reply":"2024-05-06T17:01:34.532824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_merge_predict=df_merge_desfase[df_merge_desfase[\"codmes\"].isin([\"202403\",\"202404\"])].copy().reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:35.482158Z","iopub.execute_input":"2024-05-06T17:01:35.482635Z","iopub.status.idle":"2024-05-06T17:01:35.499181Z","shell.execute_reply.started":"2024-05-06T17:01:35.482600Z","shell.execute_reply":"2024-05-06T17:01:35.497723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=[a for a in df_merge_desfase.columns if a not in ['RUC_PROVEEDOR','codmes','target',\"periodo\",\"primera_venta\"]]","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:37.164417Z","iopub.execute_input":"2024-05-06T17:01:37.165040Z","iopub.status.idle":"2024-05-06T17:01:37.170832Z","shell.execute_reply.started":"2024-05-06T17:01:37.165008Z","shell.execute_reply":"2024-05-06T17:01:37.169584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"entrena=df_merge_train[~df_merge_train.codmes.isin([\"202401\",'202402'])][features+['target']]#202401\n\nvalid=df_merge_train[df_merge_train.codmes.isin([\"202401\",'202402'])][features+['target']+[\"codmes\"]]","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:37.773658Z","iopub.execute_input":"2024-05-06T17:01:37.774060Z","iopub.status.idle":"2024-05-06T17:01:37.798616Z","shell.execute_reply.started":"2024-05-06T17:01:37.774028Z","shell.execute_reply":"2024-05-06T17:01:37.796967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"entrena.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:42.543165Z","iopub.execute_input":"2024-05-06T17:01:42.543545Z","iopub.status.idle":"2024-05-06T17:01:42.566073Z","shell.execute_reply.started":"2024-05-06T17:01:42.543517Z","shell.execute_reply":"2024-05-06T17:01:42.564749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test= train_test_split(entrena[features],entrena['target'],test_size = 0.06, random_state = 2021,stratify=entrena['target'])\ny_train.value_counts()/len(y_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:43.389251Z","iopub.execute_input":"2024-05-06T17:01:43.389708Z","iopub.status.idle":"2024-05-06T17:01:43.430193Z","shell.execute_reply.started":"2024-05-06T17:01:43.389674Z","shell.execute_reply":"2024-05-06T17:01:43.429074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### MODELAMIENTO","metadata":{}},{"cell_type":"code","source":"mod_forest=DecisionTreeClassifier(max_depth=7,max_features=19,criterion='entropy',random_state=12023,min_samples_split=2,min_samples_leaf=2)\nmod_forest.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:45.032843Z","iopub.execute_input":"2024-05-06T17:01:45.033255Z","iopub.status.idle":"2024-05-06T17:01:45.331838Z","shell.execute_reply.started":"2024-05-06T17:01:45.033223Z","shell.execute_reply":"2024-05-06T17:01:45.330739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predecir(x_valid,y_valid,x_test,y_test,model,umbral=0.5):\n    print(\"test\")\n    pred=model.predict(x_test)\n    pred_prob=model.predict_proba(x_test)[:,1]\n    print(confusion_matrix(y_test,pred_prob>umbral))\n    print(classification_report(y_test,pred_prob>umbral))\n    print(\"auc\",roc_auc_score(y_test,pred_prob))\n    print(\"\\n\")\n    print(\"validacion\")\n    pred_val=model.predict(x_valid)\n    pred_prob_val=model.predict_proba(x_valid)[:,1]\n    print(confusion_matrix(y_valid,pred_prob_val>umbral))\n    print(classification_report(y_valid,pred_prob_val>umbral))\n    print(\"AUC\",roc_auc_score(y_valid,pred_prob_val))\n    return pred_val,pred_prob_val","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:47.667241Z","iopub.execute_input":"2024-05-06T17:01:47.668623Z","iopub.status.idle":"2024-05-06T17:01:47.676084Z","shell.execute_reply.started":"2024-05-06T17:01:47.668571Z","shell.execute_reply":"2024-05-06T17:01:47.674761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predecir(valid[valid[\"codmes\"]==\"202401\"][features],valid[valid[\"codmes\"]==\"202401\"][\"target\"],x_test,y_test,mod_forest,umbral=0.5)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:48.616979Z","iopub.execute_input":"2024-05-06T17:01:48.617408Z","iopub.status.idle":"2024-05-06T17:01:48.673617Z","shell.execute_reply.started":"2024-05-06T17:01:48.617373Z","shell.execute_reply":"2024-05-06T17:01:48.672348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predecir(valid[valid[\"codmes\"]==\"202402\"][features],valid[valid[\"codmes\"]==\"202402\"][\"target\"],x_test,y_test,mod_forest,umbral=0.5)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:57.832885Z","iopub.execute_input":"2024-05-06T17:01:57.833269Z","iopub.status.idle":"2024-05-06T17:01:57.888031Z","shell.execute_reply.started":"2024-05-06T17:01:57.833238Z","shell.execute_reply":"2024-05-06T17:01:57.886882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predecir(valid[features],valid[\"target\"],x_test,y_test,mod_forest,umbral=0.5)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:01:59.340548Z","iopub.execute_input":"2024-05-06T17:01:59.341047Z","iopub.status.idle":"2024-05-06T17:01:59.414565Z","shell.execute_reply.started":"2024-05-06T17:01:59.341014Z","shell.execute_reply":"2024-05-06T17:01:59.413074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### SUBMISSION","metadata":{}},{"cell_type":"code","source":"pred_202403_04=mod_forest.predict_proba(df_merge_predict[features])[:,1]\npred_202403_04=(pred_202403_04>0.5).astype(int)\npd.Series(pred_202403_04).value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:02:06.768024Z","iopub.execute_input":"2024-05-06T17:02:06.769256Z","iopub.status.idle":"2024-05-06T17:02:06.782556Z","shell.execute_reply.started":"2024-05-06T17:02:06.769203Z","shell.execute_reply":"2024-05-06T17:02:06.781463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_merge_predict_sub=df_merge_predict[[\"codmes\",\"RUC_PROVEEDOR\"]].copy()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:02:08.019058Z","iopub.execute_input":"2024-05-06T17:02:08.019670Z","iopub.status.idle":"2024-05-06T17:02:08.029375Z","shell.execute_reply.started":"2024-05-06T17:02:08.019634Z","shell.execute_reply":"2024-05-06T17:02:08.026784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_merge_predict_sub[\"CODMES_RUC_PROVEEDOR\"]=df_merge_predict_sub[\"codmes\"].astype(str)+\"_\"+df_merge_predict_sub[\"RUC_PROVEEDOR\"].astype(str)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:02:09.067673Z","iopub.execute_input":"2024-05-06T17:02:09.068537Z","iopub.status.idle":"2024-05-06T17:02:09.080175Z","shell.execute_reply.started":"2024-05-06T17:02:09.068493Z","shell.execute_reply":"2024-05-06T17:02:09.078448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_merge_predict_sub[\"TARGET\"]=pred_202403_04","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:02:24.834676Z","iopub.execute_input":"2024-05-06T17:02:24.835084Z","iopub.status.idle":"2024-05-06T17:02:24.841040Z","shell.execute_reply.started":"2024-05-06T17:02:24.835053Z","shell.execute_reply":"2024-05-06T17:02:24.839668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission=pd.read_csv(\"/kaggle/input/desafo-clasificacin-de-proveedores-activos/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:02:24.843887Z","iopub.execute_input":"2024-05-06T17:02:24.844387Z","iopub.status.idle":"2024-05-06T17:02:24.868729Z","shell.execute_reply.started":"2024-05-06T17:02:24.844324Z","shell.execute_reply":"2024-05-06T17:02:24.867777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predit_sub=pd.merge(df_submission[[\"CODMES_RUC_PROVEEDOR\"]],df_merge_predict_sub[[\"CODMES_RUC_PROVEEDOR\",\"TARGET\"]],on=\"CODMES_RUC_PROVEEDOR\")","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:02:30.613381Z","iopub.execute_input":"2024-05-06T17:02:30.614392Z","iopub.status.idle":"2024-05-06T17:02:30.632702Z","shell.execute_reply.started":"2024-05-06T17:02:30.614316Z","shell.execute_reply":"2024-05-06T17:02:30.630775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predit_sub.to_csv(\"submission.csv\",index=None,header=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T17:02:32.009014Z","iopub.execute_input":"2024-05-06T17:02:32.009520Z","iopub.status.idle":"2024-05-06T17:02:32.028844Z","shell.execute_reply.started":"2024-05-06T17:02:32.009484Z","shell.execute_reply":"2024-05-06T17:02:32.026996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}