{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-07-27T18:08:48.344310Z","iopub.execute_input":"2026-07-27T18:08:48.344551Z","iopub.status.idle":"2026-07-27T18:08:52.819625Z","shell.execute_reply.started":"2026-07-27T18:08:48.344530Z","shell.execute_reply":"2026-07-27T18:08:52.818910Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_PATH = \"/kaggle/input/competitions/ashrae-energy-prediction/\"\n\ntrain_path = DATA_PATH + \"train.csv\"\nweather_path = DATA_PATH + \"weather_train.csv\"\nbuilding_path = DATA_PATH + \"building_metadata.csv\"\n\ntrain = pd.read_csv(train_path)\nweather = pd.read_csv(weather_path)\nbuilding = pd.read_csv(building_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T18:08:52.820875Z","iopub.execute_input":"2026-07-27T18:08:52.821265Z","iopub.status.idle":"2026-07-27T18:09:01.474710Z","shell.execute_reply.started":"2026-07-27T18:08:52.821240Z","shell.execute_reply":"2026-07-27T18:09:01.474113Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"timestamp\"] = pd.to_datetime(train[\"timestamp\"])\nweather[\"timestamp\"] = pd.to_datetime(weather[\"timestamp\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T18:09:01.475525Z","iopub.execute_input":"2026-07-27T18:09:01.475720Z","iopub.status.idle":"2026-07-27T18:09:03.073818Z","shell.execute_reply.started":"2026-07-27T18:09:01.475703Z","shell.execute_reply":"2026-07-27T18:09:03.073104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = train.merge(\n    building,\n    on=\"building_id\",\n    how=\"left\"\n)\n\n\ndf = df.merge(\n    weather,\n    on=[\"site_id\", \"timestamp\"],\n    how=\"left\"\n)\n\n\nprint(\"After Merge:\", df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T18:09:03.075378Z","iopub.execute_input":"2026-07-27T18:09:03.075625Z","iopub.status.idle":"2026-07-27T18:09:06.382903Z","shell.execute_reply.started":"2026-07-27T18:09:03.075603Z","shell.execute_reply":"2026-07-27T18:09:06.382318Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.sort_values(\n    [\"building_id\", \"timestamp\"]\n)\n\ndf[\"previous_hour_consumption\"] = (\n    df.groupby(\n        [\"building_id\", \"meter\"]\n    )[\"meter_reading\"]\n    .shift(1)\n)\n\ndf[\"previous_day_consumption\"] = (\n    df.groupby(\n        [\"building_id\", \"meter\"]\n    )[\"meter_reading\"]\n    .shift(24)\n)\n\ndf[\"previous_week_consumption\"] = (\n    df.groupby(\n        [\"building_id\", \"meter\"]\n    )[\"meter_reading\"]\n    .shift(168)\n)\n\ndf[\"consumption_diff_day\"] = (\n    df[\"previous_hour_consumption\"]\n    -\n    df[\"previous_day_consumption\"]\n)\n\ndf[\"rolling_3h_mean\"] = (\n    df.groupby(\n        [\"building_id\", \"meter\"]\n    )[\"meter_reading\"]\n    .shift(1)\n    .rolling(3)\n    .mean()\n)\n\ndf[\"rolling_24h_mean\"] = (\n    df.groupby(\n        [\"building_id\", \"meter\"]\n    )[\"meter_reading\"]\n    .shift(1)\n    .rolling(24)\n    .mean()\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T18:09:06.383651Z","iopub.execute_input":"2026-07-27T18:09:06.383869Z","iopub.status.idle":"2026-07-27T18:09:13.748197Z","shell.execute_reply.started":"2026-07-27T18:09:06.383848Z","shell.execute_reply":"2026-07-27T18:09:13.747428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Keep electricity + cooling + heating\ndf = df[df[\"meter\"].isin([0, 1, 2])]\nprint(\"After meter filter:\", df.shape)","metadata":{"trusted":true},"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\ndf[\"dayofyear\"] = df[\"timestamp\"].dt.dayofyear\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"drop_columns = [\n    \"floor_count\",\n    \"cloud_coverage\",\n    \"precip_depth_1_hr\",\n    \"wind_direction\",\n    \"sea_level_pressure\"\n]\n\ndf = df.dropna(\n    subset=[\n        \"previous_hour_consumption\",\n        \"previous_day_consumption\"\n    ]\n)\n\ndf.drop(\n    drop_columns,\n    axis=1,\n    inplace=True\n)\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"year_built\"] = df[\"year_built\"].fillna(\n    df[\"year_built\"].median()\n)\n\nweather_features = [\n    \"air_temperature\",\n    \"dew_temperature\",\n    \"wind_speed\"\n]\n\nfor col in weather_features:\n    df[col] = df[col].fillna(\n        df[col].median()\n    )\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"encoder = LabelEncoder()\n\ndf[\"primary_use\"] = encoder.fit_transform(\n    df[\"primary_use\"]\n)\n\nprint(df.isnull().sum())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Building age\ncurrent_year = 2026\n\ndf[\"building_age\"] = current_year - df[\"year_built\"]\n\n\n# Working hour feature\ndf[\"is_working_hour\"] = (\n    (df[\"hour\"] >= 8) &\n    (df[\"hour\"] <= 18)\n).astype(int)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = [\n    \"building_id\",\n    \"site_id\",\n    \"meter\",\n    \"primary_use\",\n\n    \"square_feet\",\n    \"year_built\",\n    \"building_age\",\n\n    \"air_temperature\",\n    \"dew_temperature\",\n    \"wind_speed\",\n\n    \"hour\",\n    \"day\",\n    \"month\",\n    \"weekday\",\n    \"dayofyear\",\n\n    \"is_working_hour\",\n\n    \"previous_hour_consumption\",\n    \"previous_day_consumption\"\n]\n\nX = df[features]\n\ny = df[\"meter_reading\"]\n\ny = np.log1p(y)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"split_date = \"2016-10-01\"\n\ntrain_df = df[df[\"timestamp\"] < split_date]\ntest_df = df[df[\"timestamp\"] >= split_date]\n\nX_train = train_df[features]\ny_train = np.log1p(train_df[\"meter_reading\"])\n\nX_test = test_df[features]\ny_test = np.log1p(test_df[\"meter_reading\"])\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.drop(\n    \"timestamp\",\n    axis=1,\n    inplace=True\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.to_csv(\n    \"X_train.csv\",\n    index=False\n)\n\nX_test.to_csv(\n    \"X_test.csv\",\n    index=False\n)\n\ny_train.to_csv(\n    \"y_train.csv\",\n    index=False\n)\n\ny_test.to_csv(\n    \"y_test.csv\",\n    index=False\n)\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"digital_twin_data = df.sample(\n    5000,\n    random_state=42\n)\n\n\ndigital_twin_data.to_csv(\n    \"digital_twin_data.csv\",\n    index=False\n)\n\n\nprint(\"Preprocessing Completed Successfully!\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"meter\"].value_counts()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(y_test.head())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape\nX_test.shape\nX_train.head()","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}