{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":4477,"databundleVersionId":44220,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport zipfile\n\nbase_path = \"/kaggle/input/grasp-and-lift-eeg-detection\"\n\n# Zip dosyalarını çıkar\nwith zipfile.ZipFile(f\"{base_path}/train.zip\", 'r') as zip_ref:\n    zip_ref.extractall(\"/kaggle/working/train\")\n\nwith zipfile.ZipFile(f\"{base_path}/test.zip\", 'r') as zip_ref:\n    zip_ref.extractall(\"/kaggle/working/test\")\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-12T13:40:10.707358Z","iopub.execute_input":"2025-06-12T13:40:10.708308Z","iopub.status.idle":"2025-06-12T13:40:47.296687Z","shell.execute_reply.started":"2025-06-12T13:40:10.708268Z","shell.execute_reply":"2025-06-12T13:40:47.295582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndef prepare_training_data(subject):\n    X_list, y_list = [], []\n    path = \"/kaggle/working/train/train\"\n    for series in range(1, 9):\n        data = pd.read_csv(f\"{path}/subj{subject}_series{series}_data.csv\").drop(\"id\", axis=1)\n        events = pd.read_csv(f\"{path}/subj{subject}_series{series}_events.csv\").drop(\"id\", axis=1)\n        X_list.append(data)\n        y_list.append(events)\n    return pd.concat(X_list).reset_index(drop=True), pd.concat(y_list).reset_index(drop=True)\n\nX_raw, y_raw = prepare_training_data(1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T13:40:54.309223Z","iopub.execute_input":"2025-06-12T13:40:54.309644Z","iopub.status.idle":"2025-06-12T13:41:05.645194Z","shell.execute_reply.started":"2025-06-12T13:40:54.309613Z","shell.execute_reply":"2025-06-12T13:41:05.64366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\ndef sliding_window_features(df, window_size=50, step=25):\n    features, indices = [], []\n    for start in range(0, len(df) - window_size + 1, step):\n        window = df.iloc[start:start+window_size]\n        feat = np.concatenate([\n            window.mean().values,\n            window.std().values,\n            window.min().values,\n            window.max().values\n        ])\n        features.append(feat)\n        indices.append(start + window_size // 2)\n    return pd.DataFrame(features), indices\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T13:41:29.52444Z","iopub.execute_input":"2025-06-12T13:41:29.524827Z","iopub.status.idle":"2025-06-12T13:41:29.532413Z","shell.execute_reply.started":"2025-06-12T13:41:29.524799Z","shell.execute_reply":"2025-06-12T13:41:29.531344Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\n\nlabels = y_raw.columns.tolist()\nmodels = {}\n\nfor label in labels:\n    X_feat, idx = sliding_window_features(X_raw)\n    y_label = y_raw[label].iloc[idx].reset_index(drop=True)\n    \n    model = lgb.LGBMClassifier(random_state=42)\n    model.fit(X_feat, y_label)\n    models[label] = model\n    print(f\"{label} modeli eğitildi.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T13:42:00.701865Z","iopub.execute_input":"2025-06-12T13:42:00.702271Z","iopub.status.idle":"2025-06-12T13:46:40.729181Z","shell.execute_reply.started":"2025-06-12T13:42:00.702239Z","shell.execute_reply":"2025-06-12T13:46:40.727952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv1D, MaxPooling1D, Flatten, Dense\n\ndef prepare_dl_data(X, y, window_size=50, step=25):\n    segments, labels = [], []\n    for start in range(0, len(X) - window_size + 1, step):\n        end = start + window_size\n        segments.append(X.iloc[start:end].values)\n        labels.append(y.iloc[end // 2].values)\n    return np.array(segments), np.array(labels)\n\nX_segments, y_segments = prepare_dl_data(X_raw, y_raw)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T13:49:34.37163Z","iopub.execute_input":"2025-06-12T13:49:34.371951Z","iopub.status.idle":"2025-06-12T13:49:37.194743Z","shell.execute_reply.started":"2025-06-12T13:49:34.371931Z","shell.execute_reply":"2025-06-12T13:49:37.193776Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cnn_model = Sequential([\n    Conv1D(64, kernel_size=3, activation='relu', input_shape=(X_segments.shape[1], X_segments.shape[2])),\n    MaxPooling1D(pool_size=2),\n    Flatten(),\n    Dense(100, activation='relu'),\n    Dense(y_segments.shape[1], activation='sigmoid')\n])\n\ncnn_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['AUC'])\ncnn_model.fit(X_segments, y_segments, epochs=5, batch_size=64, validation_split=0.2)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T13:49:52.928129Z","iopub.execute_input":"2025-06-12T13:49:52.928493Z","iopub.status.idle":"2025-06-12T13:50:27.908223Z","shell.execute_reply.started":"2025-06-12T13:49:52.928471Z","shell.execute_reply":"2025-06-12T13:50:27.907163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\nfrom sklearn.model_selection import train_test_split\n\nfor label in labels:\n    X_feat, idx = sliding_window_features(X_raw)\n    y_label = y_raw[label].iloc[idx].reset_index(drop=True)\n    \n    X_tr, X_val, y_tr, y_val = train_test_split(X_feat, y_label, test_size=0.2, random_state=42)\n    model = lgb.LGBMClassifier(random_state=42)\n    model.fit(X_tr, y_tr)\n    \n    y_pred = model.predict_proba(X_val)[:, 1]\n    auc = roc_auc_score(y_val, y_pred)\n    print(f\"{label} ROC AUC: {auc:.4f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T13:54:02.000367Z","iopub.execute_input":"2025-06-12T13:54:02.000892Z","iopub.status.idle":"2025-06-12T13:58:50.084121Z","shell.execute_reply.started":"2025-06-12T13:54:02.000865Z","shell.execute_reply":"2025-06-12T13:58:50.083056Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import glob\n\ndef process_test_files(test_path=\"/kaggle/working/test/test\", window_size=50, step=25):\n    preds = []\n    test_files = glob.glob(f\"{test_path}/*.csv\")\n\n    for file in test_files:\n        df = pd.read_csv(file)\n        ids = df[\"id\"] if \"id\" in df.columns else pd.Series([f\"{os.path.basename(file)}_{i}\" for i in range(len(df))])\n        X_test_feat, idx = sliding_window_features(df.drop(\"id\", axis=1, errors=\"ignore\"), window_size, step)\n        window_ids = ids.iloc[idx].reset_index(drop=True)\n        \n        row = pd.DataFrame({\"id\": window_ids})\n        for label in labels:\n            row[label] = models[label].predict_proba(X_test_feat)[:, 1]\n        preds.append(row)\n    \n    return pd.concat(preds, ignore_index=True)\n\nsubmission = process_test_files()\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"✅ Submission dosyası oluşturuldu.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T13:59:08.893728Z","iopub.execute_input":"2025-06-12T13:59:08.894062Z","iopub.status.idle":"2025-06-12T14:00:58.114992Z","shell.execute_reply.started":"2025-06-12T13:59:08.894037Z","shell.execute_reply":"2025-06-12T14:00:58.113943Z"}},"outputs":[],"execution_count":null}]}