{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Ahmed Gaitani Code ✨","metadata":{}},{"cell_type":"markdown","source":"# ✔Random Forest VS ✔XGBoost","metadata":{}},{"cell_type":"markdown","source":"***Import Necessary Libraries***","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom xgboost import XGBClassifier\nfrom sklearn.metrics import confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2024-06-27T20:47:08.825979Z","iopub.execute_input":"2024-06-27T20:47:08.826363Z","iopub.status.idle":"2024-06-27T20:47:08.833855Z","shell.execute_reply.started":"2024-06-27T20:47:08.826335Z","shell.execute_reply":"2024-06-27T20:47:08.832290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Load and Preprocess the Data***","metadata":{}},{"cell_type":"code","source":"# Load the data\ntrain_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# Separate features and labels\nX = train_data.drop('label', axis=1)\ny = train_data['label']\n\n# Normalize the pixel values\nX = X / 255.0\ntest_data = test_data / 255.0\n\n# Split the training data for validation\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T20:47:09.904414Z","iopub.execute_input":"2024-06-27T20:47:09.904839Z","iopub.status.idle":"2024-06-27T20:47:15.652881Z","shell.execute_reply.started":"2024-06-27T20:47:09.904808Z","shell.execute_reply":"2024-06-27T20:47:15.651671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Train a Random Forest Model**","metadata":{}},{"cell_type":"code","source":"# Initialize and train the Random Forest model\nrf_model = RandomForestClassifier(n_estimators=100, random_state=42)\nrf_model.fit(X_train, y_train)\n\n# Make predictions on the validation set\ny_val_pred_rf = rf_model.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T20:22:53.967122Z","iopub.execute_input":"2024-06-27T20:22:53.967474Z","iopub.status.idle":"2024-06-27T20:23:21.444613Z","shell.execute_reply.started":"2024-06-27T20:22:53.967443Z","shell.execute_reply":"2024-06-27T20:23:21.443237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Train an XGBoost ModelRandom Forest VS XGBoost**","metadata":{}},{"cell_type":"code","source":"# Initialize and train the XGBoost model\nxgb_model = XGBClassifier(n_estimators=100, random_state=42, use_label_encoder=False, eval_metric='mlogloss')\nxgb_model.fit(X_train, y_train)\n\n# Make predictions on the validation set\ny_val_pred_xgb = xgb_model.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T20:23:25.665143Z","iopub.execute_input":"2024-06-27T20:23:25.665579Z","iopub.status.idle":"2024-06-27T20:27:01.419023Z","shell.execute_reply.started":"2024-06-27T20:23:25.665548Z","shell.execute_reply":"2024-06-27T20:27:01.417899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Evaluate the Models***","metadata":{}},{"cell_type":"code","source":"# Evaluate Random Forest Model\nprint(\"Random Forest Model Evaluation:\")\nprint(classification_report(y_val, y_val_pred_rf))\n\n# Evaluate XGBoost Model\nprint(\"XGBoost Model Evaluation:\")\nprint(classification_report(y_val, y_val_pred_xgb))","metadata":{"execution":{"iopub.status.busy":"2024-06-27T20:27:39.099802Z","iopub.execute_input":"2024-06-27T20:27:39.100226Z","iopub.status.idle":"2024-06-27T20:27:39.167310Z","shell.execute_reply.started":"2024-06-27T20:27:39.100194Z","shell.execute_reply":"2024-06-27T20:27:39.165877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Visualizations***","metadata":{}},{"cell_type":"code","source":"def plot_confusion_matrix(y_true, y_pred, title):\n    cm = confusion_matrix(y_true, y_pred)\n    plt.figure(figsize=(10, 8))\n    sns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\")\n    plt.title(title)\n    plt.xlabel('Predicted')\n    plt.ylabel('Actual')\n    plt.show()\n\n# Plot confusion matrix for Random Forest\nplot_confusion_matrix(y_val, y_val_pred_rf, \"Confusion Matrix for Random Forest\")\n\n# Plot confusion matrix for XGBoost\nplot_confusion_matrix(y_val, y_val_pred_xgb, \"Confusion Matrix for XGBoost\")","metadata":{"execution":{"iopub.status.busy":"2024-06-27T20:35:42.304408Z","iopub.execute_input":"2024-06-27T20:35:42.305366Z","iopub.status.idle":"2024-06-27T20:35:43.695094Z","shell.execute_reply.started":"2024-06-27T20:35:42.305325Z","shell.execute_reply":"2024-06-27T20:35:43.693781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Feature importance for Random Forest\nimportances_rf = rf_model.feature_importances_\nindices_rf = np.argsort(importances_rf)[-10:]\n\nplt.figure(figsize=(12, 8))\nplt.title('Top 10 Feature Importances - Random Forest')\nplt.barh(range(len(indices_rf)), importances_rf[indices_rf], align='center')\nplt.yticks(range(len(indices_rf)), indices_rf)\nplt.xlabel('Relative Importance')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T20:35:48.349957Z","iopub.execute_input":"2024-06-27T20:35:48.350374Z","iopub.status.idle":"2024-06-27T20:35:48.711989Z","shell.execute_reply.started":"2024-06-27T20:35:48.350343Z","shell.execute_reply":"2024-06-27T20:35:48.710495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Feature importance for XGBoost\nimportances_xgb = xgb_model.feature_importances_\nindices_xgb = np.argsort(importances_xgb)[-10:]\n\nplt.figure(figsize=(12, 8))\nplt.title('Top 10 Feature Importances - XGBoost')\nplt.barh(range(len(indices_xgb)), importances_xgb[indices_xgb], align='center')\nplt.yticks(range(len(indices_xgb)), indices_xgb)\nplt.xlabel('Relative Importance')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T20:35:52.639073Z","iopub.execute_input":"2024-06-27T20:35:52.639592Z","iopub.status.idle":"2024-06-27T20:35:52.937747Z","shell.execute_reply.started":"2024-06-27T20:35:52.639540Z","shell.execute_reply":"2024-06-27T20:35:52.936364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test data\ntest_pred_rf = rf_model.predict(test_data)\ntest_pred_xgb = xgb_model.predict(test_data)\n\n# Prepare submission file for Random Forest\nsubmission_rf = pd.DataFrame({'ImageId': range(1, len(test_pred_rf) + 1), 'Label': test_pred_rf})\nsubmission_rf.to_csv('submission_rf.csv', index=False)\n\n# Prepare submission file for XGBoost\nsubmission_xgb = pd.DataFrame({'ImageId': range(1, len(test_pred_xgb) + 1), 'Label': test_pred_xgb})\nsubmission_xgb.to_csv('submission_xgb.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T20:37:04.295199Z","iopub.execute_input":"2024-06-27T20:37:04.295763Z","iopub.status.idle":"2024-06-27T20:37:06.134175Z","shell.execute_reply.started":"2024-06-27T20:37:04.295659Z","shell.execute_reply":"2024-06-27T20:37:06.133053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}