{"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":11580522,"sourceType":"datasetVersion","datasetId":7261066}],"dockerImageVersionId":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Adım 1: Dataseti Okuma**","metadata":{}},{"cell_type":"code","source":"# Gerekli kütüphaneleri yükleyelim \n\nimport pandas as pd \n\nimport numpy as np \n\n  \n\n# Veriyi okuyalım \n\ndf = pd.read_csv('/kaggle/input/train-dataset/train_v2.csv') \n\n  \n\n# İlk birkaç satırı görelim \n\ndf.head() ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-04-27T17:38:02.396311Z","iopub.execute_input":"2025-04-27T17:38:02.396727Z","iopub.status.idle":"2025-04-27T17:45:05.481991Z","shell.execute_reply.started":"2025-04-27T17:38:02.396703Z","shell.execute_reply":"2025-04-27T17:45:05.480236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T17:55:34.832682Z","iopub.execute_input":"2025-04-27T17:55:34.833794Z","iopub.status.idle":"2025-04-27T17:55:34.843625Z","shell.execute_reply.started":"2025-04-27T17:55:34.833745Z","shell.execute_reply":"2025-04-27T17:55:34.842509Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isnull().sum()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T17:55:37.348799Z","iopub.execute_input":"2025-04-27T17:55:37.349093Z","iopub.status.idle":"2025-04-27T17:55:39.309895Z","shell.execute_reply.started":"2025-04-27T17:55:37.349072Z","shell.execute_reply":"2025-04-27T17:55:39.30892Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Adım 2: Veri İncelemesi ve Temizlik**","metadata":{}},{"cell_type":"code","source":"import json\n\n# 'totals' kolonundan transactionRevenue'yu çıkar\ndef extract_transaction_revenue(row):\n    try:\n        data = json.loads(row.replace(\"'\", '\"'))\n        return data.get('transactionRevenue', 0)\n    except:\n        return 0\n\ndf['transactionRevenue'] = df['totals'].apply(extract_transaction_revenue)\n# transactionRevenue sütununu sayıya çeviriyoruz\ndf['transactionRevenue'] = pd.to_numeric(df['transactionRevenue'], errors='coerce').fillna(0)\n\n\n# Kullanılmayan kolonları güvenli şekilde sil\ndf = df.drop(['socialEngagementType', 'hits', 'customDimensions', 'fullVisitorId'], axis=1, errors='ignore')\n\nprint(\"Yeni shape:\", df.shape)\nprint(df['transactionRevenue'].dtype)\nprint(df['transactionRevenue'].describe())\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T17:55:42.160919Z","iopub.execute_input":"2025-04-27T17:55:42.161251Z","iopub.status.idle":"2025-04-27T17:55:47.581573Z","shell.execute_reply.started":"2025-04-27T17:55:42.161228Z","shell.execute_reply":"2025-04-27T17:55:47.580529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import datetime\n\n# visitStartTime sütununu datetime tipine çeviriyoruz\ndf['visitStartTime'] = pd.to_datetime(df['visitStartTime'], unit='s')\n\n# Yeni tarih özellikleri oluşturuyoruz\ndf['year'] = df['visitStartTime'].dt.year\ndf['month'] = df['visitStartTime'].dt.month\ndf['day'] = df['visitStartTime'].dt.day\ndf['weekday'] = df['visitStartTime'].dt.weekday\ndf['hour'] = df['visitStartTime'].dt.hour\n\nprint(df[['visitStartTime', 'year', 'month', 'day', 'weekday', 'hour']].head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T18:11:22.405546Z","iopub.execute_input":"2025-04-27T18:11:22.405945Z","iopub.status.idle":"2025-04-27T18:11:22.879543Z","shell.execute_reply.started":"2025-04-27T18:11:22.40592Z","shell.execute_reply":"2025-04-27T18:11:22.878656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 'device' kolonunu JSON gibi açalım\ndef extract_device_info(row):\n    try:\n        data = json.loads(row.replace(\"'\", '\"'))\n        return pd.Series({\n            'device_browser': data.get('browser', None),\n            'device_operatingSystem': data.get('operatingSystem', None),\n            'device_isMobile': data.get('isMobile', None),\n            'device_deviceCategory': data.get('deviceCategory', None),\n        })\n    except:\n        return pd.Series({\n            'device_browser': None,\n            'device_operatingSystem': None,\n            'device_isMobile': None,\n            'device_deviceCategory': None,\n        })\n\n# Uygula\ndevice_features = df['device'].apply(extract_device_info)\n\n# Yeni feature'ları ana dataframe'e ekleyelim\ndf = pd.concat([df, device_features], axis=1)\n\nprint(df[['device_browser', 'device_operatingSystem', 'device_isMobile', 'device_deviceCategory']].head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T18:11:26.587277Z","iopub.execute_input":"2025-04-27T18:11:26.587637Z","iopub.status.idle":"2025-04-27T18:17:15.763722Z","shell.execute_reply.started":"2025-04-27T18:11:26.587612Z","shell.execute_reply":"2025-04-27T18:17:15.76269Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 'geoNetwork' kolonunu JSON gibi açalım\ndef extract_geo_info(row):\n    try:\n        data = json.loads(row.replace(\"'\", '\"'))\n        return pd.Series({\n            'geo_continent': data.get('continent', None),\n            'geo_country': data.get('country', None),\n            'geo_city': data.get('city', None),\n            'geo_networkDomain': data.get('networkDomain', None),\n        })\n    except:\n        return pd.Series({\n            'geo_continent': None,\n            'geo_country': None,\n            'geo_city': None,\n            'geo_networkDomain': None,\n        })\n\n# Uygula\ngeo_features = df['geoNetwork'].apply(extract_geo_info)\n\n# Yeni feature'ları ana dataframe'e ekle\ndf = pd.concat([df, geo_features], axis=1)\n\nprint(df[['geo_continent', 'geo_country', 'geo_city', 'geo_networkDomain']].head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T14:19:35.742335Z","iopub.execute_input":"2025-04-27T14:19:35.743154Z","iopub.status.idle":"2025-04-27T14:24:52.468571Z","shell.execute_reply.started":"2025-04-27T14:19:35.743121Z","shell.execute_reply":"2025-04-27T14:24:52.467678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"drop_cols = ['device', 'geoNetwork', 'trafficSource', 'totals', 'visitId']\ndf = df.drop(drop_cols, axis=1)\n\nprint(df.isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T18:23:58.059533Z","iopub.execute_input":"2025-04-27T18:23:58.060193Z","iopub.status.idle":"2025-04-27T18:23:58.234614Z","shell.execute_reply.started":"2025-04-27T18:23:58.060159Z","shell.execute_reply":"2025-04-27T18:23:58.233748Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\ncat_cols = ['device_browser', 'device_operatingSystem', 'device_deviceCategory',\n            'geo_continent', 'geo_country', 'geo_city', 'geo_networkDomain']\n\nle = LabelEncoder()\nfor col in cat_cols:\n    df[col] = le.fit_transform(df[col].astype(str))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T18:24:01.35775Z","iopub.execute_input":"2025-04-27T18:24:01.358082Z","iopub.status.idle":"2025-04-27T18:24:03.631143Z","shell.execute_reply.started":"2025-04-27T18:24:01.358059Z","shell.execute_reply":"2025-04-27T18:24:03.629929Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Adım 3: Veri Hazırlığı**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\n# Transaction revenue'u dönüştür\ndf['transactionRevenue'] = pd.to_numeric(df['transactionRevenue'])\ndf['transactionRevenue'] = np.log1p(df['transactionRevenue'])\n\n# Feature seçimi\nfeatures = [\n    'visitNumber', 'year', 'month', 'day', 'weekday', 'hour',\n    'device_browser', 'device_operatingSystem', 'device_isMobile', 'device_deviceCategory',\n    'geo_continent', 'geo_country', 'geo_city', 'geo_networkDomain'\n]\n\nX = df[features]\ny = df['transactionRevenue']\n\n# Boolean kolonları düzelt\nX['device_isMobile'] = X['device_isMobile'].astype('int8')\n\n# Kategorik kolonları One-Hot Encode et\nX = pd.get_dummies(X)\n\nX.head()\ny.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T14:36:17.604826Z","iopub.execute_input":"2025-04-27T14:36:17.605119Z","iopub.status.idle":"2025-04-27T14:36:17.805342Z","shell.execute_reply.started":"2025-04-27T14:36:17.605101Z","shell.execute_reply":"2025-04-27T14:36:17.804512Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Adım 4: Train/Test Split**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Eğitim ve test verilerini ayıralım\nX_train, X_test, y_train, y_test = train_test_split(\n    X, y, test_size=0.2, random_state=42\n)\n\nprint(f\"X_train shape: {X_train.shape}\")\nprint(f\"X_test shape: {X_test.shape}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T14:36:38.439369Z","iopub.execute_input":"2025-04-27T14:36:38.439658Z","iopub.status.idle":"2025-04-27T14:36:38.828746Z","shell.execute_reply.started":"2025-04-27T14:36:38.439639Z","shell.execute_reply":"2025-04-27T14:36:38.827816Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Adım 5: İlk Model — LightGBM**","metadata":{}},{"cell_type":"code","source":"import lightgbm as lgb\nfrom lightgbm import early_stopping\nfrom sklearn.metrics import mean_squared_error, r2_score\nimport numpy as np\n\n# LightGBM veri seti oluştur\ntrain_data = lgb.Dataset(X_train, label=y_train)\ntest_data = lgb.Dataset(X_test, label=y_test, reference=train_data)\n\n# Model parametreleri\nparams = {\n    'objective': 'regression',\n    'metric': 'rmse',\n    'boosting_type': 'gbdt',\n    'learning_rate': 0.05,\n    'num_leaves': 31,\n    'verbose': -1\n}\n\nevals_result = {}\n\nlgb_model = lgb.train(\n    params,\n    train_data,\n    num_boost_round=1000,\n    valid_sets=[train_data, test_data],\n    valid_names=['train', 'valid'],\n    callbacks=[\n        lgb.record_evaluation(evals_result),\n        lgb.early_stopping(stopping_rounds=50),\n        lgb.log_evaluation(period=100)  # Burada kaç iterasyonda bir loglasın istiyorsun\n    ]\n)\n\n# Tahmin yap\ny_pred = lgb_model.predict(X_test, num_iteration=lgb_model.best_iteration)\n\n# Değerlendirme\nrmse = np.sqrt(mean_squared_error(y_test, y_pred))\nr2 = r2_score(y_test, y_pred)\n# MLP Loss grafiği\nplt.figure(figsize=(10, 6))\nplt.plot(mlp_model.loss_curve_, label='MLP Loss')\nplt.xlabel('Iteration')\nplt.ylabel('Loss (MSE)')\nplt.title('MLP - Loss vs Iterations')\nplt.legend()\nplt.grid(True)\nplt.tight_layout()\nplt.show()\nprint(f\"LightGBM RMSE: {rmse}\")\nprint(f\"LightGBM R^2 Score (Accuracy): {r2 * 100:.2f}%\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T15:29:46.186903Z","iopub.execute_input":"2025-04-27T15:29:46.187237Z","iopub.status.idle":"2025-04-27T15:30:14.454592Z","shell.execute_reply.started":"2025-04-27T15:29:46.187207Z","shell.execute_reply":"2025-04-27T15:30:14.453516Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Adım 6: İkinci Model MLP(Multi-Layer Perceptron)**\n","metadata":{"execution":{"iopub.status.busy":"2025-04-27T13:31:30.991245Z","iopub.execute_input":"2025-04-27T13:31:30.992019Z","iopub.status.idle":"2025-04-27T13:31:32.007429Z","shell.execute_reply.started":"2025-04-27T13:31:30.991987Z","shell.execute_reply":"2025-04-27T13:31:32.00618Z"}}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nfrom sklearn.neural_network import MLPRegressor\nfrom sklearn.metrics import mean_squared_error, r2_score\nimport numpy as np\n\n# 1. Veriyi ölçeklendirme\nscaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_test_scaled = scaler.transform(X_test)\n\n# 2. MLP modelini tanımlama\nmlp_model = MLPRegressor(hidden_layer_sizes=(128, 64, 32),\n                         activation='relu',\n                         solver='adam',\n                         max_iter=100,\n                         early_stopping=True,\n                         random_state=42)\n\n# 3. Modeli eğitme\nmlp_model.fit(X_train_scaled, y_train)\n\n# 4. Tahminler\ny_pred_mlp = mlp_model.predict(X_test_scaled)\n\n# 5. Performans ölçümü\nmlp_rmse = mean_squared_error(y_test, y_pred_mlp, squared=False)\nmlp_r2 = r2_score(y_test, y_pred_mlp) * 100\n\nprint(f\"MLP RMSE: {mlp_rmse}\")\nprint(f\"MLP R^2 Score (Accuracy): {mlp_r2:.2f}%\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T14:39:48.934972Z","iopub.execute_input":"2025-04-27T14:39:48.935299Z","iopub.status.idle":"2025-04-27T15:05:29.887655Z","shell.execute_reply.started":"2025-04-27T14:39:48.935276Z","shell.execute_reply":"2025-04-27T15:05:29.886411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# MLP Loss grafiği\nplt.figure(figsize=(10, 6))\nplt.plot(mlp_model.loss_curve_, label='MLP Loss')\nplt.xlabel('Iteration')\nplt.ylabel('Loss (MSE)')\nplt.title('MLP - Loss vs Iterations')\nplt.legend()\nplt.grid(True)\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T15:23:04.670282Z","iopub.execute_input":"2025-04-27T15:23:04.670585Z","iopub.status.idle":"2025-04-27T15:23:04.927482Z","shell.execute_reply.started":"2025-04-27T15:23:04.670566Z","shell.execute_reply":"2025-04-27T15:23:04.926573Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Adım 7: Model-3 CatBoost**","metadata":{}},{"cell_type":"code","source":"from catboost import CatBoostRegressor\nfrom sklearn.metrics import mean_squared_error, r2_score\n\n# CatBoost modelini oluştur\ncat_model = CatBoostRegressor(\n    iterations=1000,\n    learning_rate=0.05,\n    depth=6,\n    loss_function='RMSE',\n    eval_metric='RMSE',\n    verbose=100,\n    random_seed=42\n)\n\n# Modeli eğit\ncat_model.fit(X_train, y_train, eval_set=(X_test, y_test), early_stopping_rounds=50)\n\n# Tahmin yap\ny_pred_cat = cat_model.predict(X_test)\n\n# Başarı metriği hesapla\ncat_rmse = mean_squared_error(y_test, y_pred_cat, squared=False)\ncat_r2 = r2_score(y_test, y_pred_cat)\n\n# CatBoost RMSE grafiği\ncat_evals = cat_model.get_evals_result()\n\nplt.figure(figsize=(10, 6))\nplt.plot(cat_evals['learn']['RMSE'], label='CatBoost Train RMSE')\nplt.plot(cat_evals['validation']['RMSE'], label='CatBoost Validation RMSE')\nplt.xlabel('Iteration')\nplt.ylabel('RMSE')\nplt.title('CatBoost - RMSE vs Iterations')\nplt.legend()\nplt.grid(True)\nplt.tight_layout()\nplt.show()\n\nprint(f\"CatBoost RMSE: {cat_rmse}\")\nprint(f\"CatBoost R^2 Score (Accuracy): {cat_r2 * 100:.2f}%\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T15:27:33.599612Z","iopub.execute_input":"2025-04-27T15:27:33.599984Z","iopub.status.idle":"2025-04-27T15:29:18.257504Z","shell.execute_reply.started":"2025-04-27T15:27:33.599963Z","shell.execute_reply":"2025-04-27T15:29:18.256468Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Adım 8: Grafikler yardımıyla sonuç kıyaslama**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Modellerin skorlarını tanımla\nmodels = ['LightGBM', 'MLP', 'CatBoost']\nrmse_scores = [0.06315, 0.06339, 0.06318]\nr2_scores = [4.05, 3.32, 3.95]\n\n# Grafik boyutunu ayarla\nfig, axes = plt.subplots(1, 2, figsize=(14, 6))\n\n# RMSE Grafik\naxes[0].bar(models, rmse_scores, color=['skyblue', 'lightgreen', 'salmon'])\naxes[0].set_title('Model Karşılaştırması - RMSE')\naxes[0].set_ylabel('RMSE Değeri (Daha düşük daha iyi)')\naxes[0].grid(axis='y')\n\n# R^2 Grafik\naxes[1].bar(models, r2_scores, color=['skyblue', 'lightgreen', 'salmon'])\naxes[1].set_title('Model Karşılaştırması - R² Skoru')\naxes[1].set_ylabel('Doğruluk (%) (Daha yüksek daha iyi)')\naxes[1].grid(axis='y')\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T15:15:06.552141Z","iopub.execute_input":"2025-04-27T15:15:06.55246Z","iopub.status.idle":"2025-04-27T15:15:06.927083Z","shell.execute_reply.started":"2025-04-27T15:15:06.552437Z","shell.execute_reply":"2025-04-27T15:15:06.926083Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Adım 9: Sonuçların iyileştirilmesi için Hiperparametre Optimizasyonu**","metadata":{}},{"cell_type":"markdown","source":"# **Adım 9.1: LightBoost için Hiperparametre Optimizasyonu**","metadata":{}},{"cell_type":"code","source":"!pip install lightgbm optuna","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T15:34:08.076951Z","iopub.execute_input":"2025-04-27T15:34:08.077351Z","iopub.status.idle":"2025-04-27T15:34:14.766948Z","shell.execute_reply.started":"2025-04-27T15:34:08.077324Z","shell.execute_reply":"2025-04-27T15:34:14.765616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import optuna\nimport lightgbm as lgb\nfrom lightgbm import early_stopping, log_evaluation\n\ndef objective(trial):\n    params = {\n        'n_estimators': trial.suggest_int('n_estimators', 100, 1000),\n        'max_depth': trial.suggest_int('max_depth', 3, 15),\n        'learning_rate': trial.suggest_float('learning_rate', 1e-4, 1e-1, log=True),\n        'num_leaves': trial.suggest_int('num_leaves', 20, 300),\n        'min_child_samples': trial.suggest_int('min_child_samples', 5, 100),\n        'subsample': trial.suggest_float('subsample', 0.5, 1.0),\n        'colsample_bytree': trial.suggest_float('colsample_bytree', 0.5, 1.0),\n        'reg_alpha': trial.suggest_float('reg_alpha', 1e-4, 10.0, log=True),\n        'reg_lambda': trial.suggest_float('reg_lambda', 1e-4, 10.0, log=True),\n        'random_state': 42,\n        'n_jobs': -1\n    }\n    \n    model = lgb.LGBMRegressor(**params)\n    \n    model.fit(\n        X_train_optuna, y_train_optuna,\n        eval_set=[(X_valid_optuna, y_valid_optuna)],\n        eval_metric='rmse',\n        callbacks=[early_stopping(stopping_rounds=50), log_evaluation(0)]\n    )\n    \n    preds = model.predict(X_valid_optuna)\n    rmse = mean_squared_error(y_valid_optuna, preds, squared=False)\n    \n    return rmse\n\n# Optuna çalıştır\nstudy = optuna.create_study(direction='minimize')\nstudy.optimize(objective, n_trials=10)\n\nprint(\"En iyi hiperparametreler:\")\nprint(study.best_params)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T15:38:09.162457Z","iopub.execute_input":"2025-04-27T15:38:09.162827Z","iopub.status.idle":"2025-04-27T16:20:38.667094Z","shell.execute_reply.started":"2025-04-27T15:38:09.162802Z","shell.execute_reply":"2025-04-27T16:20:38.666262Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\nfrom lightgbm import early_stopping, log_evaluation, record_evaluation\nfrom sklearn.metrics import mean_squared_error, r2_score\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Stil ayarı\nsns.set_style(\"whitegrid\")\nsns.set_context(\"talk\")\n\n# LightGBM veri seti oluştur\ntrain_data = lgb.Dataset(X_train, label=y_train)\nvalid_data = lgb.Dataset(X_test, label=y_test, reference=train_data)\n\n# Optuna ile bulunan en iyi parametreler\nparams = {\n    'objective': 'regression',\n    'metric': 'rmse',\n    'boosting_type': 'gbdt',\n    'learning_rate': 0.01599035963247372,\n    'num_leaves': 190,\n    'max_depth': 13,\n    'subsample': 0.5870915985404044,\n    'colsample_bytree': 0.5257230507016665,\n    'reg_alpha': 0.01318006722575277,\n    'reg_lambda': 0.17250259808580623,\n    'random_state': 42,\n    'verbose': -1,\n    'n_jobs': -1\n}\n\nevals_result = {}\n\n# Modeli eğit\nlgb_model = lgb.train(\n    params,\n    train_data,\n    num_boost_round=832,  # Optuna'dan gelen 'n_estimators'\n    valid_sets=[train_data, valid_data],\n    valid_names=['train', 'valid'],\n    callbacks=[\n        record_evaluation(evals_result),\n        early_stopping(stopping_rounds=50),\n        log_evaluation(period=100)\n    ]\n)\n\n# Test seti üzerinde tahmin yap\ny_pred = lgb_model.predict(X_test, num_iteration=lgb_model.best_iteration)\n\n# Performans ölçümleri\nrmse = np.sqrt(mean_squared_error(y_test, y_pred))\nr2 = r2_score(y_test, y_pred)\n\n# Loss (RMSE) grafiği seaborn ile çiz\nplt.figure(figsize=(12, 7))\nsns.lineplot(x=np.arange(len(evals_result['train']['rmse'])), y=evals_result['train']['rmse'], label='Train RMSE', linewidth=2)\nsns.lineplot(x=np.arange(len(evals_result['valid']['rmse'])), y=evals_result['valid']['rmse'], label='Validation RMSE', linewidth=2)\nplt.xlabel('Iteration', fontsize=14)\nplt.ylabel('RMSE', fontsize=14)\nplt.title('LightGBM - RMSE vs Iterations', fontsize=16, weight='bold')\nplt.legend(fontsize=12)\nplt.grid(True, which='both', linestyle='--', linewidth=0.5)\nplt.tight_layout()\nplt.show()\n\nprint(f\"LightGBM RMSE: {rmse}\")\nprint(f\"LightGBM R^2 Score (Accuracy): {r2 * 100:.2f}%\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T16:25:18.244865Z","iopub.execute_input":"2025-04-27T16:25:18.245226Z","iopub.status.idle":"2025-04-27T16:26:38.960136Z","shell.execute_reply.started":"2025-04-27T16:25:18.245204Z","shell.execute_reply":"2025-04-27T16:26:38.959021Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Adım 9.2: CatBoost Hiperparametre Optimizasyonu**","metadata":{}},{"cell_type":"code","source":"import optuna\nfrom catboost import CatBoostRegressor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error\n\nX = df.drop('target', axis=1)  # kendi dataframe ve target kolonuna göre değiştir\ny = df['target']\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# 2. Adım: Optuna objective fonksiyonu\ndef objective(trial):\n    params = {\n        'iterations': trial.suggest_int('iterations', 500, 1500),\n        'depth': trial.suggest_int('depth', 4, 10),\n        'learning_rate': trial.suggest_float('learning_rate', 1e-4, 1e-1, log=True),\n        'l2_leaf_reg': trial.suggest_float('l2_leaf_reg', 1e-3, 10.0, log=True),\n        'bagging_temperature': trial.suggest_float('bagging_temperature', 0.0, 1.0),\n        'random_strength': trial.suggest_float('random_strength', 0.0, 1.0),\n        'border_count': trial.suggest_int('border_count', 32, 255),\n        'verbose': 0,\n        'task_type': 'CPU',  # GPU varsa 'GPU' yap\n        'random_seed': 42\n    }\n    \n    model = CatBoostRegressor(**params)\n    \n    model.fit(\n        X_train, y_train,\n        eval_set=(X_test, y_test),\n        early_stopping_rounds=50,\n        verbose=0\n    )\n    \n    preds = model.predict(X_test)\n    rmse = mean_squared_error(y_test, preds, squared=False)\n    \n    return rmse\n\n# 3. Adım: Optuna ile hiperparametre arama\nstudy = optuna.create_study(direction=\"minimize\")\nstudy.optimize(objective, n_trials=10)\n\nprint(\"En iyi hiperparametreler:\")\nprint(study.best_params)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T17:37:21.13707Z","iopub.execute_input":"2025-04-27T17:37:21.13783Z","iopub.status.idle":"2025-04-27T17:37:21.152385Z","shell.execute_reply.started":"2025-04-27T17:37:21.137805Z","shell.execute_reply":"2025-04-27T17:37:21.151154Z"}},"outputs":[],"execution_count":null}]}