{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#Meng-import module yang akan digunakan\nimport numpy as np #Untuk perhitungan yang berkaitan dengan matematika\nimport pandas as pd #Untuk read csv dan preprocessing data\n\n# Memudahkan import csv dari input kaggle\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n#Melakukan import csv dari data-data yang disediakan dengan panda.read_csv        \ntrain_data = pd.read_csv(\"../input/ashrae-energy-prediction/train.csv\")\nbuilding = pd.read_csv(\"../input/ashrae-energy-prediction/building_metadata.csv\")\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-11T12:40:26.260734Z","iopub.execute_input":"2023-05-11T12:40:26.261147Z","iopub.status.idle":"2023-05-11T12:40:36.975071Z","shell.execute_reply.started":"2023-05-11T12:40:26.261114Z","shell.execute_reply":"2023-05-11T12:40:36.974095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Membuat fungsi untuk reducing memory pada data sehingga program lebih ringan dan dapat tetap berjalan\ndef reduce_mem_usage(df, df_name):\n\n    start_mem = df.memory_usage().sum() / 1024**2\n    \n    for col in df.columns:\n\n        col_type = df[col].dtype\n\n        if col_type != object:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)\n        else:\n            df[col] = df[col].astype('category')\n\n    end_mem = df.memory_usage().sum() / 1024**2\n\n    print('Memory usage of {} is reduced by {:.2f} %. Usage dropped from {:.2f} MB to {:.2f} MB.'.format(df_name, (100 * (start_mem - end_mem) / start_mem), start_mem, end_mem))\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2023-05-11T12:40:36.976964Z","iopub.execute_input":"2023-05-11T12:40:36.977667Z","iopub.status.idle":"2023-05-11T12:40:36.992935Z","shell.execute_reply.started":"2023-05-11T12:40:36.977612Z","shell.execute_reply":"2023-05-11T12:40:36.991823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reducing memory dataframe yang telah diinisiasi\ntrain_data = reduce_mem_usage(train_data, 'Train Data')\nbuilding = reduce_mem_usage(building, 'Building Data')\n","metadata":{"execution":{"iopub.status.busy":"2023-05-11T12:40:37.040011Z","iopub.execute_input":"2023-05-11T12:40:37.040695Z","iopub.status.idle":"2023-05-11T12:40:39.870953Z","shell.execute_reply.started":"2023-05-11T12:40:37.040654Z","shell.execute_reply":"2023-05-11T12:40:39.870226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Meng-import pacakge/modul yang akan digunakan\nimport pandas as pd\nimport matplotlib as plt\nimport numpy as np\nimport seaborn as sns\nimport io\nimport plotly.express as px\nimport warnings\nwarnings.filterwarnings('ignore')\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-11T12:47:44.109027Z","iopub.execute_input":"2023-05-11T12:47:44.110777Z","iopub.status.idle":"2023-05-11T12:47:44.116593Z","shell.execute_reply.started":"2023-05-11T12:47:44.110742Z","shell.execute_reply":"2023-05-11T12:47:44.114914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Melakukan import csv dari data-data yang disediakan dengan panda.read_csv\nbuilding = pd.read_csv('/kaggle/input/ashrae-energy-prediction/building_metadata.csv')\nprint(building.info())\n\ntrain_data = pd.read_csv('/kaggle/input/ashrae-energy-prediction/train.csv')\nprint(train_data.info())","metadata":{"execution":{"iopub.status.busy":"2023-05-11T12:48:34.964017Z","iopub.execute_input":"2023-05-11T12:48:34.964408Z","iopub.status.idle":"2023-05-11T12:48:45.411476Z","shell.execute_reply.started":"2023-05-11T12:48:34.964382Z","shell.execute_reply":"2023-05-11T12:48:45.410365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Menggabungkan kolom 'site_id', 'building_id' dan 'primary_use', 'square_feet'\nbuilding2 = building[building['primary_use'] == 'Office']\nbuilding3 = building2[['site_id', 'building_id', 'primary_use', 'square_feet']]\nbuilding3","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:34:50.995919Z","iopub.execute_input":"2023-05-11T14:34:50.996320Z","iopub.status.idle":"2023-05-11T14:34:51.017171Z","shell.execute_reply.started":"2023-05-11T14:34:50.996291Z","shell.execute_reply":"2023-05-11T14:34:51.015825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Menggabungkan tabel train dengan kolom meter_reading\nenergi2 = train_data[train_data['meter'] == 0]\nenergi3 = energi2[energi2['meter_reading'] > 0]\nenergi3","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:35:01.413119Z","iopub.execute_input":"2023-05-11T14:35:01.413570Z","iopub.status.idle":"2023-05-11T14:35:02.630620Z","shell.execute_reply.started":"2023-05-11T14:35:01.413512Z","shell.execute_reply":"2023-05-11T14:35:02.629578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Menggabungkan tabel building, weather train dan train\nmergetrain = pd.merge(building3, energi3, on = 'building_id')\ndata = pd.concat([mergetrain])\ndata","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:35:05.511822Z","iopub.execute_input":"2023-05-11T14:35:05.512227Z","iopub.status.idle":"2023-05-11T14:35:07.411935Z","shell.execute_reply.started":"2023-05-11T14:35:05.512195Z","shell.execute_reply":"2023-05-11T14:35:07.411019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data.info())","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:35:11.955711Z","iopub.execute_input":"2023-05-11T14:35:11.956103Z","iopub.status.idle":"2023-05-11T14:35:11.965849Z","shell.execute_reply.started":"2023-05-11T14:35:11.956072Z","shell.execute_reply":"2023-05-11T14:35:11.964834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Informasi beberapa data kategorik\nid_lokasi = data['site_id'].unique()\nprint('Daftar site_id \\n', id_lokasi)\nprint('Banyak lokasi: ', len(id_lokasi),'\\n')\n\nbuilding_index = data['building_id'].unique()\nprint('Daftar building_id \\n', building_index)\nprint('Banyak gedung: ', len(building_index),'\\n')\n\nfungsi_building = data['primary_use'].unique()\nprint('Daftar fungsi gedung: ', fungsi_building)\nprint('Banyak kategori fungsi gedung: ', len(fungsi_building),'\\n')\n\njenis_meteran = data['meter'].unique()\nprint('Jenis meteran yang digunakan: ', jenis_meteran)\nprint('Banyak jenis meteran: ', len(jenis_meteran))","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:08:04.034787Z","iopub.execute_input":"2023-05-11T13:08:04.035801Z","iopub.status.idle":"2023-05-11T13:08:04.270699Z","shell.execute_reply.started":"2023-05-11T13:08:04.035763Z","shell.execute_reply":"2023-05-11T13:08:04.269590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Mengubah tipe data pada kolom  berikut\ndata['site_id'] = data['site_id'].astype('category')\ndata['building_id'] = data['building_id'].astype('category')\ndata['primary_use'] = data['primary_use'].astype('category')\n\ndata['meter'] = data['meter'].astype('category')","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:35:24.974723Z","iopub.execute_input":"2023-05-11T14:35:24.975091Z","iopub.status.idle":"2023-05-11T14:35:25.009152Z","shell.execute_reply.started":"2023-05-11T14:35:24.975062Z","shell.execute_reply":"2023-05-11T14:35:25.008265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Memeriksa info data\ndata.info() ","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:35:43.035425Z","iopub.execute_input":"2023-05-11T14:35:43.035823Z","iopub.status.idle":"2023-05-11T14:35:43.049017Z","shell.execute_reply.started":"2023-05-11T14:35:43.035793Z","shell.execute_reply":"2023-05-11T14:35:43.047886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Mengganti type data dari column timestamp dari object menjadi date time\ndata['timestamp'] = pd.to_datetime(data['timestamp'],format='%Y-%m-%d %H:%M:%S')\nnewdata = data.set_index('timestamp')\nnewdata.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:35:45.565465Z","iopub.execute_input":"2023-05-11T14:35:45.565851Z","iopub.status.idle":"2023-05-11T14:35:46.289413Z","shell.execute_reply.started":"2023-05-11T14:35:45.565822Z","shell.execute_reply":"2023-05-11T14:35:46.288291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Melihat informasi data\ndata.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:35:49.345678Z","iopub.execute_input":"2023-05-11T14:35:49.346779Z","iopub.status.idle":"2023-05-11T14:35:49.358263Z","shell.execute_reply.started":"2023-05-11T14:35:49.346730Z","shell.execute_reply":"2023-05-11T14:35:49.357067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:35:52.862370Z","iopub.execute_input":"2023-05-11T14:35:52.863475Z","iopub.status.idle":"2023-05-11T14:35:52.991230Z","shell.execute_reply.started":"2023-05-11T14:35:52.863433Z","shell.execute_reply":"2023-05-11T14:35:52.990136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc={'figure.figsize':(30,15)})\nsns.boxplot(newdata)\nplt.semilogy()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Melihat jumlah data yang terduplikasi\ndata.duplicated().sum() ","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:36:20.160314Z","iopub.execute_input":"2023-05-11T14:36:20.160719Z","iopub.status.idle":"2023-05-11T14:36:20.684539Z","shell.execute_reply.started":"2023-05-11T14:36:20.160687Z","shell.execute_reply":"2023-05-11T14:36:20.683589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Outlier\n#karena 'meter_reading' merupakan target variable, maka kita akan mencari apakah 'meter_reading' memiliki outlier atau tidak.\n\nfig, ax = plt.subplots(figsize=(8, 8))\nsns.distplot(train['meter_reading'], color='blue', ax = ax).set_title(\"Tabel Distribusi meter_reading\", fontsize=16)\nplt.xlabel('meter_reading', fontsize=12)\nplt.ylabel('Density', fontsize=12)\nplt.show()\n\nsns.boxplot(x=train['meter_reading'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Melihat jumlah Outlier\nnewdata.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:43:40.243835Z","iopub.execute_input":"2023-05-11T14:43:40.244252Z","iopub.status.idle":"2023-05-11T14:43:40.269349Z","shell.execute_reply.started":"2023-05-11T14:43:40.244216Z","shell.execute_reply":"2023-05-11T14:43:40.268261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Persentase missing values\n(newdata.isnull().sum()/len(newdata)*100).to_frame('persentase missing')","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:37:59.206300Z","iopub.execute_input":"2023-05-11T14:37:59.206595Z","iopub.status.idle":"2023-05-11T14:37:59.230292Z","shell.execute_reply.started":"2023-05-11T14:37:59.206569Z","shell.execute_reply":"2023-05-11T14:37:59.229401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Melihat informasi data\nnewdata.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:04:25.003495Z","iopub.execute_input":"2023-05-11T15:04:25.003896Z","iopub.status.idle":"2023-05-11T15:04:25.016097Z","shell.execute_reply.started":"2023-05-11T15:04:25.003864Z","shell.execute_reply":"2023-05-11T15:04:25.014832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Tabel histogram untuk memeriksa distribusi variabel\nnewdata.hist(bins=50, figsize=(24,15))\nplt.suptitle('Histogram', fontsize=16)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:15:10.447560Z","iopub.execute_input":"2023-05-11T15:15:10.447982Z","iopub.status.idle":"2023-05-11T15:15:12.020286Z","shell.execute_reply.started":"2023-05-11T15:15:10.447955Z","shell.execute_reply":"2023-05-11T15:15:12.019174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newdata_month = newdata.resample(\"M\").mean()\nnewdata_month","metadata":{"execution":{"iopub.status.busy":"2023-05-11T14:56:12.822412Z","iopub.execute_input":"2023-05-11T14:56:12.822854Z","iopub.status.idle":"2023-05-11T14:56:13.449962Z","shell.execute_reply.started":"2023-05-11T14:56:12.822819Z","shell.execute_reply":"2023-05-11T14:56:13.448890Z"},"trusted":true},"execution_count":null,"outputs":[]}]}