{"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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport datetime \nimport seaborn as sns\nimport plotly.offline as py\nimport matplotlib.pyplot as plt\nimport plotly.graph_objs as go","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Load the data\n\nweather_train = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/weather_train.csv\")\ntrain_data = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/train.csv\")\nbuilding_metadata = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/building_metadata.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/test.csv\")\nweather_test = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/weather_test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"building_metadata.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"*Reduce the memory Usage \nreduce the size of Integer to min size used in the column:*"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.memory_usage()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Reduce Memory Usuage \ndef reduce_mm_usage(df, verbose=True):\n    type_list = ['int16','int32','int64','float16','float32','float64']\n    \n    print(\"Inital memory usage in KB\", df.memory_usage().sum()/1024)\n    initial_mm = df.memory_usage().sum()/1024\n    for col in df.columns:\n        if df[col].dtypes in type_list:\n            \n            col_min = df[col].min()\n            col_max = df[col].max()\n            \n            if str(df[col].dtypes)[:3]==\"int\":\n                \n                if col_min > np.iinfo(np.int8).min and col_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif col_min > np.iinfo(np.int16).min and col_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif col_min > np.iinfo(np.int32).min and col_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif col_min > np.iinfo(np.int64).min and col_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)\n                    \n            else:\n                \n                if col_min > np.finfo(np.float16).min and col_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif col_min > np.finfo(np.float32).min and col_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else: #col_min > np.finfo(np.float64).min and col_max < np.finfo(np.float64).max:\n                    df[col] = df[col].astype(np.float64)\n    \n    end_mm = df.memory_usage().sum()/1024\n    print(\"after memory usuage in KB\", df.memory_usage().sum()/1024)\n    print(\"percetage reduction in memory\", ( (initial_mm-end_mm)/initial_mm) )\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = reduce_mm_usage(train_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data = reduce_mm_usage(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"building_metadata = reduce_mm_usage(building_metadata)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train = reduce_mm_usage(weather_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_test = reduce_mm_usage(weather_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#null values \ntrain_data.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.timestamp = pd.to_datetime(train_data.timestamp, format='%Y-%m-%d %H:%M:%S')\ntrain_data = train_data.set_index('timestamp')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#groupby buliding_id and meter_id, roll on monthly data over summation of meter reading \n\ntrain_data = train_data.groupby([pd.Grouper(freq='M'),'building_id','meter'], as_index=True)['meter_reading'].sum().reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.timestamp = train_data.timestamp.dt.strftime(\"%Y-%m-%d\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(18,12))\nlm = sns.scatterplot(x='timestamp',y=\"meter_reading\",data=train_data, hue='meter', palette='tab20b',s=100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Distribution of target variable that is meter_reading \nplt.figure(figsize=(18,12))\nsns.distplot(train_data.meter_reading)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Missing Data Analysis\n\ndef miss_data(df):\n    x = ['column_name','missing_data', 'missing_in_percentage']\n    missing_data = pd.DataFrame(columns=x)\n    columns = df.columns\n    for col in columns:\n        icolumn_name = col\n        imissing_data = df[col].isnull().sum()\n        imissing_in_percentage = (df[col].isnull().sum()/df[col].shape[0])*100\n        \n        missing_data.loc[len(missing_data)] = [icolumn_name, imissing_data, imissing_in_percentage]\n    print(missing_data)\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"miss_data(train_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"miss_data(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"miss_data(weather_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"miss_data(weather_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"miss_data(building_metadata)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"*Now Check the Correlation of target variable with others:::::::*"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.corr()['meter_reading'].sort_values()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.heatmap(train_data.corr(), cmap=\"rainbow\", vmin=-0.01, vmax=1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Check the distribution of variable in training data (train_data, weather_data) ::::: "},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_dist(df, column):\n    plt.figure(figsize=(18,12))\n    ax = sns.distplot(df[column].dropna())\n    ax.set_title(column+\" Distribution\", fontsize=16)\n    plt.xlabel(column, fontsize=12)\n    #plt.ylabel(\"distribution\", fontsize=12)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_dist(train_data, \"meter_reading\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_dist(weather_train, \"air_temperature\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_dist(weather_train, \"dew_temperature\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_dist(weather_train, \"sea_level_pressure\")","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}