{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from pandas.api.types import is_datetime64_any_dtype as is_datetime\nfrom pandas.api.types import is_categorical_dtype\nimport gc\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"trainB=pd.read_csv('/kaggle/input/ashrae-energy-prediction/train.csv')\ntrainB_meta=pd.read_csv('/kaggle/input/ashrae-energy-prediction/building_metadata.csv')\ntestB=pd.read_csv('/kaggle/input/ashrae-energy-prediction/test.csv')\ntrainw=pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_train.csv')\ntestw=pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_test.csv')\nsubmission=pd.read_csv('/kaggle/input/ashrae-energy-prediction/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"def reduce_mem_usage(df, use_float16=False):\n    \"\"\"\n    Iterate through all the columns of a dataframe and modify the data type to reduce memory usage.        \n    \"\"\"\n    \n    start_mem = df.memory_usage().sum() / 1024**2\n    print(\"Memory usage of dataframe is {:.2f} MB\".format(start_mem))\n    \n    for col in df.columns:\n        if is_datetime(df[col]) or is_categorical_dtype(df[col]):\n            continue\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 use_float16 and 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    print(\"Memory usage after optimization is: {:.2f} MB\".format(end_mem))\n    print(\"Decreased by {:.1f}%\".format(100 * (start_mem - end_mem) / start_mem))\n    \n    return df\n\ntrainB= reduce_mem_usage(trainB,use_float16=True)\ntestB= reduce_mem_usage(testB,use_float16=True)\ntrainw= reduce_mem_usage(trainw,use_float16=True)\ntestw= reduce_mem_usage(testw,use_float16=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Concatenate weather data ,anargy consumption and building meta data for test and training sets\ndef merge(trainw,trainB,trainB_meta):\n    trainB=trainB.merge(trainB_meta, left_on='building_id',right_on='building_id',how='left')\n    train= trainB.merge(trainw,how='left',left_on=['site_id','timestamp'],right_on=['site_id','timestamp'])\n    del trainw\n    del trainB\n    del trainB_meta\n    gc.collect()\n    train = reduce_mem_usage(train,use_float16=True)\n    return train;\ntrain=merge(trainw,trainB,trainB_meta)\ntest=merge(testw,testB,trainB_meta)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Train Data****"},{"metadata":{"trusted":true},"cell_type":"code","source":"train.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#The percentage of null data in each column in train set \ntrain.isnull().sum()*100/train.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#The total of null data in each column in train set \ndef miss_val(train):\n    percent = (train.isnull().sum()).sort_values(ascending=False)\n    percent.plot(kind=\"bar\", figsize = (20,10), fontsize = 20)\n    plt.xlabel(\"Columns\", fontsize = 20)\n    plt.ylabel(\"Number of rows\", fontsize = 20)\n    plt.title(\"Total Missing Values per column\", fontsize = 20)\nmiss_val(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# The percentage of null lignes \ndef miss_val_lines(train):\n\n    print(len(np.where(train.isnull().sum(axis=1)==0)[0])*100/train.shape[0])\n    print(len(np.where(train.isnull().sum(axis=1)==1)[0])*100/train.shape[0])\n    print(len(np.where(train.isnull().sum(axis=1)==2)[0])*100/train.shape[0])\n    print(len(np.where(train.isnull().sum(axis=1)==3)[0])*100/train.shape[0])\n    print(len(np.where(train.isnull().sum(axis=1)==4)[0])*100/train.shape[0])\n    print(len(np.where(train.isnull().sum(axis=1)==5)[0])*100/train.shape[0])\n    print(len(np.where(train.isnull().sum(axis=1)==6)[0])*100/train.shape[0])\n    print(len(np.where(train.isnull().sum(axis=1)==7)[0])*100/train.shape[0])\n    print(len(np.where(train.isnull().sum(axis=1)==8)[0])*100/train.shape[0])\n    print(len(np.where(train.isnull().sum(axis=1)==9)[0])*100/train.shape[0])\nmiss_val_lines(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#The distribution of primary_use column\ndef cat_column(train):\n    \n    fig = plt.figure(figsize=(20,15))\n    train.groupby('primary_use').primary_use.count().sort_values(ascending=False).plot.bar(ylim=0)\n    plt.show()\ncat_column(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# The change in energy consumption by date\nimport plotly.express as px\ndef energy_cons(train):   \n    train['timestamp'] = pd.to_datetime(train.timestamp, format='%Y-%m-%d %H:%M:%S')\n    train['date']=train['timestamp'].dt.date\n    train_temp_df_meter = train.groupby('date')['meter_reading'].sum()\n    train_temp_df_meter = train_temp_df_meter.to_frame().reset_index()\n    fig = px.line(train_temp_df_meter, x='date', y='meter_reading')\n    fig.show()\nenergy_cons(train)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Test Data********"},{"metadata":{"trusted":true},"cell_type":"code","source":"test=test.drop('row_id',axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.isnull().sum()*100/test.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"miss_val(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"miss_val_lines(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cat_column(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}