{"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":"gpu","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\ndf = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/train.csv\")\ndf","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:02.753007Z","iopub.execute_input":"2024-03-28T14:29:02.753395Z","iopub.status.idle":"2024-03-28T14:29:04.014821Z","shell.execute_reply.started":"2024-03-28T14:29:02.753362Z","shell.execute_reply":"2024-03-28T14:29:04.013752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = df[[\"seizure_vote\", \"lpd_vote\",\"gpd_vote\",  \"lrda_vote\", \"grda_vote\", \"other_vote\", \"expert_consensus\"]]\ndf1.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:04.016725Z","iopub.execute_input":"2024-03-28T14:29:04.017040Z","iopub.status.idle":"2024-03-28T14:29:04.035037Z","shell.execute_reply.started":"2024-03-28T14:29:04.017015Z","shell.execute_reply":"2024-03-28T14:29:04.034143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.expert_consensus.unique()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:04.036187Z","iopub.execute_input":"2024-03-28T14:29:04.036480Z","iopub.status.idle":"2024-03-28T14:29:04.055351Z","shell.execute_reply.started":"2024-03-28T14:29:04.036455Z","shell.execute_reply":"2024-03-28T14:29:04.054291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:04.056537Z","iopub.execute_input":"2024-03-28T14:29:04.056813Z","iopub.status.idle":"2024-03-28T14:29:04.078648Z","shell.execute_reply.started":"2024-03-28T14:29:04.056790Z","shell.execute_reply":"2024-03-28T14:29:04.077755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\ndf1[\"expert_consensus\"] = le.fit_transform(df1[\"expert_consensus\"])\ndf1.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:04.081580Z","iopub.execute_input":"2024-03-28T14:29:04.082133Z","iopub.status.idle":"2024-03-28T14:29:05.262309Z","shell.execute_reply.started":"2024-03-28T14:29:04.082079Z","shell.execute_reply":"2024-03-28T14:29:05.261255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df1.iloc[:, :-1].values\ny = df1.iloc[:, -1].values","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:05.263627Z","iopub.execute_input":"2024-03-28T14:29:05.264062Z","iopub.status.idle":"2024-03-28T14:29:05.269744Z","shell.execute_reply.started":"2024-03-28T14:29:05.264033Z","shell.execute_reply":"2024-03-28T14:29:05.268820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:05.270955Z","iopub.execute_input":"2024-03-28T14:29:05.271345Z","iopub.status.idle":"2024-03-28T14:29:05.370180Z","shell.execute_reply.started":"2024-03-28T14:29:05.271295Z","shell.execute_reply":"2024-03-28T14:29:05.369386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:05.371253Z","iopub.execute_input":"2024-03-28T14:29:05.371530Z","iopub.status.idle":"2024-03-28T14:29:05.376681Z","shell.execute_reply.started":"2024-03-28T14:29:05.371506Z","shell.execute_reply":"2024-03-28T14:29:05.375689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:05.377985Z","iopub.execute_input":"2024-03-28T14:29:05.378350Z","iopub.status.idle":"2024-03-28T14:29:05.389582Z","shell.execute_reply.started":"2024-03-28T14:29:05.378317Z","shell.execute_reply":"2024-03-28T14:29:05.388667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:05.390745Z","iopub.execute_input":"2024-03-28T14:29:05.391136Z","iopub.status.idle":"2024-03-28T14:29:05.400626Z","shell.execute_reply.started":"2024-03-28T14:29:05.391085Z","shell.execute_reply":"2024-03-28T14:29:05.399692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:05.401809Z","iopub.execute_input":"2024-03-28T14:29:05.402150Z","iopub.status.idle":"2024-03-28T14:29:05.410940Z","shell.execute_reply.started":"2024-03-28T14:29:05.402119Z","shell.execute_reply":"2024-03-28T14:29:05.409932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\nrf_model = RandomForestClassifier(random_state=0)\n\nrf_model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:05.412049Z","iopub.execute_input":"2024-03-28T14:29:05.412384Z","iopub.status.idle":"2024-03-28T14:29:09.221065Z","shell.execute_reply.started":"2024-03-28T14:29:05.412353Z","shell.execute_reply":"2024-03-28T14:29:09.220140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import GridSearchCV\nimport pandas as pd\n\n# Parametre aralıklarını belirle\nparametreler = {\n    'n_estimators': [50, 100],\n    'max_depth': [5, 10],\n    'min_samples_split': [2, 5],\n    'min_samples_leaf': [2, 4],\n    'max_features': ['auto', 'sqrt']\n}\n\n# GridSearchCV oluştur\ngrid_search = GridSearchCV(estimator=rf_model, param_grid=parametreler, cv=3, n_jobs=-1, verbose=2)\n\n# GridSearchCV'yi veriye uygula\ngrid_search.fit(X, y)\n\n# En iyi parametreleri ve en iyi skoru yazdır\nprint(\"En iyi parametreler:\", grid_search.best_params_)\nprint(\"En iyi skor:\", grid_search.best_score_)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:29:09.222281Z","iopub.execute_input":"2024-03-28T14:29:09.222576Z","iopub.status.idle":"2024-03-28T14:30:13.319315Z","shell.execute_reply.started":"2024-03-28T14:29:09.222551Z","shell.execute_reply":"2024-03-28T14:30:13.318113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\n# En iyi parametreleri kullanarak RandomForestClassifier oluştur\nrf_model_best = RandomForestClassifier(max_depth=10,\n                                       max_features='sqrt',\n                                       min_samples_leaf=4,\n                                       min_samples_split=2,\n                                       n_estimators=100)\n\n# Veri seti ile modeli eğit\nrf_model_best.fit(X, y)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:13.323698Z","iopub.execute_input":"2024-03-28T14:30:13.323991Z","iopub.status.idle":"2024-03-28T14:30:16.682274Z","shell.execute_reply.started":"2024-03-28T14:30:13.323963Z","shell.execute_reply":"2024-03-28T14:30:16.681386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = rf_model_best.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:16.683334Z","iopub.execute_input":"2024-03-28T14:30:16.683588Z","iopub.status.idle":"2024-03-28T14:30:16.968135Z","shell.execute_reply.started":"2024-03-28T14:30:16.683566Z","shell.execute_reply":"2024-03-28T14:30:16.967140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.metrics import confusion_matrix\ncm = confusion_matrix(y_test, y_pred)\n\n# Heatmap'i çizme\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\")\n\n# Eksen etiketlerini ayarlama\nplt.title(\"Confusion Matrix\")\nplt.xlabel(\"Tahmin Edilen Etiketler\")\nplt.ylabel(\"Gerçek Etiketler\")\n\n# Grafiği gösterme\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:16.969318Z","iopub.execute_input":"2024-03-28T14:30:16.969615Z","iopub.status.idle":"2024-03-28T14:30:17.680195Z","shell.execute_reply.started":"2024-03-28T14:30:16.969591Z","shell.execute_reply":"2024-03-28T14:30:17.679144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\naccuracy = accuracy_score(y_test, y_pred)\nprint(\"Doğruluk:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:17.681358Z","iopub.execute_input":"2024-03-28T14:30:17.681759Z","iopub.status.idle":"2024-03-28T14:30:17.688732Z","shell.execute_reply.started":"2024-03-28T14:30:17.681734Z","shell.execute_reply":"2024-03-28T14:30:17.687831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import precision_score, recall_score\n\nprecision = precision_score(y_test, y_pred, average='weighted')\nrecall = recall_score(y_test, y_pred, average='weighted')\n\nprint(\"Kesinlik:\", precision)\nprint(\"Hassasiyet:\", recall)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:17.689924Z","iopub.execute_input":"2024-03-28T14:30:17.690243Z","iopub.status.idle":"2024-03-28T14:30:17.710826Z","shell.execute_reply.started":"2024-03-28T14:30:17.690217Z","shell.execute_reply":"2024-03-28T14:30:17.709775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import f1_score\n\nf1 = f1_score(y_test, y_pred, average='weighted')\nprint(\"F1 Skoru:\", f1)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:17.712299Z","iopub.execute_input":"2024-03-28T14:30:17.713460Z","iopub.status.idle":"2024-03-28T14:30:17.723425Z","shell.execute_reply.started":"2024-03-28T14:30:17.713421Z","shell.execute_reply":"2024-03-28T14:30:17.722346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nunseen = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/sample_submission.csv\")\nunseen","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:17.724840Z","iopub.execute_input":"2024-03-28T14:30:17.725193Z","iopub.status.idle":"2024-03-28T14:30:17.745610Z","shell.execute_reply.started":"2024-03-28T14:30:17.725166Z","shell.execute_reply":"2024-03-28T14:30:17.744693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unseen1 = unseen.iloc[:, 1:]","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:17.746889Z","iopub.execute_input":"2024-03-28T14:30:17.747311Z","iopub.status.idle":"2024-03-28T14:30:17.752623Z","shell.execute_reply.started":"2024-03-28T14:30:17.747276Z","shell.execute_reply":"2024-03-28T14:30:17.751637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = rf_model_best.predict(unseen1)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:17.753833Z","iopub.execute_input":"2024-03-28T14:30:17.754198Z","iopub.status.idle":"2024-03-28T14:30:17.775190Z","shell.execute_reply.started":"2024-03-28T14:30:17.754169Z","shell.execute_reply":"2024-03-28T14:30:17.774252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:17.776834Z","iopub.execute_input":"2024-03-28T14:30:17.777262Z","iopub.status.idle":"2024-03-28T14:30:17.784517Z","shell.execute_reply.started":"2024-03-28T14:30:17.777225Z","shell.execute_reply":"2024-03-28T14:30:17.783473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = le.inverse_transform(y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:17.785752Z","iopub.execute_input":"2024-03-28T14:30:17.786060Z","iopub.status.idle":"2024-03-28T14:30:17.795126Z","shell.execute_reply.started":"2024-03-28T14:30:17.786020Z","shell.execute_reply":"2024-03-28T14:30:17.794034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unseen[\"predict\"] = y_pred\nunseen.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:17.797844Z","iopub.execute_input":"2024-03-28T14:30:17.798208Z","iopub.status.idle":"2024-03-28T14:30:17.815285Z","shell.execute_reply.started":"2024-03-28T14:30:17.798181Z","shell.execute_reply":"2024-03-28T14:30:17.814349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unseen.drop(columns=[\"predict\"], inplace=True) ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Veri setini CSV dosyasına kaydetme\nunseen.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:30:17.816569Z","iopub.execute_input":"2024-03-28T14:30:17.816854Z","iopub.status.idle":"2024-03-28T14:30:17.827872Z","shell.execute_reply.started":"2024-03-28T14:30:17.816829Z","shell.execute_reply":"2024-03-28T14:30:17.827074Z"},"trusted":true},"execution_count":null,"outputs":[]}]}