{ "cells": [ { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "# Save and Load Model\n", "import pickle\n", "import json" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\luay9\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\sklearn\\base.py:299: UserWarning: Trying to unpickle estimator MinMaxScaler from version 1.2.2 when using version 1.2.1. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n", "C:\\Users\\luay9\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\sklearn\\base.py:299: UserWarning: Trying to unpickle estimator StandardScaler from version 1.2.2 when using version 1.2.1. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n", "C:\\Users\\luay9\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\sklearn\\base.py:299: UserWarning: Trying to unpickle estimator ColumnTransformer from version 1.2.2 when using version 1.2.1. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n", "C:\\Users\\luay9\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\sklearn\\base.py:299: UserWarning: Trying to unpickle estimator NearestNeighbors from version 1.2.2 when using version 1.2.1. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n", "C:\\Users\\luay9\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\sklearn\\base.py:299: UserWarning: Trying to unpickle estimator DecisionTreeClassifier from version 1.2.2 when using version 1.2.1. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n", "C:\\Users\\luay9\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\sklearn\\base.py:299: UserWarning: Trying to unpickle estimator RandomForestClassifier from version 1.2.2 when using version 1.2.1. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n" ] } ], "source": [ "# Loading Model Files\n", "\n", "with open('rf_gridcv_best.pkl', 'rb') as file_1:\n", " rf_gridcv_best = pickle.load(file_1)\n", "\n", "with open('Drop_Columns.txt','r') as file_2:\n", " Drop_Columns = json.load(file_2)" ] }, { "cell_type": "code", "execution_count": 5, "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", "
ageanaemiacreatinine_phosphokinasediabetesejection_fractionhigh_blood_pressureplateletsserum_creatinineserum_sodiumsexsmokingtimeDEATH_EVENT
065.00.056.00.025.00.0237000.005.00130.00.00.0207.00.0
167.00.0213.00.038.00.0215000.001.20133.00.00.0245.00.0
275.00.0582.00.020.01.0265000.001.90130.01.00.04.01.0
372.01.0943.00.025.01.0338000.001.70139.01.01.0111.01.0
465.00.0198.01.035.01.0281000.000.90137.01.01.0146.00.0
555.00.07861.00.038.00.0263358.031.10136.01.00.06.01.0
675.01.081.00.038.01.0368000.004.00131.01.01.010.01.0
753.01.0707.00.038.00.0330000.001.40137.01.01.0209.00.0
862.00.061.01.038.01.0155000.001.10143.01.01.0270.00.0
975.00.0582.00.040.00.0263358.031.18137.01.00.0107.00.0
\n", "
" ], "text/plain": [ " age anaemia creatinine_phosphokinase diabetes ejection_fraction \\\n", "0 65.0 0.0 56.0 0.0 25.0 \n", "1 67.0 0.0 213.0 0.0 38.0 \n", "2 75.0 0.0 582.0 0.0 20.0 \n", "3 72.0 1.0 943.0 0.0 25.0 \n", "4 65.0 0.0 198.0 1.0 35.0 \n", "5 55.0 0.0 7861.0 0.0 38.0 \n", "6 75.0 1.0 81.0 0.0 38.0 \n", "7 53.0 1.0 707.0 0.0 38.0 \n", "8 62.0 0.0 61.0 1.0 38.0 \n", "9 75.0 0.0 582.0 0.0 40.0 \n", "\n", " high_blood_pressure platelets serum_creatinine serum_sodium sex \\\n", "0 0.0 237000.00 5.00 130.0 0.0 \n", "1 0.0 215000.00 1.20 133.0 0.0 \n", "2 1.0 265000.00 1.90 130.0 1.0 \n", "3 1.0 338000.00 1.70 139.0 1.0 \n", "4 1.0 281000.00 0.90 137.0 1.0 \n", "5 0.0 263358.03 1.10 136.0 1.0 \n", "6 1.0 368000.00 4.00 131.0 1.0 \n", "7 0.0 330000.00 1.40 137.0 1.0 \n", "8 1.0 155000.00 1.10 143.0 1.0 \n", "9 0.0 263358.03 1.18 137.0 1.0 \n", "\n", " smoking time DEATH_EVENT \n", "0 0.0 207.0 0.0 \n", "1 0.0 245.0 0.0 \n", "2 0.0 4.0 1.0 \n", "3 1.0 111.0 1.0 \n", "4 1.0 146.0 0.0 \n", "5 0.0 6.0 1.0 \n", "6 1.0 10.0 1.0 \n", "7 1.0 209.0 0.0 \n", "8 1.0 270.0 0.0 \n", "9 0.0 107.0 0.0 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# data inference\n", "df_inf = pd.read_csv('G:\\My Drive\\FTDS\\phase 1\\p1gc3\\h8dsft_p1g3_Ahmad Luay Adnani_inference.csv')\n", "df_inf_copy = df_inf.copy()\n", "df_inf_copy = df_inf.drop(['Unnamed: 0'],axis=1)\n", "df_inf_copy" ] }, { "cell_type": "code", "execution_count": 6, "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", "
agecreatinine_phosphokinaseejection_fractionplateletsserum_creatinineserum_sodiumtimeDEATH_EVENT
065.056.025.0237000.005.00130.0207.00.0
167.0213.038.0215000.001.20133.0245.00.0
275.0582.020.0265000.001.90130.04.01.0
372.0943.025.0338000.001.70139.0111.01.0
465.0198.035.0281000.000.90137.0146.00.0
555.07861.038.0263358.031.10136.06.01.0
675.081.038.0368000.004.00131.010.01.0
753.0707.038.0330000.001.40137.0209.00.0
862.061.038.0155000.001.10143.0270.00.0
975.0582.040.0263358.031.18137.0107.00.0
\n", "
" ], "text/plain": [ " age creatinine_phosphokinase ejection_fraction platelets \\\n", "0 65.0 56.0 25.0 237000.00 \n", "1 67.0 213.0 38.0 215000.00 \n", "2 75.0 582.0 20.0 265000.00 \n", "3 72.0 943.0 25.0 338000.00 \n", "4 65.0 198.0 35.0 281000.00 \n", "5 55.0 7861.0 38.0 263358.03 \n", "6 75.0 81.0 38.0 368000.00 \n", "7 53.0 707.0 38.0 330000.00 \n", "8 62.0 61.0 38.0 155000.00 \n", "9 75.0 582.0 40.0 263358.03 \n", "\n", " serum_creatinine serum_sodium time DEATH_EVENT \n", "0 5.00 130.0 207.0 0.0 \n", "1 1.20 133.0 245.0 0.0 \n", "2 1.90 130.0 4.0 1.0 \n", "3 1.70 139.0 111.0 1.0 \n", "4 0.90 137.0 146.0 0.0 \n", "5 1.10 136.0 6.0 1.0 \n", "6 4.00 131.0 10.0 1.0 \n", "7 1.40 137.0 209.0 0.0 \n", "8 1.10 143.0 270.0 0.0 \n", "9 1.18 137.0 107.0 0.0 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Removing unnecessary features\n", "df_inf_final = df_inf_copy.drop(Drop_Columns,axis=1).sort_index()\n", "df_inf_final" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1., 0., 1., 1., 0., 1., 1., 0., 0., 0.])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Predict using RandomForest\n", "\n", "y_pred_inf = rf_gridcv_best.predict(df_inf_final) #rf_gridcv_best from load files\n", "y_pred_inf" ] }, { "cell_type": "code", "execution_count": 9, "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", "
DEATH_EVENT_Prediction
01.0
10.0
21.0
31.0
40.0
51.0
61.0
70.0
80.0
90.0
\n", "
" ], "text/plain": [ " DEATH_EVENT_Prediction\n", "0 1.0\n", "1 0.0\n", "2 1.0\n", "3 1.0\n", "4 0.0\n", "5 1.0\n", "6 1.0\n", "7 0.0\n", "8 0.0\n", "9 0.0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create DEATH_EVENT Prediction DataFrame\n", "\n", "y_pred_inf_df = pd.DataFrame(y_pred_inf, columns=['DEATH_EVENT_Prediction'],index=df_inf_final.index)\n", "round(y_pred_inf_df.head(10),2)" ] }, { "cell_type": "code", "execution_count": 10, "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", "
DEATH_EVENTDEATH_EVENT_Prediction
00.01.0
10.00.0
21.01.0
31.01.0
40.00.0
51.01.0
61.01.0
70.00.0
80.00.0
90.00.0
\n", "
" ], "text/plain": [ " DEATH_EVENT DEATH_EVENT_Prediction\n", "0 0.0 1.0\n", "1 0.0 0.0\n", "2 1.0 1.0\n", "3 1.0 1.0\n", "4 0.0 0.0\n", "5 1.0 1.0\n", "6 1.0 1.0\n", "7 0.0 0.0\n", "8 0.0 0.0\n", "9 0.0 0.0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Comparing the real default_payment_next_month and predicted classification\n", "model_inf = pd.concat([df_inf_copy['DEATH_EVENT'], y_pred_inf_df['DEATH_EVENT_Prediction']], axis=1).sort_index()\n", "model_inf" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9.13" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }