{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":9994,"databundleVersionId":752467,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns \nimport warnings\n\nwarnings.filterwarnings(\"ignore\")\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-06T10:03:06.84416Z","iopub.execute_input":"2025-02-06T10:03:06.844539Z","iopub.status.idle":"2025-02-06T10:03:08.061029Z","shell.execute_reply.started":"2025-02-06T10:03:06.844509Z","shell.execute_reply":"2025-02-06T10:03:08.059966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/ashrae-energy-prediction/train.csv')\nhava=pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_train.csv')\ntest=pd.read_csv('/kaggle/input/ashrae-energy-prediction/test.csv')\nhava_test=pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T10:03:08.062264Z","iopub.execute_input":"2025-02-06T10:03:08.06267Z","iopub.status.idle":"2025-02-06T10:04:00.34365Z","shell.execute_reply.started":"2025-02-06T10:03:08.062644Z","shell.execute_reply":"2025-02-06T10:04:00.342637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:18.268949Z","iopub.execute_input":"2025-02-06T09:28:18.26934Z","iopub.status.idle":"2025-02-06T09:28:20.391536Z","shell.execute_reply.started":"2025-02-06T09:28:18.269303Z","shell.execute_reply":"2025-02-06T09:28:20.390309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:20.393129Z","iopub.execute_input":"2025-02-06T09:28:20.393535Z","iopub.status.idle":"2025-02-06T09:28:20.407383Z","shell.execute_reply.started":"2025-02-06T09:28:20.393506Z","shell.execute_reply":"2025-02-06T09:28:20.406246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:20.408279Z","iopub.execute_input":"2025-02-06T09:28:20.408604Z","iopub.status.idle":"2025-02-06T09:28:20.427734Z","shell.execute_reply.started":"2025-02-06T09:28:20.408577Z","shell.execute_reply":"2025-02-06T09:28:20.426465Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info\ndf.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:20.428873Z","iopub.execute_input":"2025-02-06T09:28:20.429332Z","iopub.status.idle":"2025-02-06T09:28:21.531813Z","shell.execute_reply.started":"2025-02-06T09:28:20.429291Z","shell.execute_reply":"2025-02-06T09:28:21.530529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hava.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:21.532676Z","iopub.execute_input":"2025-02-06T09:28:21.532975Z","iopub.status.idle":"2025-02-06T09:28:21.549322Z","shell.execute_reply.started":"2025-02-06T09:28:21.53295Z","shell.execute_reply":"2025-02-06T09:28:21.548267Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hava.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:21.552128Z","iopub.execute_input":"2025-02-06T09:28:21.5525Z","iopub.status.idle":"2025-02-06T09:28:21.601109Z","shell.execute_reply.started":"2025-02-06T09:28:21.552459Z","shell.execute_reply":"2025-02-06T09:28:21.599922Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hava.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:21.602502Z","iopub.execute_input":"2025-02-06T09:28:21.602798Z","iopub.status.idle":"2025-02-06T09:28:21.680156Z","shell.execute_reply.started":"2025-02-06T09:28:21.602775Z","shell.execute_reply":"2025-02-06T09:28:21.679059Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hava.isnull().sum().sort_values(ascending=False)/139773*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:21.681318Z","iopub.execute_input":"2025-02-06T09:28:21.681758Z","iopub.status.idle":"2025-02-06T09:28:21.702889Z","shell.execute_reply.started":"2025-02-06T09:28:21.681721Z","shell.execute_reply":"2025-02-06T09:28:21.701658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.subplot(1,2,1)\nhava['precip_depth_1_hr'].hist(bins=50, edgecolor='black')\nplt.subplot(1,2,2)\nhava['cloud_coverage'].hist(bins=50, edgecolor='black')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:21.704165Z","iopub.execute_input":"2025-02-06T09:28:21.704527Z","iopub.status.idle":"2025-02-06T09:28:22.348471Z","shell.execute_reply.started":"2025-02-06T09:28:21.704501Z","shell.execute_reply":"2025-02-06T09:28:22.347463Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hava['precip_depth_1_hr'].fillna(0, inplace=True)\n\nfor i in ['cloud_coverage','sea_level_pressure','wind_direction', 'wind_speed','dew_temperature','air_temperature']:\n    hava[i].fillna(hava[i].median(), inplace=True)\n\nhava_test['precip_depth_1_hr'].fillna(0, inplace=True)\n\nfor i in ['cloud_coverage','sea_level_pressure','wind_direction', 'wind_speed','dew_temperature','air_temperature']:\n    hava_test[i].fillna(hava_test[i].median(), inplace=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:22.349958Z","iopub.execute_input":"2025-02-06T09:28:22.350448Z","iopub.status.idle":"2025-02-06T09:28:22.399665Z","shell.execute_reply.started":"2025-02-06T09:28:22.350407Z","shell.execute_reply":"2025-02-06T09:28:22.398408Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hava_test.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:28.377894Z","iopub.execute_input":"2025-02-06T09:28:28.37835Z","iopub.status.idle":"2025-02-06T09:28:28.410578Z","shell.execute_reply.started":"2025-02-06T09:28:28.378318Z","shell.execute_reply":"2025-02-06T09:28:28.409337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hava.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:30.617486Z","iopub.execute_input":"2025-02-06T09:28:30.617871Z","iopub.status.idle":"2025-02-06T09:28:30.638637Z","shell.execute_reply.started":"2025-02-06T09:28:30.617839Z","shell.execute_reply":"2025-02-06T09:28:30.637533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(7,7))\nsns.heatmap(hava.drop(columns=['site_id','timestamp']).corr(), annot=True, cmap='coolwarm')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:33.388783Z","iopub.execute_input":"2025-02-06T09:28:33.38929Z","iopub.status.idle":"2025-02-06T09:28:33.889857Z","shell.execute_reply.started":"2025-02-06T09:28:33.389247Z","shell.execute_reply":"2025-02-06T09:28:33.888583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hava.drop(columns=['dew_temperature'], inplace=True)\nhava_test.drop(columns=['dew_temperature'], inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:38.626256Z","iopub.execute_input":"2025-02-06T09:28:38.626654Z","iopub.status.idle":"2025-02-06T09:28:38.655851Z","shell.execute_reply.started":"2025-02-06T09:28:38.626624Z","shell.execute_reply":"2025-02-06T09:28:38.653877Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"meta=pd.read_csv('/kaggle/input/ashrae-energy-prediction/building_metadata.csv')\ndf=df.merge(meta[['building_id', 'site_id']], on='building_id', how='left')\ntest=test.merge(meta[['building_id', 'site_id']], on='building_id', how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:40.464238Z","iopub.execute_input":"2025-02-06T09:28:40.464603Z","iopub.status.idle":"2025-02-06T09:28:48.099532Z","shell.execute_reply.started":"2025-02-06T09:28:40.464576Z","shell.execute_reply":"2025-02-06T09:28:48.098481Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['timestamp']=pd.to_datetime(df['timestamp'])\nhava['timestamp']=pd.to_datetime(hava['timestamp'])\ntest['timestamp']=pd.to_datetime(test['timestamp'])\nhava_test['timestamp']=pd.to_datetime(hava_test['timestamp'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:28:55.717615Z","iopub.execute_input":"2025-02-06T09:28:55.718105Z","iopub.status.idle":"2025-02-06T09:29:04.487992Z","shell.execute_reply.started":"2025-02-06T09:28:55.718053Z","shell.execute_reply":"2025-02-06T09:29:04.486907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.merge(hava, on=['timestamp', 'site_id'], how='left')\ntest = test.merge(hava_test, on=['timestamp', 'site_id'], how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:29:07.475966Z","iopub.execute_input":"2025-02-06T09:29:07.476357Z","iopub.status.idle":"2025-02-06T09:29:17.484638Z","shell.execute_reply.started":"2025-02-06T09:29:07.476331Z","shell.execute_reply":"2025-02-06T09:29:17.483586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['hour']=df['timestamp'].dt.hour\ndf['day']=df['timestamp'].dt.day\ndf['month']=df['timestamp'].dt.month\ndf['weekday']=df['timestamp'].dt.weekday\n\ntest['hour']=test['timestamp'].dt.hour\ntest['day']=test['timestamp'].dt.day\ntest['month']=test['timestamp'].dt.month\ntest['weekday']=test['timestamp'].dt.weekday\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:29:54.928415Z","iopub.execute_input":"2025-02-06T09:29:54.928782Z","iopub.status.idle":"2025-02-06T09:30:01.982356Z","shell.execute_reply.started":"2025-02-06T09:29:54.928756Z","shell.execute_reply":"2025-02-06T09:30:01.9813Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:30:05.137578Z","iopub.execute_input":"2025-02-06T09:30:05.138025Z","iopub.status.idle":"2025-02-06T09:30:05.160972Z","shell.execute_reply.started":"2025-02-06T09:30:05.137996Z","shell.execute_reply":"2025-02-06T09:30:05.159559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hourly_mean=df.groupby('hour')['meter_reading'].mean().sort_values(ascending=False)\nprint(hourly_mean)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:30:09.635213Z","iopub.execute_input":"2025-02-06T09:30:09.635586Z","iopub.status.idle":"2025-02-06T09:30:10.095276Z","shell.execute_reply.started":"2025-02-06T09:30:09.635558Z","shell.execute_reply":"2025-02-06T09:30:10.094181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"monthly_mean=df.groupby('month')['meter_reading'].mean().sort_values(ascending=False)\nprint(monthly_mean)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:30:10.096445Z","iopub.execute_input":"2025-02-06T09:30:10.096743Z","iopub.status.idle":"2025-02-06T09:30:10.556839Z","shell.execute_reply.started":"2025-02-06T09:30:10.09672Z","shell.execute_reply":"2025-02-06T09:30:10.555892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_season(month):\n    if month in [12,1,2]:\n        return 'Winter'\n    elif month in [3,4,5]:\n        return 'spring'\n    elif month in [6,7,8]:\n        return 'summer'\n    elif month in [9,10,11]:\n        return 'fall'\ndf['season']=df['month'].apply(get_season)\ntest['season']=test['month'].apply(get_season)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:30:12.907297Z","iopub.execute_input":"2025-02-06T09:30:12.907737Z","iopub.status.idle":"2025-02-06T09:30:29.295409Z","shell.execute_reply.started":"2025-02-06T09:30:12.907705Z","shell.execute_reply":"2025-02-06T09:30:29.294417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.groupby('season')['meter_reading'].mean().sort_values(ascending=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:30:33.306673Z","iopub.execute_input":"2025-02-06T09:30:33.307026Z","iopub.status.idle":"2025-02-06T09:30:34.982996Z","shell.execute_reply.started":"2025-02-06T09:30:33.307Z","shell.execute_reply":"2025-02-06T09:30:34.981841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.get_dummies(df, columns=['season'], drop_first=True)\ntest=pd.get_dummies(test, columns=['season'], drop_first=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:30:43.278217Z","iopub.execute_input":"2025-02-06T09:30:43.278714Z","iopub.status.idle":"2025-02-06T09:30:56.834496Z","shell.execute_reply.started":"2025-02-06T09:30:43.278677Z","shell.execute_reply":"2025-02-06T09:30:56.833205Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Machine Learning Algorithm","metadata":{}},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:11:31.555075Z","iopub.execute_input":"2025-02-06T09:11:31.555713Z","iopub.status.idle":"2025-02-06T09:11:31.581651Z","shell.execute_reply.started":"2025-02-06T09:11:31.555675Z","shell.execute_reply":"2025-02-06T09:11:31.58043Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Aykırı değerleri temizleyelim (üst %1'lik dilimi keselim)\ndf = df[df[\"meter_reading\"] < df[\"meter_reading\"].quantile(0.99)]\n\n# Log dönüşümü uygulayalım (enerji tüketimindeki büyük değerleri normalize eder)\n#df[\"meter_reading\"] = np.log1p(df[\"meter_reading\"])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:31:09.388556Z","iopub.execute_input":"2025-02-06T09:31:09.389113Z","iopub.status.idle":"2025-02-06T09:31:11.430112Z","shell.execute_reply.started":"2025-02-06T09:31:09.389031Z","shell.execute_reply":"2025-02-06T09:31:11.428929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX=df.drop(columns=['meter_reading', 'timestamp'])\ny=df['meter_reading']\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:31:28.697471Z","iopub.execute_input":"2025-02-06T09:31:28.698065Z","iopub.status.idle":"2025-02-06T09:31:36.919245Z","shell.execute_reply.started":"2025-02-06T09:31:28.698006Z","shell.execute_reply":"2025-02-06T09:31:36.918201Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.metrics import mean_squared_error\n\n# LightGBM için veri setlerini oluştur\ntrain_data = lgb.Dataset(X_train, label=y_train)\nval_data = lgb.Dataset(X_val, label=y_val)\n\n# LightGBM parametreleri\nparams = {\n    'objective': 'regression',\n    'metric': 'rmse',\n    'boosting_type': 'gbdt',\n    'learning_rate': 0.05,  # Daha küçük learning rate ile daha iyi öğrenme sağlanır\n    'num_leaves': 50,  # Yaprak sayısını artırarak daha iyi modelleme sağlarız\n    'feature_fraction': 0.8,\n    'bagging_fraction': 0.8,\n    'bagging_freq': 5\n}\n\n# Modeli eğitme (Doğru early stopping kullanımı)\nmodel = lgb.train(\n    params,\n    train_data,\n    valid_sets=[val_data],\n    num_boost_round=2000,\n    callbacks=[\n        lgb.early_stopping(stopping_rounds=100),  # Erken durdurma mekanizması\n        lgb.log_evaluation(period=100)  # Her 100 iterasyonda metrikleri logla\n    ]\n)\n\n# Doğrulama seti için tahmin yapalım\ny_pred = model.predict(X_val)\n\n# RMSE hesaplayalım\nrmse = mean_squared_error(y_val, y_pred, squared=False)\nprint(f\"Validation RMSE: {rmse}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T09:31:56.19865Z","iopub.execute_input":"2025-02-06T09:31:56.199156Z","iopub.status.idle":"2025-02-06T10:01:01.235782Z","shell.execute_reply.started":"2025-02-06T09:31:56.199023Z","shell.execute_reply":"2025-02-06T10:01:01.233298Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Modelin ihtiyacı olmayan sütunları kaldıralım\nX_test = test.drop(columns=['timestamp', 'row_id'])\n\n# Modeli kullanarak tahmin yapalım\ntest[\"meter_reading\"] = model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T10:01:44.395566Z","iopub.execute_input":"2025-02-06T10:01:44.395946Z","iopub.status.idle":"2025-02-06T10:01:44.483771Z","shell.execute_reply.started":"2025-02-06T10:01:44.395916Z","shell.execute_reply":"2025-02-06T10:01:44.482246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Submission dosyası oluştur\nsubmission = pd.DataFrame({\n    \"row_id\": test_df[\"row_id\"],  # Kaggle test verisindeki satır ID'si\n    \"meter_reading\": test_df[\"meter_reading\"]\n})\n\n# CSV olarak kaydedelim\nsubmission.to_csv(\"submission.csv\", index=False)\n\nprint(\"Submission dosyası başarıyla oluşturuldu!\")\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}