{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0d037fd2", "metadata": { "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5", "execution": { "iopub.execute_input": "2023-07-02T16:26:58.047008Z", "iopub.status.busy": "2023-07-02T16:26:58.046651Z", "iopub.status.idle": "2023-07-02T16:26:58.056466Z", "shell.execute_reply": "2023-07-02T16:26:58.055830Z" }, "papermill": { "duration": 0.015212, "end_time": "2023-07-02T16:26:58.058238", "exception": false, "start_time": "2023-07-02T16:26:58.043026", "status": "completed" }, "tags": [] }, "outputs": [], "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", " pass\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": "d8611678", "metadata": { "execution": { "iopub.execute_input": "2023-07-02T16:26:58.062283Z", "iopub.status.busy": "2023-07-02T16:26:58.061970Z", "iopub.status.idle": "2023-07-02T16:26:59.733761Z", "shell.execute_reply": "2023-07-02T16:26:59.732840Z" }, "papermill": { "duration": 1.676101, "end_time": "2023-07-02T16:26:59.735922", "exception": false, "start_time": "2023-07-02T16:26:58.059821", "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.svm import SVC\n", "from sklearn.preprocessing import StandardScaler\n", "from sklearn.impute import SimpleImputer\n", "from sklearn.metrics import log_loss\n", "from sklearn.feature_selection import SelectKBest, f_classif\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", "# Perform feature selection using SelectKBest and ANOVA F-value\n", "k = 20 # Number of top features to select\n", "feature_selector = SelectKBest(f_classif, k=k)\n", "X_train_selected = feature_selector.fit_transform(X_train_scaled, y_train)\n", "X_test_selected = feature_selector.transform(X_test_scaled)\n", "selected_feature_indices = feature_selector.get_support(indices=True)\n", "selected_features = X_train.columns[selected_feature_indices]\n", "\n", "# Create an SVM model with tuned parameters\n", "svm = SVC(kernel='rbf', C=10, gamma='scale', probability=True)\n", "\n", "# Train the SVM model\n", "svm.fit(X_train_selected, y_train)\n", "\n", "# Predict on the test set\n", "svm_pred = svm.predict(X_test_selected)\n", "\n", "# Predict probabilities for each class in the test set\n", "svm_pred_proba = svm.predict_proba(X_test_selected)\n", "\n", "# Create a DataFrame to store the predictions\n", "predictions_df = pd.DataFrame({'Id': df_test['Id'],\n", " 'class_0': svm_pred_proba[:, 0],\n", " 'class_1': svm_pred_proba[:, 1]})\n", "\n", "# Save the predictions to a CSV file\n", "predictions_df.to_csv('submission.csv', index=False)" ] } ], "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": 11.310487, "end_time": "2023-07-02T16:27:00.661219", "environment_variables": {}, "exception": null, "input_path": "__notebook__.ipynb", "output_path": "__notebook__.ipynb", "parameters": {}, "start_time": "2023-07-02T16:26:49.350732", "version": "2.4.0" } }, "nbformat": 4, "nbformat_minor": 5 }