File size: 7,457 Bytes
aa79d7c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "75418eb6",
"metadata": {
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
"_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
"execution": {
"iopub.execute_input": "2023-07-06T13:42:23.141738Z",
"iopub.status.busy": "2023-07-06T13:42:23.141143Z",
"iopub.status.idle": "2023-07-06T13:42:23.155666Z",
"shell.execute_reply": "2023-07-06T13:42:23.154445Z"
},
"papermill": {
"duration": 0.021833,
"end_time": "2023-07-06T13:42:23.158621",
"exception": false,
"start_time": "2023-07-06T13:42:23.136788",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/kaggle/input/icr-identify-age-related-conditions/sample_submission.csv\n",
"/kaggle/input/icr-identify-age-related-conditions/greeks.csv\n",
"/kaggle/input/icr-identify-age-related-conditions/train.csv\n",
"/kaggle/input/icr-identify-age-related-conditions/test.csv\n"
]
}
],
"source": [
"# This Python 3 environment comes with many helpful analytics libraries installed\n",
"# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n",
"# For example, here's several helpful packages to load\n",
"\n",
"import numpy as np # linear algebra\n",
"import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
"\n",
"# Input data files are available in the read-only \"../input/\" directory\n",
"# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n",
"\n",
"import os\n",
"for dirname, _, filenames in os.walk('/kaggle/input'):\n",
" for filename in filenames:\n",
" print(os.path.join(dirname, filename))\n",
"\n",
"# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n",
"# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "21694925",
"metadata": {
"execution": {
"iopub.execute_input": "2023-07-06T13:42:23.164800Z",
"iopub.status.busy": "2023-07-06T13:42:23.164345Z",
"iopub.status.idle": "2023-07-06T13:43:47.729268Z",
"shell.execute_reply": "2023-07-06T13:43:47.728318Z"
},
"papermill": {
"duration": 84.570727,
"end_time": "2023-07-06T13:43:47.731786",
"exception": false,
"start_time": "2023-07-06T13:42:23.161059",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.preprocessing import StandardScaler\n",
"from sklearn.impute import SimpleImputer\n",
"from imblearn.over_sampling import RandomOverSampler\n",
"from sklearn.model_selection import GridSearchCV\n",
"from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, VotingClassifier\n",
"\n",
"# Open file with pd.read_csv\n",
"df_train = pd.read_csv(\"/kaggle/input/icr-identify-age-related-conditions/train.csv\")\n",
"df_test = pd.read_csv(\"/kaggle/input/icr-identify-age-related-conditions/test.csv\")\n",
"\n",
"# Convert 'A' and 'B' values in 'EJ' column to 0 and 1 respectively\n",
"df_train['EJ'] = df_train['EJ'].map({'A': 0, 'B': 1})\n",
"df_test['EJ'] = df_test['EJ'].map({'A': 0, 'B': 1})\n",
"\n",
"# Split the training data into features (X) and target variable (y)\n",
"X_train = df_train.drop([\"Class\", \"Id\"], axis=1) # Exclude non-numeric columns\n",
"y_train = df_train[\"Class\"]\n",
"\n",
"# Split the test data into features (X_test)\n",
"X_test = df_test.drop(\"Id\", axis=1)\n",
"\n",
"# Identify columns with missing values\n",
"columns_with_missing = X_train.columns[X_train.isna().any()].tolist()\n",
"\n",
"# Impute missing values with the mean of each column\n",
"imputer = SimpleImputer(strategy='mean')\n",
"X_train_imputed = imputer.fit_transform(X_train)\n",
"X_test_imputed = imputer.transform(X_test)\n",
"\n",
"# Scale the features using StandardScaler\n",
"scaler = StandardScaler()\n",
"X_train_scaled = scaler.fit_transform(X_train_imputed)\n",
"X_test_scaled = scaler.transform(X_test_imputed)\n",
"\n",
"# Handling class imbalance using oversampling\n",
"oversampler = RandomOverSampler(random_state=42)\n",
"X_train_scaled, y_train = oversampler.fit_resample(X_train_scaled, y_train)\n",
"\n",
"# Hyperparameter tuning for Random Forest Classifier\n",
"rfc = RandomForestClassifier(n_estimators=100, random_state=42)\n",
"param_grid = {'max_depth': [None, 5, 10], 'min_samples_split': [2, 5, 10]}\n",
"grid_search = GridSearchCV(rfc, param_grid, cv=5, scoring='neg_log_loss')\n",
"grid_search.fit(X_train_scaled, y_train)\n",
"best_rfc = grid_search.best_estimator_\n",
"\n",
"# Hyperparameter tuning for Gradient Boosting Classifier\n",
"gbc = GradientBoostingClassifier(n_estimators=100, random_state=42)\n",
"param_grid = {'max_depth': [3, 5, 7], 'learning_rate': [0.01, 0.1, 1.0]}\n",
"grid_search = GridSearchCV(gbc, param_grid, cv=5, scoring='neg_log_loss')\n",
"grid_search.fit(X_train_scaled, y_train)\n",
"best_gbc = grid_search.best_estimator_\n",
"\n",
"# Ensemble of models\n",
"ensemble_model = VotingClassifier(estimators=[('rfc', best_rfc), ('gbc', best_gbc)], voting='soft')\n",
"ensemble_model.fit(X_train_scaled, y_train)\n",
"\n",
"# Predict probabilities for each class in the test set\n",
"ensemble_pred_proba = ensemble_model.predict_proba(X_test_scaled)\n",
"\n",
"# Create a DataFrame to store the predictions\n",
"predictions_df = pd.DataFrame({'Id': df_test['Id'],\n",
" 'class_0': ensemble_pred_proba[:, 0],\n",
" 'class_1': ensemble_pred_proba[:, 1]})\n",
"\n",
"# Save the predictions to a CSV file\n",
"predictions_df.to_csv('submission.csv', index=False)\n",
" "
]
}
],
"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.10.10"
},
"papermill": {
"default_parameters": {},
"duration": 97.427632,
"end_time": "2023-07-06T13:43:48.755891",
"environment_variables": {},
"exception": null,
"input_path": "__notebook__.ipynb",
"output_path": "__notebook__.ipynb",
"parameters": {},
"start_time": "2023-07-06T13:42:11.328259",
"version": "2.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|