{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","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":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"![](https://avatars.mds.yandex.net/i?id=56a9a3ed4f9f28fe8e7bfd780189a0e2c0a47ace-9577606-images-thumbs&n=13)","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.simplefilter(action='ignore', category=RuntimeWarning)\ndf_train=pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/train.csv\")\ndf_test=pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/test.csv\")\ndf_weather_train=pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/weather_train.csv\")\ndf_weather_test=pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/weather_test.csv\")\ndf_building=pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/building_metadata.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:20:25.894215Z","iopub.execute_input":"2025-08-16T12:20:25.894629Z","iopub.status.idle":"2025-08-16T12:21:20.300011Z","shell.execute_reply.started":"2025-08-16T12:20:25.894597Z","shell.execute_reply":"2025-08-16T12:21:20.299078Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:20.301657Z","iopub.execute_input":"2025-08-16T12:21:20.301961Z","iopub.status.idle":"2025-08-16T12:21:20.324936Z","shell.execute_reply.started":"2025-08-16T12:21:20.301937Z","shell.execute_reply":"2025-08-16T12:21:20.323923Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:20.325838Z","iopub.execute_input":"2025-08-16T12:21:20.326096Z","iopub.status.idle":"2025-08-16T12:21:20.34543Z","shell.execute_reply.started":"2025-08-16T12:21:20.326077Z","shell.execute_reply":"2025-08-16T12:21:20.344449Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:20.347193Z","iopub.execute_input":"2025-08-16T12:21:20.347485Z","iopub.status.idle":"2025-08-16T12:21:22.36074Z","shell.execute_reply.started":"2025-08-16T12:21:20.347463Z","shell.execute_reply":"2025-08-16T12:21:22.359706Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"meter is categorical thats why i am not gonna call that right skewed but meter reading is right skewed which means we cant use here KNN or SVM because of that models based on skewed","metadata":{}},{"cell_type":"code","source":"df_train[\"meter_reading\"].unique","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:22.361646Z","iopub.execute_input":"2025-08-16T12:21:22.361975Z","iopub.status.idle":"2025-08-16T12:21:22.370097Z","shell.execute_reply.started":"2025-08-16T12:21:22.361952Z","shell.execute_reply":"2025-08-16T12:21:22.369279Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"that is not classification problem,its a regression","metadata":{}},{"cell_type":"code","source":"df_weather_train.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:22.370821Z","iopub.execute_input":"2025-08-16T12:21:22.371137Z","iopub.status.idle":"2025-08-16T12:21:22.399233Z","shell.execute_reply.started":"2025-08-16T12:21:22.371116Z","shell.execute_reply":"2025-08-16T12:21:22.398113Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train=df_train.merge(df_building,on=\"building_id\",how=\"left\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:22.400142Z","iopub.execute_input":"2025-08-16T12:21:22.40043Z","iopub.status.idle":"2025-08-16T12:21:26.733616Z","shell.execute_reply.started":"2025-08-16T12:21:22.400406Z","shell.execute_reply":"2025-08-16T12:21:26.732685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:26.734714Z","iopub.execute_input":"2025-08-16T12:21:26.735137Z","iopub.status.idle":"2025-08-16T12:21:26.748522Z","shell.execute_reply.started":"2025-08-16T12:21:26.735102Z","shell.execute_reply":"2025-08-16T12:21:26.747492Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train=df_train.merge(df_weather_train,on=[\"site_id\",\"timestamp\"],how=\"left\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:26.749641Z","iopub.execute_input":"2025-08-16T12:21:26.750043Z","iopub.status.idle":"2025-08-16T12:21:35.589263Z","shell.execute_reply.started":"2025-08-16T12:21:26.750008Z","shell.execute_reply":"2025-08-16T12:21:35.588221Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:35.591868Z","iopub.execute_input":"2025-08-16T12:21:35.592206Z","iopub.status.idle":"2025-08-16T12:21:35.610121Z","shell.execute_reply.started":"2025-08-16T12:21:35.592182Z","shell.execute_reply":"2025-08-16T12:21:35.609223Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.isnull().any()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:35.611081Z","iopub.execute_input":"2025-08-16T12:21:35.611404Z","iopub.status.idle":"2025-08-16T12:21:38.106911Z","shell.execute_reply.started":"2025-08-16T12:21:35.611375Z","shell.execute_reply":"2025-08-16T12:21:38.106161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:38.107658Z","iopub.execute_input":"2025-08-16T12:21:38.107915Z","iopub.status.idle":"2025-08-16T12:21:49.251326Z","shell.execute_reply.started":"2025-08-16T12:21:38.107867Z","shell.execute_reply":"2025-08-16T12:21:49.25033Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"i am gonna preprocessing with first columns","metadata":{}},{"cell_type":"code","source":"df_train=df_train.drop(columns=[\"building_id\",\"site_id\"],axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:49.252171Z","iopub.execute_input":"2025-08-16T12:21:49.252416Z","iopub.status.idle":"2025-08-16T12:21:50.333453Z","shell.execute_reply.started":"2025-08-16T12:21:49.252399Z","shell.execute_reply":"2025-08-16T12:21:50.332338Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:21:50.334469Z","iopub.execute_input":"2025-08-16T12:21:50.334731Z","iopub.status.idle":"2025-08-16T12:21:50.350958Z","shell.execute_reply.started":"2025-08-16T12:21:50.334709Z","shell.execute_reply":"2025-08-16T12:21:50.349989Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"lets deal with date","metadata":{}},{"cell_type":"code","source":"df_train[\"timestamp\"]=pd.to_datetime(df_train[\"timestamp\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:23:03.047412Z","iopub.execute_input":"2025-08-16T12:23:03.047818Z","iopub.status.idle":"2025-08-16T12:23:05.425667Z","shell.execute_reply.started":"2025-08-16T12:23:03.047793Z","shell.execute_reply":"2025-08-16T12:23:05.424533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train[\"year\"]=df_train[\"timestamp\"].dt.year\ndf_train[\"month\"]=df_train[\"timestamp\"].dt.month\ndf_train[\"day\"]=df_train[\"timestamp\"].dt.day\ndf_train[\"hour\"]=df_train[\"timestamp\"].dt.hour\ndf_train[\"minute\"]=df_train[\"timestamp\"].dt.minute\ndf_train[\"second\"]=df_train[\"timestamp\"].dt.second\ndf_train=df_train.drop(\"timestamp\",axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:26:32.972712Z","iopub.execute_input":"2025-08-16T12:26:32.973106Z","iopub.status.idle":"2025-08-16T12:26:37.592829Z","shell.execute_reply.started":"2025-08-16T12:26:32.97308Z","shell.execute_reply":"2025-08-16T12:26:37.591905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:27:04.500962Z","iopub.execute_input":"2025-08-16T12:27:04.502402Z","iopub.status.idle":"2025-08-16T12:27:04.524732Z","shell.execute_reply.started":"2025-08-16T12:27:04.502339Z","shell.execute_reply":"2025-08-16T12:27:04.52345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train[\"primary_use\"].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:32:34.634959Z","iopub.execute_input":"2025-08-16T12:32:34.635659Z","iopub.status.idle":"2025-08-16T12:32:36.239932Z","shell.execute_reply.started":"2025-08-16T12:32:34.63562Z","shell.execute_reply":"2025-08-16T12:32:36.238767Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle=LabelEncoder()\ndf_train[\"primary_use\"]=le.fit_transform(df_train[\"primary_use\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:35:04.146483Z","iopub.execute_input":"2025-08-16T12:35:04.146994Z","iopub.status.idle":"2025-08-16T12:35:07.766837Z","shell.execute_reply.started":"2025-08-16T12:35:04.146958Z","shell.execute_reply":"2025-08-16T12:35:07.765698Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"i thought to use get dummies method but that will do +15 columns thats really bad idea","metadata":{}},{"cell_type":"code","source":"df_train[\"primary_use\"].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:35:12.985021Z","iopub.execute_input":"2025-08-16T12:35:12.986455Z","iopub.status.idle":"2025-08-16T12:35:13.127014Z","shell.execute_reply.started":"2025-08-16T12:35:12.986412Z","shell.execute_reply":"2025-08-16T12:35:13.125571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train[\"square_feet\"].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:40:22.644386Z","iopub.execute_input":"2025-08-16T12:40:22.644714Z","iopub.status.idle":"2025-08-16T12:40:22.799335Z","shell.execute_reply.started":"2025-08-16T12:40:22.644692Z","shell.execute_reply":"2025-08-16T12:40:22.798033Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"we need to scaler that","metadata":{}},{"cell_type":"code","source":"df_train[\"year_built\"].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:40:39.161529Z","iopub.execute_input":"2025-08-16T12:40:39.162019Z","iopub.status.idle":"2025-08-16T12:40:39.342538Z","shell.execute_reply.started":"2025-08-16T12:40:39.161993Z","shell.execute_reply":"2025-08-16T12:40:39.341425Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"i need to choose a method here so i have two options first way is tree based regressor model second is linear regressor models\ni will look again describe.t if there is so much skewed's i should pick tree based model","metadata":{}},{"cell_type":"code","source":"df_train.describe().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:44:06.298797Z","iopub.execute_input":"2025-08-16T12:44:06.300174Z","iopub.status.idle":"2025-08-16T12:44:20.34655Z","shell.execute_reply.started":"2025-08-16T12:44:06.300117Z","shell.execute_reply":"2025-08-16T12:44:20.345622Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"all features based on right skewed should use tree based model here","metadata":{}},{"cell_type":"markdown","source":"okay so lets look outlier values because if there is too much outlier values i will NaN values equal to median if not i will equal to mean","metadata":{}},{"cell_type":"code","source":"df_train.isnull().any()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:56:44.903905Z","iopub.execute_input":"2025-08-16T12:56:44.904292Z","iopub.status.idle":"2025-08-16T12:56:45.586785Z","shell.execute_reply.started":"2025-08-16T12:56:44.904269Z","shell.execute_reply":"2025-08-16T12:56:45.585862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"col_list = [\"floor_count\",\"air_temperature\",\"cloud_coverage\",\"dew_temperature\",\n            \"precip_depth_1_hr\",\"sea_level_pressure\",\"wind_direction\",\"wind_speed\"]\n\nfor col in col_list:\n    plt.figure(figsize=(8,4))\n    sns.boxplot(x=df_train[col])\n    plt.title(f'Boxplot of {col}')\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T13:01:16.920219Z","iopub.execute_input":"2025-08-16T13:01:16.92061Z","iopub.status.idle":"2025-08-16T13:01:31.443761Z","shell.execute_reply.started":"2025-08-16T13:01:16.920583Z","shell.execute_reply":"2025-08-16T13:01:31.442631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for c in col_list:\n    median=df_train[c].median()\n    df_train[c]=df_train[c].fillna(median)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T13:08:22.406342Z","iopub.execute_input":"2025-08-16T13:08:22.406795Z","iopub.status.idle":"2025-08-16T13:08:26.226878Z","shell.execute_reply.started":"2025-08-16T13:08:22.406767Z","shell.execute_reply":"2025-08-16T13:08:26.226015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mean=df_train[\"year_built\"].mean()\ndf_train[\"year_built\"]=df_train[\"year_built\"].fillna(mean)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-16T12:54:00.289999Z","iopub.execute_input":"2025-08-16T12:54:00.291261Z","iopub.status.idle":"2025-08-16T12:54:00.783699Z","shell.execute_reply.started":"2025-08-16T12:54:00.291204Z","shell.execute_reply":"2025-08-16T12:54:00.782351Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.isnull().any()","metadata":{"trusted":true,"execution":{"execution_failed":"2025-08-16T13:43:47.28Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"##lets try with drop year day month\nx=df_train.drop(columns=[\"meter_reading\",\"year\",\"month\",\"day\"],axis=1)\ny=df_train[\"meter_reading\"]\nmodel.fit(x,y)\nmodel.score(x,y)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"lets see real score","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import cross_val_score\nscore=cross_val_score(model,x,y,cv=5,scoring=\"r2\")\nprint(score)","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}